-
Notifications
You must be signed in to change notification settings - Fork 59.8k
175 lines (150 loc) · 6.26 KB
/
staging-deploy-pr.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
name: Staging - Deploy PR
# **What it does**: To deploy PRs to a Heroku staging environment.
# **Why we have it**: To deploy with high visibility in case of failures.
# **Who does it impact**: All contributors.
on:
pull_request:
types:
- opened
- reopened
- synchronize
- unlocked
workflow_dispatch:
inputs:
pullRequestUrl:
description: 'Pull Request URL'
required: true
default: 'https://github.com/github/docs/pull/1234'
forceRebuild:
description: 'Force the Heroku App to be rebuilt from scratch? (true/false)'
required: false
default: 'false'
jobs:
validate-inputs:
if: ${{ github.repository == 'github/docs-internal' || github.repository == 'github/docs' }}
name: Validate inputs
runs-on: ubuntu-latest
timeout-minutes: 2
outputs:
headRef: ${{ steps.validate.outputs.headRef }}
steps:
- if: ${{ github.event_name == 'workflow_dispatch' }}
name: Check out repo
uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
with:
# Enables cloning the Early Access repo later with the relevant PAT
persist-credentials: 'false'
- if: ${{ github.event_name == 'workflow_dispatch' }}
name: Setup node
uses: actions/setup-node@38d90ce44d5275ad62cc48384b3d8a58c500bb5f
with:
node-version: 16.x
cache: npm
- if: ${{ github.event_name == 'workflow_dispatch' }}
name: Install dependencies
run: npm ci
- if: ${{ github.event_name == 'workflow_dispatch' }}
name: Validate and get head.ref
id: validate
uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d
env:
PR_URL: ${{ github.event.inputs.pullRequestUrl }}
FORCE_REBUILD: ${{ github.event.inputs.forceRebuild }}
with:
script: |
const parsePrUrl = require('./script/deployment/parse-pr-url')
// Manually resolve workflow_dispatch inputs
const { PR_URL, FORCE_REBUILD } = process.env
if (!['true', 'false'].includes(FORCE_REBUILD)) {
throw new Error(`'forceRebuild' input must be either 'true' or 'false' but was '${FORCE_REBUILD}'`)
}
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 github.pulls.get({
owner,
repo,
pull_number: pullNumber
})
core.setOutput('headRef', pullRequest.head.ref)
deploy:
if: ${{ github.repository == 'github/docs-internal' || github.repository == 'github/docs' }}
needs: validate-inputs
name: Deploy
runs-on: ubuntu-latest
timeout-minutes: 10
concurrency:
group: staging_${{ needs.validate-inputs.outputs.headRef || github.head_ref }}
cancel-in-progress: true
steps:
- name: Check out repo
uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
with:
# Enables cloning the Early Access repo later with the relevant PAT
persist-credentials: 'false'
- name: Setup node
uses: actions/setup-node@38d90ce44d5275ad62cc48384b3d8a58c500bb5f
with:
node-version: 16.x
cache: npm
- name: Install dependencies
run: npm ci
- name: Deploy
uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HEROKU_API_TOKEN: ${{ secrets.HEROKU_API_TOKEN }}
DOCUBOT_REPO_PAT: ${{ secrets.DOCUBOT_REPO_PAT }}
HYDRO_ENDPOINT: ${{ secrets.HYDRO_ENDPOINT }}
HYDRO_SECRET: ${{ secrets.HYDRO_SECRET }}
PR_URL: ${{ github.event.inputs.pullRequestUrl }}
FORCE_REBUILD: ${{ github.event.inputs.forceRebuild }}
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!')
}
const parsePrUrl = require('./script/deployment/parse-pr-url')
const getOctokit = require('./script/helpers/github')
const deployToStaging = require('./script/deployment/deploy-to-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 {
let pullRequest = null
let forceRebuild = false
// Manually resolve workflow_dispatch inputs
if (context.eventName === 'workflow_dispatch') {
const { PR_URL, FORCE_REBUILD } = process.env
forceRebuild = FORCE_REBUILD === 'true'
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: pr } = await octokit.pulls.get({
owner,
repo,
pull_number: pullNumber
})
pullRequest = pr
}
await deployToStaging({
herokuToken: HEROKU_API_TOKEN,
octokit,
pullRequest: pullRequest || context.payload.pull_request,
forceRebuild,
runId: context.runId
})
} catch (error) {
console.error(`Failed to deploy to staging: ${error.message}`)
console.error(error)
throw error
}