-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (47 loc) · 1.49 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
const exec = require('child_process').exec
/**
* Plugin to prevent us from deploying the wrong Git branch to the wrong environment
*/
class CheckGitBranchBeforeDeploy {
constructor (serverless, options) {
this.commands = {
deploy: {
lifecycleEvents: [
'resources',
]
}
}
this.hooks = {
'before:deploy:resources': () => checkGitBranch(serverless),
}
}
}
const checkGitBranch = serverless => {
const stage = serverless.service.provider.stage
const requiredBranch = serverless.service.custom.checkGitBranchBeforeDeploy[stage]
if (!requiredBranch) {
serverless.cli.log(`[CheckGitBranchBeforeDeploy] No branch requirement for stage "${stage}"`)
return
}
serverless.cli.log(`[CheckGitBranchBeforeDeploy] Checking branch requirement "${requiredBranch}" for stage "${stage}"`)
return promiseExec('git rev-parse --abbrev-ref HEAD').then(({stdout}) => {
const currentBranch = stdout.split('\n')[0]
if (currentBranch !== requiredBranch) {
throw `[CheckGitBranchBeforeDeploy]\n\n
Current branch "${currentBranch}" and required branch "${requiredBranch}" mismatch.\n
Do a "git fetch --all && git checkout ${requiredBranch}" before deploy :)`
}
})
}
const promiseExec = cmd => (
new Promise((resolve, reject) => (
exec(cmd, (err, stdout, stderr) => {
if (err) {
reject(err)
} else {
resolve({stdout, stderr})
}
})
))
)
module.exports = CheckGitBranchBeforeDeploy