Skip to content

Commit

Permalink
feat: add get-github-token
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Apr 13, 2023
1 parent b49d89c commit 4da661d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ This is a collection of reusable composite actions for GitHub Actions workflows.
- [rebase](#rebase)
- [update-tags](#update-tags)
- [compare-branches](#compare-branches)
- [GitHub](#github)
- [get-github-token](#get-github-token)
- [Miscellaneous](#miscellaneous)
- [bundlewatch](#bundlewatch)
- [cache-nx](#cache-nx)
Expand Down Expand Up @@ -578,6 +580,49 @@ Check if there are new commits in head that are not in base.
| `commits` | List of commits in head that are not in base | `• fix: fix a bug (3 days ago)` |
| `compare-url` | Link to the compare view of both commits | `https://github.com/myparcelnl/woocommerce/compare/main..develop` |

### GitHub

#### get-github-token

[Source](get-github-token/action.yml)

Gets a GitHub token to use in the workflow. If `token` is passed, it will be used. Otherwise, an app token will be generated. Either `token` or `app-id` and `private-key` must be passed.

Meant for use within other actions, because obviously you could just use the `token` input directly.

##### Example

```yaml
- uses: myparcelnl/actions/get-github-token@v3
id: passed-token
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

- uses: myparcelnl/actions/get-github-token@v3
id: generated-token
with:
app-id: ${{ secrets.GITHUB_APP_ID }}
private-key: ${{ secrets.GITHUB_APP_PRIVATE_KEY }}

- run: echo ${{ steps.passed-token.outputs.token }} # The GitHub token that was passed in.

- run: echo ${{ steps.generated-token.outputs.token }} # The GitHub token that was generated.
```

##### Inputs

| Required | Name | Description | Example | Default |
| -------- | ------------- | ------------------------------------------------------------------------------------------------ | -------------------------------- | ------- |
| false | `token` | GitHub token to use. If passed, takes precedence over the `app-id` and `app-private-key` inputs. | `${{ secrets.GITHUB_TOKEN }}` ||
| false | `app-id` | The app ID of the app. | `${{ secrets.APP_ID }}` ||
| false | `private-key` | The private key of the app. | `${{ secrets.APP_PRIVATE_KEY }}` ||

##### Outputs

| Name | Description | Example |
| ------- | ----------------- | ------- |
| `token` | The GitHub token. | `***` |

### Miscellaneous

#### bundlewatch
Expand Down
47 changes: 47 additions & 0 deletions get-github-token/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: 'Get a GitHub token'
description: 'Get a GitHub token. If `token` is passed, it will be used. Otherwise, an app token will be generated.'

inputs:
token:
description: 'GitHub token to use. If passed, takes precedence over the `app-id` and `app-private-key` inputs.'

app-id:
description: 'The app ID of the app.'

private-key:
description: 'The private key of the app.'

outputs:
token:
description: 'The GitHub token.'
value: ${{ steps.token.outputs.token || steps.app.outputs.token }}

runs:
using: composite
steps:
- id: log
shell: bash
run: |
echo "token=${{ inputs.token }}"
echo "app-id=${{ inputs.app-id }}"
echo "private-key=${{ inputs.private-key }}"
- id: app
if: inputs.token == '' && (inputs.app-id != '' || inputs.private-key != '')
uses: myparcelnl/actions/setup-app-credentials@v3
with:
app-id: ${{ inputs.app-id }}
private-key: ${{ inputs.private-key }}

- id: token
if: inputs.token != ''
shell: bash
run: |
echo "token=${{ inputs.token }}" >> $GITHUB_OUTPUT
- id: error
if: inputs.token == '' && (inputs.app-id == '' || inputs.private-key == '')
shell: bash
run: |
echo "::error::No GitHub token or app credentials provided."
exit 1

0 comments on commit 4da661d

Please sign in to comment.