This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
forked from amondnet/now-deployment
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
204 lines (178 loc) · 6.42 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
const core = require("@actions/core")
const github = require("@actions/github")
const exec = require("@actions/exec")
const vercelToken = core.getInput("vercelToken")
const vercelOrgId = core.getInput("vercelOrgId")
const vercelProjectId = core.getInput("vercelProjectId")
const buildOption = core.getInput("buildOption") === "true"
const buildSource = core.getInput("buildSource")
const buildOutput = core.getInput("buildOutput")
const buildDomain = core.getInput("buildDomain")
const githubToken = core.getInput("githubToken")
let octokit = new github.GitHub(githubToken)
async function run() {
core.info("--- start ---")
let ref
let sha
let commit
if (github.context.eventName === "push") {
core.info("Retriving push metadata")
ref = github.context.ref
sha = github.context.sha
commit = await exec.exec("git log -1 --pretty=format:%B")
commit = commit.toString().trim()
} else if (github.context.eventName === "pull_request") {
core.info("Retriving pull request metadata")
const pullRequestPayload = github.context.payload
ref = pullRequestPayload.pull_request.head.ref
sha = pullRequestPayload.pull_request.head.sha
const { data: commitData } = await octokit.git.getCommit({
...github.context.repo,
commit_sha: sha,
})
commit = commitData.message
}
if (buildOption) {
await buildStatic()
}
await setVercelEnv()
const deploymentUrl = await vercelDeploy(ref, commit)
if (buildDomain) {
await assignDomain(deploymentUrl)
}
if (github.context.issue.number) {
core.info("this is related issue or pull_request ")
await createCommentOnPullRequest(sha, deploymentUrl)
} else if (github.context.eventName === "push") {
core.info("this is push event")
await createCommentOnCommit(sha, deploymentUrl)
}
core.info("---- end ----")
}
async function buildStatic() {
core.info("[Build starts]")
let myOutput = ""
let myError = ""
const options = {}
options.listeners = {
stdout: (data) => {
myOutput += data.toString()
core.info(data.toString())
},
stderr: (data) => {
myError += data.toString()
core.info(data.toString())
},
}
options.cwd = "./" + buildSource
core.info("Build source is at : " + options.cwd)
await exec.exec("npx", ["yarn"], options)
await exec.exec("npx", ["yarn", "build"], options)
core.info("[Build ends]")
return
}
async function setVercelEnv() {
core.info("[Set env starts]")
if (vercelOrgId) {
core.exportVariable("VERCEL_ORG_ID", vercelOrgId)
}
if (vercelProjectId) {
core.exportVariable("VERCEL_PROJECT_ID", vercelProjectId)
}
core.info("[Set env ends]")
}
async function vercelDeploy(ref, commit) {
core.info("[Deploy starts]")
let myOutput = ""
let myError = ""
const options = {}
options.listeners = {
stdout: (data) => {
myOutput += data.toString()
core.info(data.toString())
},
stderr: (data) => {
myError += data.toString()
core.info(data.toString())
},
}
options.cwd = "./" + buildOutput
core.info("Build output is at : " + options.cwd)
await exec.exec(
"npx",
[
"vercel",
"--token",
vercelToken,
"-m",
`githubCommitSha=${github.context.sha}`,
"-m",
`githubCommitAuthorName=${github.context.actor}`,
"-m",
`githubCommitAuthorLogin=${github.context.actor}`,
"-m",
"githubDeployment=1",
"-m",
`githubOrg=${github.context.repo.owner}`,
"-m",
`githubRepo=${github.context.repo.repo}`,
"-m",
`githubCommitOrg=${github.context.repo.owner}`,
"-m",
`githubCommitRepo=${github.context.repo.repo}`,
"-m",
`githubCommitMessage=${commit}`,
"-m",
`githubCommitRef=${ref}`,
],
options
)
core.info("[Deploy ends]")
return myOutput
}
async function assignDomain(deploymentUrl) {
core.info("[Assign domain starts]")
let myOutput = ""
let myError = ""
const options = {}
options.listeners = {
stdout: (data) => {
myOutput += data.toString()
core.info(data.toString())
},
stderr: (data) => {
myError += data.toString()
core.info(data.toString())
},
}
try {
await exec.exec(
"npx",
["vercel", "alias", deploymentUrl, buildDomain],
options
)
} catch (error) {
core.warning("Assigning domain failed with error : " + error)
}
core.info("[Assign domain ends]")
return
}
async function createCommentOnCommit(deploymentCommit, deploymentUrl) {
const body = `![Vercel](https://raw.githubusercontent.com/xmflsct/action-vercel-deployment/master/src/vercel.svg "Vercel")\r\n<img align="left" width="24" height="24" src="https://raw.githubusercontent.com/xmflsct/action-vercel-deployment/master/src/info.svg"> This commit ${deploymentCommit} is built and deployed to [Vercel](https://vercel.com/).\r\n<img align="left" width="24" height="24" src="https://raw.githubusercontent.com/xmflsct/action-vercel-deployment/master/src/check-in-circle.svg"> Preview: ${deploymentUrl}\r\n\r\n<img align="left" width="24" height="24" src="https://raw.githubusercontent.com/xmflsct/action-vercel-deployment/master/src/award.svg"> This commit has been automatically deployed with [vercel-deployment](https://github.com/xmflsct/action-vercel-deployment)`
await octokit.repos.createCommitComment({
...github.context.repo,
commit_sha: github.context.sha,
body: body,
})
}
async function createCommentOnPullRequest(deploymentCommit, deploymentUrl) {
const body = `![Vercel](https://raw.githubusercontent.com/xmflsct/action-vercel-deployment/master/src/vercel.svg "Vercel")\r\n<img align="left" width="24" height="24" src="https://raw.githubusercontent.com/xmflsct/action-vercel-deployment/master/src/info.svg"> This commit ${deploymentCommit} is built and deployed to [Vercel](https://vercel.com/).\r\n<img align="left" width="24" height="24" src="https://raw.githubusercontent.com/xmflsct/action-vercel-deployment/master/src/check-in-circle.svg"> Preview: ${deploymentUrl}\r\n\r\n<img align="left" width="24" height="24" src="https://raw.githubusercontent.com/xmflsct/action-vercel-deployment/master/src/award.svg"> This pull request has been automatically deployed with [vercel-deployment](https://github.com/xmflsct/action-vercel-deployment)`
await octokit.issues.createComment({
...github.context.repo,
issue_number: github.context.issue.number,
body: body,
})
}
run().catch((error) => {
core.setFailed(error.message)
})