Skip to content

Deal with expired installation access tokens #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ jobs:
)

const getInstallationAccessToken = require('./get-installation-access-token')
const accessToken = await getInstallationAccessToken(
const { expiresAt, token: accessToken } = await getInstallationAccessToken(
console,
appId,
privateKey,
installationId
)

core.setSecret(accessToken)
core.setOutput('expires-at', expiresAt)
core.setOutput('token', accessToken)

- name: get check run id
Expand Down Expand Up @@ -295,6 +296,45 @@ jobs:
exit 1
fi

- name: refresh installation token (if needed)
if: env.CREATE_CHECK_RUN != 'false'
id: refresh
uses: actions/github-script@v6
with:
script: |
// GitHub Apps' installation access tokens expire after one hour, see:
// https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps#authenticating-as-an-installation
// let's generate a new one if less than 5 minutes before the expiry date, otherwise reuse it
if (Date.parse('${{ steps.setup.outputs.expires-at }}') - Date.now() > 5 * 60 * 1000) {
core.setOutput('expires-at', '${{ steps.setup.outputs.expires-at }}')
core.setOutput('token', '${{ steps.setup.outputs.token }}')
core.info('Continuing to use the unexpired installation access token')
return
}
const appId = ${{ secrets.GH_APP_ID }}
const privateKey = `${{ secrets.GH_APP_PRIVATE_KEY }}`

const getAppInstallationId = require('./get-app-installation-id')
const installationId = await getAppInstallationId(
console,
appId,
privateKey,
process.env.OWNER,
process.env.REPO
)

const getInstallationAccessToken = require('./get-installation-access-token')
const { expiresAt, token: accessToken } = await getInstallationAccessToken(
console,
appId,
privateKey,
installationId
)

core.setSecret(accessToken)
core.setOutput('expires-at', expiresAt)
core.setOutput('token', accessToken)

- name: update check-run
if: env.CREATE_CHECK_RUN != 'false'
uses: actions/github-script@v6
Expand All @@ -303,7 +343,7 @@ jobs:
const updateCheckRun = require('./update-check-run')
await updateCheckRun(
console,
'${{ steps.setup.outputs.token }}',
'${{ steps.refresh.outputs.token }}',
process.env.OWNER,
process.env.REPO,
'${{ steps.check-run.outputs.id }}',
Expand Down Expand Up @@ -347,7 +387,7 @@ jobs:
const updateCheckRun = require('./update-check-run')
await updateCheckRun(
console,
'${{ steps.setup.outputs.token }}',
'${{ steps.refresh.outputs.token }}',
process.env.OWNER,
process.env.REPO,
'${{ steps.check-run.outputs.id }}',
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/create-azure-self-hosted-runners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
)

const getInstallationAccessToken = require('./get-installation-access-token')
const accessToken = await getInstallationAccessToken(
const { token: accessToken } = await getInstallationAccessToken(
console,
appId,
privateKey,
Expand Down
5 changes: 4 additions & 1 deletion get-installation-access-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ module.exports = async (context, appId, privateKey, installation_id) => {
'POST',
`/app/installations/${installation_id}/access_tokens`)
if (answer.error) throw answer.error
if (answer.token) return answer.token
if (answer.token) return {
expiresAt: answer.expires_at,
token: answer.token
}
throw new Error(`Unhandled response:\n${JSON.stringify(answer, null, 2)}`)
}