generated from BetaHuhn/github-actions-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
index.js
205 lines (163 loc) · 5.99 KB
/
index.js
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const core = require('@actions/core')
const Github = require('./github')
const Vercel = require('./vercel')
const { addSchema } = require('./helpers')
const crypto = require('crypto')
const {
GITHUB_DEPLOYMENT,
USER,
REPOSITORY,
BRANCH,
PR_NUMBER,
SHA,
IS_PR,
PR_LABELS,
CREATE_COMMENT,
DELETE_EXISTING_COMMENT,
PR_PREVIEW_DOMAIN,
ALIAS_DOMAINS,
ATTACH_COMMIT_METADATA,
LOG_URL,
DEPLOY_PR_FROM_FORK,
IS_FORK,
ACTOR
} = require('./config')
// Following https://perishablepress.com/stop-using-unsafe-characters-in-urls/ only allow characters that won't break the URL.
const urlSafeParameter = (input) => input.replace(/[^a-z0-9_~]/gi, '-')
const run = async () => {
const github = Github.init()
// Refuse to deploy an untrusted fork
if (IS_FORK === true && DEPLOY_PR_FROM_FORK === false) {
core.warning(`PR is from fork and DEPLOY_PR_FROM_FORK is set to false`)
const body = `
Refusing to deploy this Pull Request to Vercel because it originates from @${ ACTOR }'s fork.
**@${ USER }** To allow this behaviour set \`DEPLOY_PR_FROM_FORK\` to true ([more info](https://github.com/BetaHuhn/deploy-to-vercel-action#deploying-a-pr-made-from-a-fork-or-dependabot)).
`
const comment = await github.createComment(body)
core.info(`Comment created: ${ comment.html_url }`)
core.setOutput('DEPLOYMENT_CREATED', false)
core.setOutput('COMMENT_CREATED', true)
core.info('Done')
return
}
if (GITHUB_DEPLOYMENT) {
core.info('Creating GitHub deployment')
const ghDeployment = await github.createDeployment()
core.info(`Deployment #${ ghDeployment.id } created`)
await github.updateDeployment('pending')
core.info(`Deployment #${ ghDeployment.id } status changed to "pending"`)
}
try {
core.info(`Creating deployment with Vercel CLI`)
const vercel = Vercel.init()
const commit = ATTACH_COMMIT_METADATA ? await github.getCommit() : undefined
const deploymentUrl = await vercel.deploy(commit)
core.info('Successfully deployed to Vercel!')
const deploymentUrls = []
if (IS_PR && PR_PREVIEW_DOMAIN) {
core.info(`Assigning custom preview domain to PR`)
if (typeof PR_PREVIEW_DOMAIN !== 'string') {
throw new Error(`invalid type for PR_PREVIEW_DOMAIN`)
}
const alias = PR_PREVIEW_DOMAIN.replace('{USER}', urlSafeParameter(USER))
.replace('{REPO}', urlSafeParameter(REPOSITORY))
.replace('{BRANCH}', urlSafeParameter(BRANCH))
.replace('{PR}', PR_NUMBER)
.replace('{SHA}', SHA.substring(0, 7))
.toLowerCase()
const previewDomainSuffix = '.vercel.app'
let nextAlias = alias
if (alias.endsWith(previewDomainSuffix)) {
let prefix = alias.substring(0, alias.indexOf(previewDomainSuffix))
if (prefix.length >= 60) {
core.warning(`The alias ${ prefix } exceeds 60 chars in length, truncating using vercel's rules. See https://vercel.com/docs/concepts/deployments/automatic-urls#automatic-branch-urls`)
prefix = prefix.substring(0, 55)
const uniqueSuffix = crypto.createHash('sha256')
.update(`git-${ BRANCH }-${ REPOSITORY }`)
.digest('hex')
.slice(0, 6)
nextAlias = `${ prefix }-${ uniqueSuffix }${ previewDomainSuffix }`
core.info(`Updated domain alias: ${ nextAlias }`)
}
}
await vercel.assignAlias(nextAlias)
deploymentUrls.push(addSchema(nextAlias))
}
if (!IS_PR && ALIAS_DOMAINS) {
core.info(`Assigning custom domains to Vercel deployment`)
if (!Array.isArray(ALIAS_DOMAINS)) {
throw new Error(`invalid type for PR_PREVIEW_DOMAIN`)
}
for (let i = 0; i < ALIAS_DOMAINS.length; i++) {
const alias = ALIAS_DOMAINS[i]
.replace('{USER}', urlSafeParameter(USER))
.replace('{REPO}', urlSafeParameter(REPOSITORY))
.replace('{BRANCH}', urlSafeParameter(BRANCH))
.replace('{SHA}', SHA.substring(0, 7))
.toLowerCase()
await vercel.assignAlias(alias)
deploymentUrls.push(addSchema(alias))
}
}
deploymentUrls.push(addSchema(deploymentUrl))
const previewUrl = deploymentUrls[0]
const deployment = await vercel.getDeployment()
core.info(`Deployment "${ deployment.id }" available at: ${ deploymentUrls.join(', ') }`)
if (GITHUB_DEPLOYMENT) {
core.info('Changing GitHub deployment status to "success"')
await github.updateDeployment('success', previewUrl)
}
if (IS_PR) {
if (DELETE_EXISTING_COMMENT) {
core.info('Checking for existing comment on PR')
const deletedCommentId = await github.deleteExistingComment()
if (deletedCommentId) core.info(`Deleted existing comment #${ deletedCommentId }`)
}
if (CREATE_COMMENT) {
core.info('Creating new comment on PR')
const body = `
This pull request has been deployed to Vercel.
<table>
<tr>
<td><strong>Latest commit:</strong></td>
<td><code>${ SHA.substring(0, 7) }</code></td>
</tr>
<tr>
<td><strong>✅ Preview:</strong></td>
<td><a href='${ previewUrl }'>${ previewUrl }</a></td>
</tr>
<tr>
<td><strong>🔍 Inspect:</strong></td>
<td><a href='${ deployment.inspectorUrl }'>${ deployment.inspectorUrl }</a></td>
</tr>
</table>
[View Workflow Logs](${ LOG_URL })
`
const comment = await github.createComment(body)
core.info(`Comment created: ${ comment.html_url }`)
}
if (PR_LABELS) {
core.info('Adding label(s) to PR')
const labels = await github.addLabel()
core.info(`Label(s) "${ labels.map((label) => label.name).join(', ') }" added`)
}
}
core.setOutput('PREVIEW_URL', previewUrl)
core.setOutput('DEPLOYMENT_URLS', deploymentUrls)
core.setOutput('DEPLOYMENT_UNIQUE_URL', deploymentUrls[deploymentUrls.length - 1])
core.setOutput('DEPLOYMENT_ID', deployment.id)
core.setOutput('DEPLOYMENT_INSPECTOR_URL', deployment.inspectorUrl)
core.setOutput('DEPLOYMENT_CREATED', true)
core.setOutput('COMMENT_CREATED', IS_PR && CREATE_COMMENT)
core.info('Done')
} catch (err) {
await github.updateDeployment('failure')
core.setFailed(err.message)
}
}
run()
.then(() => {})
.catch((err) => {
core.error('ERROR')
core.setFailed(err.message)
})