generated from BetaHuhn/github-actions-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
vercel.js
119 lines (95 loc) · 2.59 KB
/
vercel.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
const core = require('@actions/core')
const got = require('got')
const { exec, removeSchema } = require('./helpers')
const {
VERCEL_TOKEN,
PRODUCTION,
VERCEL_SCOPE,
VERCEL_ORG_ID,
VERCEL_PROJECT_ID,
SHA,
USER,
REPOSITORY,
REF,
TRIM_COMMIT_MESSAGE,
BUILD_ENV,
PREBUILT,
WORKING_DIRECTORY,
FORCE
} = require('./config')
const init = () => {
core.info('Setting environment variables for Vercel CLI')
core.exportVariable('VERCEL_ORG_ID', VERCEL_ORG_ID)
core.exportVariable('VERCEL_PROJECT_ID', VERCEL_PROJECT_ID)
let deploymentUrl
const deploy = async (commit) => {
let commandArguments = [ `--token=${ VERCEL_TOKEN }` ]
if (VERCEL_SCOPE) {
commandArguments.push(`--scope=${ VERCEL_SCOPE }`)
}
if (PRODUCTION) {
commandArguments.push('--prod')
}
if (PREBUILT) {
commandArguments.push('--prebuilt')
}
if (FORCE) {
commandArguments.push('--force')
}
if (commit) {
const metadata = [
`githubCommitAuthorName=${ commit.authorName }`,
`githubCommitAuthorLogin=${ commit.authorLogin }`,
`githubCommitMessage=${ TRIM_COMMIT_MESSAGE ? commit.commitMessage.split(/\r?\n/)[0] : commit.commitMessage }`,
`githubCommitOrg=${ USER }`,
`githubCommitRepo=${ REPOSITORY }`,
`githubCommitRef=${ REF }`,
`githubCommitSha=${ SHA }`,
`githubOrg=${ USER }`,
`githubRepo=${ REPOSITORY }`,
`githubDeployment=1`
]
metadata.forEach((item) => {
commandArguments = commandArguments.concat([ '--meta', item ])
})
}
if (BUILD_ENV) {
BUILD_ENV.forEach((item) => {
commandArguments = commandArguments.concat([ '--build-env', item ])
})
}
core.info('Starting deploy with Vercel CLI')
const output = await exec('vercel', commandArguments, WORKING_DIRECTORY)
const parsed = output.match(/(?<=https?:\/\/)(.*)/g)[0]
if (!parsed) throw new Error('Could not parse deploymentUrl')
deploymentUrl = parsed
return deploymentUrl
}
const assignAlias = async (aliasUrl) => {
const commandArguments = [ `--token=${ VERCEL_TOKEN }`, 'alias', 'set', deploymentUrl, removeSchema(aliasUrl) ]
if (VERCEL_SCOPE) {
commandArguments.push(`--scope=${ VERCEL_SCOPE }`)
}
const output = await exec('vercel', commandArguments, WORKING_DIRECTORY)
return output
}
const getDeployment = async () => {
const url = `https://api.vercel.com/v11/now/deployments/get?url=${ deploymentUrl }`
const options = {
headers: {
Authorization: `Bearer ${ VERCEL_TOKEN }`
}
}
const res = await got(url, options).json()
return res
}
return {
deploy,
assignAlias,
deploymentUrl,
getDeployment
}
}
module.exports = {
init
}