Skip to content

Commit 8801555

Browse files
committed
updating the phrasing and github app action used in the gh auth login post
1 parent ef5f865 commit 8801555

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

_posts/2022-03-28-gh-auth-login-in-actions.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ There is a [`gh auth login --with-token`](https://cli.github.com/manual/gh_auth_
1717

1818
## Example 1 - gh auth login
1919

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:
2121

2222
```yml
2323
steps:
@@ -26,9 +26,11 @@ Here's an example GitHub Action sample for logging into the `gh cli` and using [
2626
gh api -X GET /repos/${{ GITHUB.REPOSITORY }}/topics --jq='.names'
2727
```
2828
29+
This works, but there's a better way that doesn't require running a `gh auth login` command at all.
30+
2931
## Example 2 - env variable
3032

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:
3234

3335
> gh: To use GitHub CLI in a GitHub Actions workflow, set the GH_TOKEN environment variable. Example:
3436
> ```yml
@@ -41,6 +43,7 @@ With this, you will notice you don't have to run `gh auth login` at all. You can
4143
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:
4244

4345
```yml
46+
steps:
4447
- run: gh issue create --title "My new issue" --body "Here are more details."
4548
env:
4649
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -71,23 +74,24 @@ jobs:
7174

7275
This example combines concepts learned in this post with the [*Demystifying GitHub Apps: Using GitHub Apps to Replace Service Accounts*](/posts/github-apps/) post.
7376

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.
7578

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:
7780

7881
```yml
7982
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
8285
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
90+
owner: ${{ github.repository_owner }}
8791
8892
- name: Create Issue
8993
env:
90-
GH_TOKEN: ${{ steps.get_installation_token.outputs.token }}
94+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
9195
run: |
9296
gh issue create --title "My new issue" --body "Here are more details." \
9397
-R my-org/my-repo

0 commit comments

Comments
 (0)