Skip to content

Commit 23ca545

Browse files
authored
Move from github-script to standalone script for staging undeploy (#23190)
* Move from github-script to standalone script for staging undeploy * Add necessary env variables
1 parent 1d5a786 commit 23ca545

2 files changed

Lines changed: 53 additions & 41 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env node
2+
3+
import parsePrUrl from '../../script/deployment/parse-pr-url.js'
4+
import getOctokit from '../../script/helpers/github.js'
5+
import undeployFromStaging from '../../script/deployment/undeploy-from-staging.js'
6+
7+
const { GITHUB_TOKEN, HEROKU_API_TOKEN } = process.env
8+
9+
// Exit if GitHub Actions PAT is not found
10+
if (!GITHUB_TOKEN) {
11+
throw new Error('You must supply a GITHUB_TOKEN environment variable!')
12+
}
13+
14+
// Exit if Heroku API token is not found
15+
if (!HEROKU_API_TOKEN) {
16+
throw new Error('You must supply a HEROKU_API_TOKEN environment variable!')
17+
}
18+
19+
// This helper uses the `GITHUB_TOKEN` implicitly!
20+
// We're using our usual version of Octokit vs. the provided `github`
21+
// instance to avoid versioning discrepancies.
22+
const octokit = getOctokit()
23+
24+
const { RUN_ID, PR_URL } = process.env
25+
26+
if (!RUN_ID) {
27+
throw new Error('$RUN_ID not set')
28+
}
29+
if (!PR_URL) {
30+
throw new Error('$PR_URL not set')
31+
}
32+
33+
const { owner, repo, pullNumber } = parsePrUrl(PR_URL)
34+
if (!owner || !repo || !pullNumber) {
35+
throw new Error(
36+
`'pullRequestUrl' input must match URL format 'https://github.com/github/(docs|docs-internal)/pull/123' but was '${PR_URL}'`
37+
)
38+
}
39+
40+
const { data: pullRequest } = await octokit.pulls.get({
41+
owner,
42+
repo,
43+
pull_number: pullNumber,
44+
})
45+
46+
await undeployFromStaging({
47+
octokit,
48+
pullRequest: pullRequest,
49+
runId: RUN_ID,
50+
})

.github/workflows/staging-undeploy-pr.yml

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -69,51 +69,13 @@ jobs:
6969
- name: Install dependencies
7070
run: npm ci
7171

72-
- name: Install one-off development-only dependencies
73-
run: npm install --no-save --include=optional esm
74-
7572
- name: Undeploy
76-
uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d
7773
env:
7874
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7975
HEROKU_API_TOKEN: ${{ secrets.HEROKU_API_TOKEN }}
80-
with:
81-
script: |
82-
const { GITHUB_TOKEN, HEROKU_API_TOKEN } = process.env
83-
84-
// Exit if GitHub Actions PAT is not found
85-
if (!GITHUB_TOKEN) {
86-
throw new Error('You must supply a GITHUB_TOKEN environment variable!')
87-
}
88-
89-
// Exit if Heroku API token is not found
90-
if (!HEROKU_API_TOKEN) {
91-
throw new Error('You must supply a HEROKU_API_TOKEN environment variable!')
92-
}
93-
94-
// Workaround to allow us to load ESM files with `require(...)`
95-
const esm = require('esm')
96-
require = esm({})
97-
98-
const { default: getOctokit } = require('./script/helpers/github')
99-
const { default: undeployFromStaging } = require('./script/deployment/undeploy-from-staging')
100-
101-
// This helper uses the `GITHUB_TOKEN` implicitly!
102-
// We're using our usual version of Octokit vs. the provided `github`
103-
// instance to avoid versioning discrepancies.
104-
const octokit = getOctokit()
105-
106-
try {
107-
await undeployFromStaging({
108-
octokit,
109-
pullRequest: context.payload.pull_request,
110-
runId: context.runId
111-
})
112-
} catch (error) {
113-
console.error(`Failed to undeploy from staging: ${error.message}`)
114-
console.error(error)
115-
throw error
116-
}
76+
RUN_ID: ${{ github.run_id }}
77+
PR_URL: ${{ github.event.pull_request.html_url }}
78+
run: .github/actions-scripts/staging-undeploy.js
11779

11880
- if: ${{ always() }}
11981
name: Remove the label from the PR to unblock deployment

0 commit comments

Comments
 (0)