-
-
Notifications
You must be signed in to change notification settings - Fork 363
/
git.ts
375 lines (331 loc) · 11 KB
/
git.ts
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import {info} from '@actions/core'
import {mkdirP, rmRF} from '@actions/io'
import fs from 'fs'
import {
ActionInterface,
DefaultExcludedFiles,
Status,
TestFlag
} from './constants'
import {execute} from './execute'
import {generateWorktree} from './worktree'
import {
extractErrorMessage,
isNullOrUndefined,
suppressSensitiveInformation
} from './util'
/**
* Initializes git in the workspace.
*/
export async function init(action: ActionInterface): Promise<void | Error> {
try {
info(`Deploying using ${action.tokenType}… 🔑`)
info('Configuring git…')
try {
await execute(
`git config --global --add safe.directory "${action.workspace}"`,
action.workspace,
action.silent
)
} catch {
info('Unable to set workspace as a safe directory…')
}
await execute(
`git config user.name "${action.name}"`,
action.workspace,
action.silent
)
await execute(
`git config user.email "${action.email}"`,
action.workspace,
action.silent
)
await execute(
`git config core.ignorecase false`,
action.workspace,
action.silent
)
try {
if ((process.env.CI && !action.sshKey) || action.isTest) {
/* Ensures that previously set Git configs do not interfere with the deployment.
Only runs in the GitHub Actions CI environment if a user is not using an SSH key.
*/
await execute(
`git config --local --unset-all http.https://${action.hostname}/.extraheader`,
action.workspace,
action.silent
)
}
if (action.isTest === TestFlag.UNABLE_TO_UNSET_GIT_CONFIG) {
throw new Error()
}
} catch {
info(
'Unable to unset previous git config authentication as it may not exist, continuing…'
)
}
try {
await execute(`git remote rm origin`, action.workspace, action.silent)
if (action.isTest === TestFlag.UNABLE_TO_REMOVE_ORIGIN) {
throw new Error()
}
} catch {
info('Attempted to remove origin but failed, continuing…')
}
await execute(
`git remote add origin ${action.repositoryPath}`,
action.workspace,
action.silent
)
info('Git configured… 🔧')
} catch (error) {
throw new Error(
`There was an error initializing the repository: ${suppressSensitiveInformation(
extractErrorMessage(error),
action
)} ❌`
)
}
}
/**
* Runs the necessary steps to make the deployment.
*/
export async function deploy(action: ActionInterface): Promise<Status> {
const temporaryDeploymentDirectory =
'github-pages-deploy-action-temp-deployment-folder'
const temporaryDeploymentBranch = `github-pages-deploy-action/${Math.random()
.toString(36)
.substr(2, 9)}`
info('Starting to commit changes…')
try {
const commitMessage = !isNullOrUndefined(action.commitMessage)
? (action.commitMessage as string)
: `Deploying to ${action.branch}${
process.env.GITHUB_SHA
? ` from @ ${process.env.GITHUB_REPOSITORY}@${process.env.GITHUB_SHA}`
: ''
} 🚀`
// Checks to see if the remote exists prior to deploying.
const branchExists =
action.isTest & TestFlag.HAS_REMOTE_BRANCH ||
Boolean(
(
await execute(
`git ls-remote --heads ${action.repositoryPath} refs/heads/${action.branch}`,
action.workspace,
action.silent
)
).stdout
)
await generateWorktree(action, temporaryDeploymentDirectory, branchExists)
/* Relaxes permissions of folder due to be deployed so rsync can write to/from it. */
try {
await execute(
`chmod -R +rw ${action.folderPath}`,
action.workspace,
action.silent
)
} catch {
info(`Unable to modify permissions…`)
}
// Ensures that items that need to be excluded from the clean job get parsed.
let excludes = ''
if (action.clean && action.cleanExclude) {
for (const item of action.cleanExclude) {
excludes += `--exclude ${item} `
}
}
if (action.targetFolder) {
info(`Creating target folder if it doesn't already exist… 📌`)
await mkdirP(`${temporaryDeploymentDirectory}/${action.targetFolder}`)
}
/*
Pushes all of the build files into the deployment directory.
Allows the user to specify the root if '.' is provided.
rsync is used to prevent file duplication. */
await execute(
`rsync -q -av --checksum --progress ${action.folderPath}/. ${
action.targetFolder
? `${temporaryDeploymentDirectory}/${action.targetFolder}`
: temporaryDeploymentDirectory
} ${
action.clean
? `--delete ${excludes} ${
!fs.existsSync(
`${action.folderPath}/${DefaultExcludedFiles.CNAME}`
)
? `--exclude ${DefaultExcludedFiles.CNAME}`
: ''
} ${
!fs.existsSync(
`${action.folderPath}/${DefaultExcludedFiles.NOJEKYLL}`
)
? `--exclude ${DefaultExcludedFiles.NOJEKYLL}`
: ''
}`
: ''
} --exclude ${DefaultExcludedFiles.SSH} --exclude ${
DefaultExcludedFiles.GIT
} --exclude ${DefaultExcludedFiles.GITHUB} ${
action.folderPath === action.workspace
? `--exclude ${temporaryDeploymentDirectory}`
: ''
}`,
action.workspace,
action.silent
)
if (action.singleCommit) {
await execute(
`git add --all .`,
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
}
// Use git status to check if we have something to commit.
// Special case is singleCommit with existing history, when
// we're really interested if the diff against the upstream branch
// changed.
const checkGitStatus =
branchExists && action.singleCommit
? `git diff origin/${action.branch}`
: `git status --porcelain`
info(`Checking if there are files to commit…`)
const hasFilesToCommit =
action.isTest & TestFlag.HAS_CHANGED_FILES ||
Boolean(
(
await execute(
checkGitStatus,
`${action.workspace}/${temporaryDeploymentDirectory}`,
true // This output is always silenced due to the large output it creates.
)
).stdout
)
if (
(!action.singleCommit && !hasFilesToCommit) ||
// Ignores the case where single commit is true with a target folder to prevent incorrect early exiting.
(action.singleCommit && !action.targetFolder && !hasFilesToCommit)
) {
return Status.SKIPPED
}
// Commits to GitHub.
await execute(
`git add --all .`,
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
await execute(
`git checkout -b ${temporaryDeploymentBranch}`,
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
await execute(
`git commit -m "${commitMessage}" --quiet --no-verify`,
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
if (action.dryRun) {
info(`Dry run complete`)
return Status.SUCCESS
}
if (action.force) {
// Force-push our changes, overwriting any changes that were added in
// the meantime
info(`Force-pushing changes...`)
await execute(
`git push --force ${action.repositoryPath} ${temporaryDeploymentBranch}:${action.branch}`,
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
} else {
const ATTEMPT_LIMIT = 3
// Attempt to push our changes, but fetch + rebase if there were
// other changes added in the meantime
let attempt = 0
// Keep track of whether the most recent attempt was rejected
let rejected = false
do {
attempt++
if (attempt > ATTEMPT_LIMIT) throw new Error(`Attempt limit exceeded`)
// Handle rejection for the previous attempt first such that, on
// the final attempt, time is not wasted rebasing it when it will
// not be pushed
if (rejected) {
info(`Fetching upstream ${action.branch}…`)
await execute(
`git fetch ${action.repositoryPath} ${action.branch}:${action.branch}`,
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
info(`Rebasing this deployment onto ${action.branch}…`)
await execute(
`git rebase ${action.branch} ${temporaryDeploymentBranch}`,
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
}
info(`Pushing changes… (attempt ${attempt} of ${ATTEMPT_LIMIT})`)
const pushResult = await execute(
`git push --porcelain ${action.repositoryPath} ${temporaryDeploymentBranch}:${action.branch}`,
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent,
true // Ignore non-zero exit status
)
rejected =
Boolean(action.isTest) ||
pushResult.stdout.includes(`[rejected]`) ||
pushResult.stdout.includes(`[remote rejected]`)
if (rejected) info('Updates were rejected')
// If the push failed for any fatal reason other than being rejected,
// there is a problem
if (!rejected && pushResult.stderr.trim().startsWith('fatal:'))
throw new Error(pushResult.stderr)
} while (rejected)
}
info(`Changes committed to the ${action.branch} branch… 📦`)
if (action.tag) {
info(`Adding '${action.tag}' tag to the commit…`)
await execute(
`git tag ${action.tag}`,
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
info(`Pushing '${action.tag}' tag to repository…`)
await execute(
`git push origin ${action.tag}`,
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
info(
`Tag '${action.tag}' created and pushed to the ${action.branch} branch… 🏷️`
)
}
return Status.SUCCESS
} catch (error) {
throw new Error(
`The deploy step encountered an error: ${suppressSensitiveInformation(
extractErrorMessage(error),
action
)} ❌`
)
} finally {
// Cleans up temporary files/folders and restores the git state.
info('Running post deployment cleanup jobs… 🗑️')
await execute(
`git checkout -B ${temporaryDeploymentBranch}`,
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
await execute(
`chmod -R +rw ${temporaryDeploymentDirectory}`,
action.workspace,
action.silent
)
await execute(
`git worktree remove ${temporaryDeploymentDirectory} --force`,
action.workspace,
action.silent
)
await rmRF(temporaryDeploymentDirectory)
}
}