You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2022-03-28-gh-auth-login-in-actions.md
+15-11Lines changed: 15 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ There is a [`gh auth login --with-token`](https://cli.github.com/manual/gh_auth_
17
17
18
18
## Example 1 - gh auth login
19
19
20
-
Here's an example GitHub Action sample for logging into the `gh cli` and using [`gh api`](https://cli.github.com/manual/gh_api) to retrieve a repositories topics:
20
+
Here's an example GitHub Action sample for logging into the `gh cli` and using [`gh api`](https://cli.github.com/manual/gh_api) to retrieve a repositories topics:
21
21
22
22
```yml
23
23
steps:
@@ -26,9 +26,11 @@ Here's an example GitHub Action sample for logging into the `gh cli` and using [
26
26
gh api -X GET /repos/${{ GITHUB.REPOSITORY }}/topics --jq='.names'
27
27
```
28
28
29
+
This works, but there's a better way that doesn't require running a `gh auth login` command at all.
30
+
29
31
## Example 2 - env variable
30
32
31
-
However, there is a better way. If you try to run a `gh` command without authenticating, you will see the following error message:
33
+
✨ If you try to run a `gh` command without authenticating, you will see the following error message:
32
34
33
35
> gh: To use GitHub CLI in a GitHub Actions workflow, set the GH_TOKEN environment variable. Example:
34
36
> ```yml
@@ -41,6 +43,7 @@ With this, you will notice you don't have to run `gh auth login` at all. You can
41
43
This is an example of the least privilege approach, setting the `env` variable at the [step](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsenv) level, and allowing different steps to use different tokens if needed:
42
44
43
45
```yml
46
+
steps:
44
47
- run: gh issue create --title "My new issue" --body "Here are more details."
45
48
env:
46
49
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -71,23 +74,24 @@ jobs:
71
74
72
75
This example combines concepts learned in this post with the [*Demystifying GitHub Apps: Using GitHub Apps to Replace Service Accounts*](/posts/github-apps/) post.
73
76
74
-
You may want to use a GitHub app to authenticate and use the `gh cli` in a GitHub Action workflow to do something. You can manage permissions more with the GitHub App, and installing it on the org / granting access to multiple repositories whereas `${{ secrets.GITHUB_TOKEN }}` only has access to resources inside of the repository running the action. In addition, you can give the actor a more meaningful name (e.g.: PR-Enforcer-Bot) vs. the default `github-actions[bot]` name.
77
+
You may want to use a GitHub app to authenticate and use the `gh cli` in a GitHub Action workflow to do something. You can manage permissions more with the GitHub App, and installing it on the org / granting access to multiple repositories whereas `${{ secrets.GITHUB_TOKEN }}` only has access to resources inside of the repository running the action. In addition, you can give the actor a more meaningful name (e.g.: `PR-Enforcer-Bot`) vs. the default `github-actions[bot]` user.
75
78
76
-
Here's an example that uses an app to create an issue in a *different* repository:
79
+
Here's an example that uses an app to create an issue in a *different* repository:
77
80
78
81
```yml
79
82
steps:
80
-
- uses: tibdex/github-app-token@v1
81
-
id: get_installation_token
83
+
- uses: actions/create-github-app-token@v1
84
+
id: app-token
82
85
with:
83
-
app_id: 170544
84
-
# installation_id not needed IF the app is installed on this current repo
85
-
installation_id: 29881931
86
-
private_key: ${{ secrets.PRIVATE_KEY }}
86
+
app-id: ${{ vars.APP_ID }}
87
+
private-key: ${{ secrets.PRIVATE_KEY }}
88
+
# optional: owner not needed IF the app has access to the repo running the workflow
89
+
# if you get 'RequestError [HttpError]: Not Found 404', pass in owner
0 commit comments