diff --git a/.github/actions-scripts/staging-undeploy.js b/.github/actions-scripts/staging-undeploy.js new file mode 100755 index 0000000000..5704008da8 --- /dev/null +++ b/.github/actions-scripts/staging-undeploy.js @@ -0,0 +1,50 @@ +#!/usr/bin/env node + +import parsePrUrl from '../../script/deployment/parse-pr-url.js' +import getOctokit from '../../script/helpers/github.js' +import undeployFromStaging from '../../script/deployment/undeploy-from-staging.js' + +const { GITHUB_TOKEN, HEROKU_API_TOKEN } = process.env + +// Exit if GitHub Actions PAT is not found +if (!GITHUB_TOKEN) { + throw new Error('You must supply a GITHUB_TOKEN environment variable!') +} + +// Exit if Heroku API token is not found +if (!HEROKU_API_TOKEN) { + throw new Error('You must supply a HEROKU_API_TOKEN environment variable!') +} + +// This helper uses the `GITHUB_TOKEN` implicitly! +// We're using our usual version of Octokit vs. the provided `github` +// instance to avoid versioning discrepancies. +const octokit = getOctokit() + +const { RUN_ID, PR_URL } = process.env + +if (!RUN_ID) { + throw new Error('$RUN_ID not set') +} +if (!PR_URL) { + throw new Error('$PR_URL not set') +} + +const { owner, repo, pullNumber } = parsePrUrl(PR_URL) +if (!owner || !repo || !pullNumber) { + throw new Error( + `'pullRequestUrl' input must match URL format 'https://github.com/github/(docs|docs-internal)/pull/123' but was '${PR_URL}'` + ) +} + +const { data: pullRequest } = await octokit.pulls.get({ + owner, + repo, + pull_number: pullNumber, +}) + +await undeployFromStaging({ + octokit, + pullRequest: pullRequest, + runId: RUN_ID, +}) diff --git a/.github/workflows/staging-undeploy-pr.yml b/.github/workflows/staging-undeploy-pr.yml index d0c3229aa4..bb5bfa2783 100644 --- a/.github/workflows/staging-undeploy-pr.yml +++ b/.github/workflows/staging-undeploy-pr.yml @@ -69,51 +69,13 @@ jobs: - name: Install dependencies run: npm ci - - name: Install one-off development-only dependencies - run: npm install --no-save --include=optional esm - - name: Undeploy - uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} HEROKU_API_TOKEN: ${{ secrets.HEROKU_API_TOKEN }} - with: - script: | - const { GITHUB_TOKEN, HEROKU_API_TOKEN } = process.env - - // Exit if GitHub Actions PAT is not found - if (!GITHUB_TOKEN) { - throw new Error('You must supply a GITHUB_TOKEN environment variable!') - } - - // Exit if Heroku API token is not found - if (!HEROKU_API_TOKEN) { - throw new Error('You must supply a HEROKU_API_TOKEN environment variable!') - } - - // Workaround to allow us to load ESM files with `require(...)` - const esm = require('esm') - require = esm({}) - - const { default: getOctokit } = require('./script/helpers/github') - const { default: undeployFromStaging } = require('./script/deployment/undeploy-from-staging') - - // This helper uses the `GITHUB_TOKEN` implicitly! - // We're using our usual version of Octokit vs. the provided `github` - // instance to avoid versioning discrepancies. - const octokit = getOctokit() - - try { - await undeployFromStaging({ - octokit, - pullRequest: context.payload.pull_request, - runId: context.runId - }) - } catch (error) { - console.error(`Failed to undeploy from staging: ${error.message}`) - console.error(error) - throw error - } + RUN_ID: ${{ github.run_id }} + PR_URL: ${{ github.event.pull_request.html_url }} + run: .github/actions-scripts/staging-undeploy.js - if: ${{ always() }} name: Remove the label from the PR to unblock deployment