Unverified Commit e69de2ff authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

[ci]: app-based release workflow (#93245)

Moves all release workflows off of a GH PAT and uses an app with a
short-lived token instead.

Test Plan:
Dry run
[here](https://github.com/vercel/next.js/actions/runs/24934593874).

However, this workflow is blocked until we figure out commit signing for
the bot app. Some options:
- The bot account generates a signing key and we use it in CI (not
great, bypasses the app)
- The org bypasses signature verification for the bot user (also not
great, requires an exemption rule)
- We need to rework the commit step so Lerna does not do the push, and
instead trigger it via the app + GH API. This seems like the best
option, will be added in a follow-up PR.

Note: `create-release-branch` workflow is broken in its current form, as
we will not be restoring administrator privileges to adjust environment
settings. This will become a manual step in the future.
parent a6782fa6
...@@ -548,10 +548,20 @@ jobs: ...@@ -548,10 +548,20 @@ jobs:
merge-multiple: true merge-multiple: true
path: crates/wasm path: crates/wasm
- name: Create GitHub App token
id: release-app-token
uses: actions/create-github-app-token@v3
with:
client-id: ${{ vars.RELEASE_GITHUB_APP_CLIENT_ID }}
private-key: ${{ secrets.RELEASE_GITHUB_APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
repositories: next.js
permission-contents: write
- run: ./scripts/publish-native.js - run: ./scripts/publish-native.js
- run: ./scripts/publish-release.js - run: ./scripts/publish-release.js
env: env:
RELEASE_BOT_GITHUB_TOKEN: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }} RELEASE_GITHUB_TOKEN: ${{ steps.release-app-token.outputs.token }}
buildPassed: buildPassed:
needs: ['deploy-target', 'build', 'build-wasm', 'build-native'] needs: ['deploy-target', 'build', 'build-wasm', 'build-native']
......
...@@ -11,10 +11,6 @@ on: ...@@ -11,10 +11,6 @@ on:
type: string type: string
required: true required: true
secrets:
RELEASE_BOT_GITHUB_TOKEN:
required: true
name: Create Release Branch name: Create Release Branch
env: env:
...@@ -39,13 +35,33 @@ jobs: ...@@ -39,13 +35,33 @@ jobs:
node-version: 20 node-version: 20
check-latest: true check-latest: true
- name: Create GitHub App token
id: release-app-token
uses: actions/create-github-app-token@v3
with:
client-id: ${{ vars.RELEASE_GITHUB_APP_CLIENT_ID }}
private-key: ${{ secrets.RELEASE_GITHUB_APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
repositories: next.js
permission-contents: write
permission-environments: write
permission-workflows: write
- name: Get GitHub App user ID
id: release-app-user
run: |
user_id="$(gh api "/users/${{ steps.release-app-token.outputs.app-slug }}[bot]" --jq .id)"
echo "user-id=$user_id" >> "$GITHUB_OUTPUT"
env:
GH_TOKEN: ${{ steps.release-app-token.outputs.token }}
- name: Clone Next.js repository - name: Clone Next.js repository
run: git clone https://github.com/vercel/next.js.git --depth=25 --single-branch --branch ${GITHUB_REF_NAME:-canary} . run: git clone https://github.com/vercel/next.js.git --depth=25 --single-branch --branch ${GITHUB_REF_NAME:-canary} .
- name: Check token - name: Check token
run: gh auth status run: gh auth status
env: env:
GITHUB_TOKEN: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }} GH_TOKEN: ${{ steps.release-app-token.outputs.token }}
# https://github.com/actions/virtual-environments/issues/1187 # https://github.com/actions/virtual-environments/issues/1187
- name: tune linux network - name: tune linux network
...@@ -72,6 +88,8 @@ jobs: ...@@ -72,6 +88,8 @@ jobs:
- run: node ./scripts/create-release-branch.js --branch-name "${INPUT_BRANCHNAME}" --tag-name "${INPUT_TAGNAME}" - run: node ./scripts/create-release-branch.js --branch-name "${INPUT_BRANCHNAME}" --tag-name "${INPUT_TAGNAME}"
env: env:
RELEASE_BOT_GITHUB_TOKEN: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }} RELEASE_GITHUB_TOKEN: ${{ steps.release-app-token.outputs.token }}
RELEASE_GITHUB_APP_SLUG: ${{ steps.release-app-token.outputs.app-slug }}
RELEASE_GITHUB_APP_USER_ID: ${{ steps.release-app-user.outputs.user-id }}
INPUT_BRANCHNAME: ${{ github.event.inputs.branchName }} INPUT_BRANCHNAME: ${{ github.event.inputs.branchName }}
INPUT_TAGNAME: ${{ github.event.inputs.tagName }} INPUT_TAGNAME: ${{ github.event.inputs.tagName }}
...@@ -84,11 +84,23 @@ jobs: ...@@ -84,11 +84,23 @@ jobs:
if: steps.precheck.outputs.should_evaluate == 'true' if: steps.precheck.outputs.should_evaluate == 'true'
run: pnpm install --frozen-lockfile --ignore-scripts run: pnpm install --frozen-lockfile --ignore-scripts
- name: Create GitHub App token
id: release-app-token
if: steps.precheck.outputs.should_evaluate == 'true'
uses: actions/create-github-app-token@v3
with:
client-id: ${{ vars.RELEASE_GITHUB_APP_CLIENT_ID }}
private-key: ${{ secrets.RELEASE_GITHUB_APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
repositories: next.js
permission-actions: read
permission-contents: read
- name: Evaluate backport release - name: Evaluate backport release
id: evaluate id: evaluate
if: steps.precheck.outputs.should_evaluate == 'true' if: steps.precheck.outputs.should_evaluate == 'true'
env: env:
RELEASE_BOT_GITHUB_TOKEN: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }} RELEASE_GITHUB_TOKEN: ${{ steps.release-app-token.outputs.token }}
WORKFLOW_RUN_ID: ${{ github.event_name == 'workflow_dispatch' && inputs.workflowRunId || github.event.workflow_run.id }} WORKFLOW_RUN_ID: ${{ github.event_name == 'workflow_dispatch' && inputs.workflowRunId || github.event.workflow_run.id }}
HEAD_SHA: ${{ github.event_name == 'workflow_dispatch' && inputs.headSha || github.event.workflow_run.head_sha }} HEAD_SHA: ${{ github.event_name == 'workflow_dispatch' && inputs.headSha || github.event.workflow_run.head_sha }}
HEAD_COMMIT_MESSAGE: ${{ github.event_name == 'workflow_dispatch' && '' || github.event.workflow_run.head_commit.message }} HEAD_COMMIT_MESSAGE: ${{ github.event_name == 'workflow_dispatch' && '' || github.event.workflow_run.head_commit.message }}
...@@ -108,9 +120,19 @@ jobs: ...@@ -108,9 +120,19 @@ jobs:
if: ${{ needs.evaluate.outputs.should_dispatch == 'true' && ((github.event_name == 'workflow_dispatch' && inputs.dispatch) || (github.event_name == 'workflow_run' && vars.ENABLE_BACKPORT_CANARY_SYNC == 'true')) }} if: ${{ needs.evaluate.outputs.should_dispatch == 'true' && ((github.event_name == 'workflow_dispatch' && inputs.dispatch) || (github.event_name == 'workflow_run' && vars.ENABLE_BACKPORT_CANARY_SYNC == 'true')) }}
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Create GitHub App token
id: release-app-token
uses: actions/create-github-app-token@v3
with:
client-id: ${{ vars.RELEASE_GITHUB_APP_CLIENT_ID }}
private-key: ${{ secrets.RELEASE_GITHUB_APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
repositories: next.js
permission-actions: write
- uses: actions/github-script@v7 - uses: actions/github-script@v7
with: with:
github-token: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }} github-token: ${{ steps.release-app-token.outputs.token }}
script: | script: |
await github.request( await github.request(
"POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches", "POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches",
......
...@@ -52,6 +52,24 @@ jobs: ...@@ -52,6 +52,24 @@ jobs:
node-version: 20 node-version: 20
check-latest: true check-latest: true
- name: Create GitHub App token
id: release-app-token
uses: actions/create-github-app-token@v3
with:
client-id: ${{ vars.RELEASE_GITHUB_APP_CLIENT_ID }}
private-key: ${{ secrets.RELEASE_GITHUB_APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
repositories: next.js
permission-contents: write
- name: Get GitHub App user ID
id: release-app-user
run: |
user_id="$(gh api "/users/${{ steps.release-app-token.outputs.app-slug }}[bot]" --jq .id)"
echo "user-id=$user_id" >> "$GITHUB_OUTPUT"
env:
GH_TOKEN: ${{ steps.release-app-token.outputs.token }}
- name: Clone Next.js repository - name: Clone Next.js repository
run: git clone https://github.com/vercel/next.js.git --depth=25 --single-branch --branch ${GITHUB_REF_NAME:-canary} . run: git clone https://github.com/vercel/next.js.git --depth=25 --single-branch --branch ${GITHUB_REF_NAME:-canary} .
...@@ -61,10 +79,13 @@ jobs: ...@@ -61,10 +79,13 @@ jobs:
# Ignoring failures for now to check if a failure truly implies a failed publish. # Ignoring failures for now to check if a failure truly implies a failed publish.
continue-on-error: true continue-on-error: true
env: env:
GITHUB_TOKEN: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }} GH_TOKEN: ${{ steps.release-app-token.outputs.token }}
- name: Get commit of the latest tag - name: Get commit of the latest tag
run: echo "LATEST_TAG_COMMIT=$(git rev-list -n 1 $(git describe --tags --abbrev=0))" >> $GITHUB_ENV run: |
git fetch --tags --force --deepen=1000 origin
latest_tag="$(git describe --tags --abbrev=0)"
echo "LATEST_TAG_COMMIT=$(git rev-list -n 1 "$latest_tag")" >> $GITHUB_ENV
- name: Get latest commit - name: Get latest commit
run: echo "LATEST_COMMIT=$(git rev-parse HEAD)" >> $GITHUB_ENV run: echo "LATEST_COMMIT=$(git rev-parse HEAD)" >> $GITHUB_ENV
...@@ -102,6 +123,8 @@ jobs: ...@@ -102,6 +123,8 @@ jobs:
- run: node ./scripts/start-release.js --release-type "${INPUT_RELEASETYPE}" --semver-type "${INPUT_SEMVERTYPE}" - run: node ./scripts/start-release.js --release-type "${INPUT_RELEASETYPE}" --semver-type "${INPUT_SEMVERTYPE}"
env: env:
RELEASE_BOT_GITHUB_TOKEN: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }} RELEASE_GITHUB_TOKEN: ${{ steps.release-app-token.outputs.token }}
RELEASE_GITHUB_APP_SLUG: ${{ steps.release-app-token.outputs.app-slug }}
RELEASE_GITHUB_APP_USER_ID: ${{ steps.release-app-user.outputs.user-id }}
INPUT_RELEASETYPE: ${{ github.event.inputs.releaseType || 'canary' }} INPUT_RELEASETYPE: ${{ github.event.inputs.releaseType || 'canary' }}
INPUT_SEMVERTYPE: ${{ github.event.inputs.semverType }} INPUT_SEMVERTYPE: ${{ github.event.inputs.semverType }}
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
const fs = require('fs/promises') const fs = require('fs/promises')
const path = require('path') const path = require('path')
const semver = require('semver') const semver = require('semver')
const {
getGitHubToken,
getGitHubTokenMissingMessage,
} = require('./release-github-auth')
const PUBLISH_RELEASE_JOB_NAME = 'Potentially publish release' const PUBLISH_RELEASE_JOB_NAME = 'Potentially publish release'
const TRIGGER_RELEASE_WORKFLOW = 'trigger_release.yml' const TRIGGER_RELEASE_WORKFLOW = 'trigger_release.yml'
...@@ -132,11 +136,11 @@ async function main() { ...@@ -132,11 +136,11 @@ async function main() {
throw new Error('Missing --head-sha') throw new Error('Missing --head-sha')
} }
const token = process.env.RELEASE_BOT_GITHUB_TOKEN || process.env.GITHUB_TOKEN const token = getGitHubToken() || process.env.GITHUB_TOKEN
const repoFullName = process.env.GITHUB_REPOSITORY const repoFullName = process.env.GITHUB_REPOSITORY
if (!token) { if (!token) {
throw new Error('Missing RELEASE_BOT_GITHUB_TOKEN or GITHUB_TOKEN') throw new Error(`${getGitHubTokenMissingMessage()} or GITHUB_TOKEN`)
} }
if (!repoFullName) { if (!repoFullName) {
......
...@@ -2,6 +2,12 @@ ...@@ -2,6 +2,12 @@
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const execa = require('execa') const execa = require('execa')
const {
configureGitHubAuth,
getGitHubToken,
getGitHubTokenMissingMessage,
verifyGitHubApiAccess,
} = require('./release-github-auth')
async function main() { async function main() {
const args = process.argv const args = process.argv
...@@ -16,25 +22,20 @@ async function main() { ...@@ -16,25 +22,20 @@ async function main() {
throw new Error('tagName value is invalid "' + tagName + '"') throw new Error('tagName value is invalid "' + tagName + '"')
} }
const githubToken = process.env.RELEASE_BOT_GITHUB_TOKEN const githubToken = getGitHubToken()
if (!githubToken) { if (!githubToken) {
console.log(`Missing RELEASE_BOT_GITHUB_TOKEN`) console.log(getGitHubTokenMissingMessage())
return return
} }
await execa( await configureGitHubAuth(githubToken)
`git remote set-url origin https://nextjs-bot:${githubToken}@github.com/vercel/next.js.git`, await verifyGitHubApiAccess(
{ stdio: 'inherit', shell: true } githubToken,
'/repos/vercel/next.js/environments/release-stable/deployment-branch-policies?per_page=1',
'release-stable deployment branch policies'
) )
await execa(`git config user.name "nextjs-bot"`, {
stdio: 'inherit',
shell: true,
})
await execa(`git config user.email "it+nextjs-bot@vercel.com"`, {
stdio: 'inherit',
shell: true,
})
await execa(`git checkout -b "${branchName}"`, { await execa(`git checkout -b "${branchName}"`, {
stdio: 'inherit', stdio: 'inherit',
shell: true, shell: true,
...@@ -96,6 +97,7 @@ async function main() { ...@@ -96,6 +97,7 @@ async function main() {
stdio: 'inherit', stdio: 'inherit',
shell: true, shell: true,
}) })
await execa(`git push origin "${branchName}"`, { await execa(`git push origin "${branchName}"`, {
stdio: 'inherit', stdio: 'inherit',
shell: true, shell: true,
......
...@@ -7,6 +7,10 @@ const semver = require('semver') ...@@ -7,6 +7,10 @@ const semver = require('semver')
const { Sema } = require('async-sema') const { Sema } = require('async-sema')
const { execSync } = require('child_process') const { execSync } = require('child_process')
const fs = require('fs') const fs = require('fs')
const {
getGitHubToken,
getGitHubTokenMissingMessage,
} = require('./release-github-auth')
const cwd = process.cwd() const cwd = process.cwd()
...@@ -126,10 +130,10 @@ const cwd = process.cwd() ...@@ -126,10 +130,10 @@ const cwd = process.cwd()
} }
const undraft = async () => { const undraft = async () => {
const githubToken = process.env.RELEASE_BOT_GITHUB_TOKEN const githubToken = getGitHubToken()
if (!githubToken) { if (!githubToken) {
throw new Error(`Missing RELEASE_BOT_GITHUB_TOKEN`) throw new Error(getGitHubTokenMissingMessage())
} }
if (isCanary) { if (isCanary) {
......
// @ts-check
const execa = require('execa')
const REPO_URL = 'github.com/vercel/next.js.git'
function getGitHubToken() {
return (
process.env.RELEASE_GITHUB_TOKEN || process.env.RELEASE_BOT_GITHUB_TOKEN
)
}
function getGitHubTokenMissingMessage() {
return 'Missing RELEASE_GITHUB_TOKEN or RELEASE_BOT_GITHUB_TOKEN'
}
function getGitUser() {
const appSlug = process.env.RELEASE_GITHUB_APP_SLUG
const appUserId = process.env.RELEASE_GITHUB_APP_USER_ID
if (appSlug && appUserId) {
return {
name: `${appSlug}[bot]`,
email: `${appUserId}+${appSlug}[bot]@users.noreply.github.com`,
}
}
return {
name: process.env.RELEASE_GITHUB_USER_NAME || 'nextjs-bot',
email: process.env.RELEASE_GITHUB_USER_EMAIL || 'it+nextjs-bot@vercel.com',
}
}
async function configureGitHubAuth(token) {
const gitUser = getGitUser()
const remoteUrl = `https://x-access-token:${encodeURIComponent(
token
)}@${REPO_URL}`
await execa('git', ['remote', 'set-url', 'origin', remoteUrl], {
stdio: 'inherit',
})
await execa('git', ['config', 'user.name', gitUser.name], {
stdio: 'inherit',
})
await execa('git', ['config', 'user.email', gitUser.email], {
stdio: 'inherit',
})
}
async function verifyGitHubApiAccess(token, path, label) {
const response = await fetch(`https://api.github.com${path}`, {
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${token}`,
'X-GitHub-Api-Version': '2022-11-28',
},
})
if (!response.ok) {
throw new Error(
`Failed to verify GitHub API access for ${label} (${response.status}): ${await response.text()}`
)
}
console.log(`Verified GitHub API access for ${label}`)
}
module.exports = {
configureGitHubAuth,
getGitHubToken,
getGitHubTokenMissingMessage,
verifyGitHubApiAccess,
}
...@@ -2,6 +2,12 @@ ...@@ -2,6 +2,12 @@
const path = require('path') const path = require('path')
const execa = require('execa') const execa = require('execa')
const resolveFrom = require('resolve-from') const resolveFrom = require('resolve-from')
const {
configureGitHubAuth,
getGitHubToken,
getGitHubTokenMissingMessage,
verifyGitHubApiAccess,
} = require('./release-github-auth')
const SEMVER_TYPES = ['patch', 'minor', 'major'] const SEMVER_TYPES = ['patch', 'minor', 'major']
...@@ -33,10 +39,10 @@ async function main() { ...@@ -33,10 +39,10 @@ async function main() {
return return
} }
const githubToken = process.env.RELEASE_BOT_GITHUB_TOKEN const githubToken = getGitHubToken()
if (!githubToken) { if (!githubToken) {
console.log(`Missing RELEASE_BOT_GITHUB_TOKEN`) console.log(getGitHubTokenMissingMessage())
return return
} }
...@@ -49,18 +55,12 @@ async function main() { ...@@ -49,18 +55,12 @@ async function main() {
const config = new ConfigStore('release') const config = new ConfigStore('release')
config.set('token', githubToken) config.set('token', githubToken)
await execa( await configureGitHubAuth(githubToken)
`git remote set-url origin https://nextjs-bot:${githubToken}@github.com/vercel/next.js.git`, await verifyGitHubApiAccess(
{ stdio: 'inherit', shell: true } githubToken,
'/repos/vercel/next.js/releases?per_page=1',
'release lookup'
) )
await execa(`git config user.name "nextjs-bot"`, {
stdio: 'inherit',
shell: true,
})
await execa(`git config user.email "it+nextjs-bot@vercel.com"`, {
stdio: 'inherit',
shell: true,
})
console.log(`Running pnpm release-${isCanary ? 'canary' : 'stable'}...`) console.log(`Running pnpm release-${isCanary ? 'canary' : 'stable'}...`)
const preleaseType = const preleaseType =
...@@ -70,19 +70,22 @@ async function main() { ...@@ -70,19 +70,22 @@ async function main() {
? 'preminor' ? 'preminor'
: 'prerelease' : 'prerelease'
const child = execa( let command = isCanary
isCanary ? `pnpm lerna version ${preleaseType} --preid canary --force-publish -y`
? `pnpm lerna version ${preleaseType} --preid canary --force-publish -y && pnpm release --pre --skip-questions --show-url` : isReleaseCandidate
: isReleaseCandidate ? `pnpm lerna version ${preleaseType} --preid rc --force-publish -y`
? `pnpm lerna version ${preleaseType} --preid rc --force-publish -y && pnpm release --pre --skip-questions --show-url` : isBeta
: isBeta ? `pnpm lerna version ${preleaseType} --preid beta --force-publish -y`
? `pnpm lerna version ${preleaseType} --preid beta --force-publish -y && pnpm release --pre --skip-questions --show-url` : `pnpm lerna version ${semverType} --force-publish -y`
: `pnpm lerna version ${semverType} --force-publish -y`,
{ if (isCanary || isReleaseCandidate || isBeta) {
stdio: 'pipe', command += ' && pnpm release --pre --skip-questions --show-url'
shell: true, }
}
) const child = execa(command, {
stdio: 'pipe',
shell: true,
})
child.stdout?.pipe(process.stdout) child.stdout?.pipe(process.stdout)
child.stderr?.pipe(process.stderr) child.stderr?.pipe(process.stderr)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment