Skip to content

Commit 224194b

Browse files
author
Peter Bengtsson
authored
Migrate from actionsgithub script to own scripts staging deploy (#23184)
* migrate from actions/github-script to own scripts (staging deploy) * make it executable * remove need to --no-save install 'esm' * don't use self-hosted runner so we get latest OS (2.285.0) * remove pointless try/catch
1 parent 9067d07 commit 224194b

2 files changed

Lines changed: 72 additions & 67 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 deployToStaging from '../../script/deployment/deploy-to-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 { PR_URL, SOURCE_BLOB_URL, CONTEXT_NAME, ACTIONS_RUN_LOG, HEAD_SHA } = process.env
25+
if (!PR_URL) {
26+
throw new Error('$PR_URL not set')
27+
}
28+
if (!SOURCE_BLOB_URL) {
29+
throw new Error('$SOURCE_BLOB_URL not set')
30+
}
31+
if (!CONTEXT_NAME) {
32+
throw new Error('$CONTEXT_NAME not set')
33+
}
34+
if (!ACTIONS_RUN_LOG) {
35+
throw new Error('$ACTIONS_RUN_LOG not set')
36+
}
37+
if (!HEAD_SHA) {
38+
throw new Error('$HEAD_SHA not set')
39+
}
40+
41+
const { owner, repo, pullNumber } = parsePrUrl(PR_URL)
42+
if (!owner || !repo || !pullNumber) {
43+
throw new Error(
44+
`'pullRequestUrl' input must match URL format 'https://github.com/github/(docs|docs-internal)/pull/123' but was '${PR_URL}'`
45+
)
46+
}
47+
48+
const { data: pullRequest } = await octokit.pulls.get({
49+
owner,
50+
repo,
51+
pull_number: pullNumber,
52+
})
53+
54+
await deployToStaging({
55+
octokit,
56+
pullRequest,
57+
forceRebuild: false,
58+
// These parameters will ONLY be set by Actions
59+
sourceBlobUrl: SOURCE_BLOB_URL,
60+
runId: context.runId,
61+
})
62+
63+
await github.repos.createCommitStatus({
64+
owner,
65+
repo,
66+
sha: HEAD_SHA,
67+
context: CONTEXT_NAME,
68+
state: 'success',
69+
description: 'Successfully deployed! See logs.',
70+
target_url: ACTIONS_RUN_LOG,
71+
})

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

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,8 @@ jobs:
486486
- name: Install dependencies
487487
run: npm ci
488488

489-
- name: Install one-off development-only dependencies
490-
run: npm install --no-save --include=optional esm
491-
492489
- name: Deploy
493490
id: deploy
494-
uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d
495491
env:
496492
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
497493
HEROKU_API_TOKEN: ${{ secrets.HEROKU_API_TOKEN }}
@@ -503,69 +499,7 @@ jobs:
503499
ACTIONS_RUN_LOG: ${{ env.ACTIONS_RUN_LOG }}
504500
HEAD_SHA: ${{ needs.pr-metadata.outputs.head_sha }}
505501
ALLOWED_POLLING_FAILURES_PER_PHASE: '15'
506-
with:
507-
script: |
508-
const { GITHUB_TOKEN, HEROKU_API_TOKEN } = process.env
509-
510-
// Exit if GitHub Actions PAT is not found
511-
if (!GITHUB_TOKEN) {
512-
throw new Error('You must supply a GITHUB_TOKEN environment variable!')
513-
}
514-
515-
// Exit if Heroku API token is not found
516-
if (!HEROKU_API_TOKEN) {
517-
throw new Error('You must supply a HEROKU_API_TOKEN environment variable!')
518-
}
519-
520-
// Workaround to allow us to load ESM files with `require(...)`
521-
const esm = require('esm')
522-
require = esm({})
523-
524-
const { default: parsePrUrl } = require('./script/deployment/parse-pr-url')
525-
const { default: getOctokit } = require('./script/helpers/github')
526-
const { default: deployToStaging } = require('./script/deployment/deploy-to-staging')
527-
528-
// This helper uses the `GITHUB_TOKEN` implicitly!
529-
// We're using our usual version of Octokit vs. the provided `github`
530-
// instance to avoid versioning discrepancies.
531-
const octokit = getOctokit()
532-
533-
try {
534-
const { PR_URL, SOURCE_BLOB_URL, CONTEXT_NAME, ACTIONS_RUN_LOG, HEAD_SHA } = process.env
535-
const { owner, repo, pullNumber } = parsePrUrl(PR_URL)
536-
if (!owner || !repo || !pullNumber) {
537-
throw new Error(`'pullRequestUrl' input must match URL format 'https://github.com/github/(docs|docs-internal)/pull/123' but was '${PR_URL}'`)
538-
}
539-
540-
const { data: pullRequest } = await octokit.pulls.get({
541-
owner,
542-
repo,
543-
pull_number: pullNumber
544-
})
545-
546-
await deployToStaging({
547-
octokit,
548-
pullRequest,
549-
forceRebuild: false,
550-
// These parameters will ONLY be set by Actions
551-
sourceBlobUrl: SOURCE_BLOB_URL,
552-
runId: context.runId
553-
})
554-
555-
await github.repos.createCommitStatus({
556-
owner,
557-
repo,
558-
sha: HEAD_SHA,
559-
context: CONTEXT_NAME,
560-
state: 'success',
561-
description: 'Successfully deployed! See logs.',
562-
target_url: ACTIONS_RUN_LOG
563-
})
564-
} catch (error) {
565-
console.error(`Failed to deploy to staging: ${error.message}`)
566-
console.error(error)
567-
throw error
568-
}
502+
run: .github/actions-scripts/staging-deploy.js
569503

570504
- name: Mark the deployment as inactive if timed out
571505
uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d

0 commit comments

Comments
 (0)