|
| 1 | +name: 'Generate GitHub App token' |
| 2 | +description: 'Generate an API token for a GitHub app' |
| 3 | + |
| 4 | +inputs: |
| 5 | + app_id: |
| 6 | + description: 'The app id' |
| 7 | + type: string |
| 8 | + installation_id: |
| 9 | + description: 'The app installation id' |
| 10 | + type: string |
| 11 | + private_key: |
| 12 | + description: 'The app private key in PEM format' |
| 13 | + type: string |
| 14 | +outputs: |
| 15 | + token: |
| 16 | + description: "The generated GitHub App token" |
| 17 | + value: ${{ steps.token.outputs.value }} |
| 18 | + |
| 19 | +runs: |
| 20 | + using: "composite" |
| 21 | + steps: |
| 22 | + - name: Create temporary directory |
| 23 | + id: tempdir |
| 24 | + shell: bash |
| 25 | + run: | |
| 26 | + tempdir=$(mktemp -d) |
| 27 | + echo "tempdir=${tempdir}" >> $GITHUB_OUTPUT |
| 28 | +
|
| 29 | + - name: Create JWT |
| 30 | + id: jwt |
| 31 | + shell: bash |
| 32 | + run: | |
| 33 | + DIR="${{ steps.tempdir.outputs.tempdir }}" |
| 34 | + python3 -m venv ${DIR}/.venv |
| 35 | + source ${DIR}/.venv/bin/activate |
| 36 | + pip install --quiet --upgrade pip |
| 37 | + pip install --quiet jwt |
| 38 | +
|
| 39 | + cat > ./create-jwt.py << EOF |
| 40 | + #!/usr/bin/env python3 |
| 41 | + from time import time |
| 42 | + from os import environ |
| 43 | + from sys import argv |
| 44 | +
|
| 45 | + from jwt import JWT, jwk_from_pem |
| 46 | +
|
| 47 | + private_key, app_id, now = argv[1], argv[2], int(time()) |
| 48 | + signing_key = jwk_from_pem(private_key.encode("utf-8")) |
| 49 | + print(JWT().encode(dict(iat=now, exp=now + 600, iss=app_id), signing_key, alg="RS256")) |
| 50 | + EOF |
| 51 | +
|
| 52 | + chmod 755 ./create-jwt.py |
| 53 | +
|
| 54 | + VALUE=$(./create-jwt.py "${{ inputs.private_key }}" "${{ inputs.app_id }}") |
| 55 | + echo "::add-mask::${VALUE}" |
| 56 | + echo "value=${VALUE}" >> "$GITHUB_OUTPUT" |
| 57 | +
|
| 58 | + - name: Create token |
| 59 | + id: token |
| 60 | + shell: bash |
| 61 | + run: | |
| 62 | + TOKEN=$(curl --silent --request POST \ |
| 63 | + --url "https://api.github.com/app/installations/${{ inputs.installation_id }}/access_tokens" \ |
| 64 | + --header "Accept: application/vnd.github+json" \ |
| 65 | + --header "Authorization: Bearer ${{ steps.jwt.outputs.value }}" \ |
| 66 | + --header "X-GitHub-Api-Version: 2022-11-28" \ |
| 67 | + | jq .token -r) |
| 68 | + echo "::add-mask::${TOKEN}" |
| 69 | + echo "value=${TOKEN}" >> "$GITHUB_OUTPUT" |
| 70 | +
|
| 71 | + - name: Delete temporary directory |
| 72 | + if: always() |
| 73 | + shell: bash |
| 74 | + run: | |
| 75 | + rm -rf ${{ steps.tempdir.outputs.tempdir }} |
0 commit comments