Created
May 31, 2018 07:26
-
-
Save AntoineToubhans/0363c12994e3291cfe8a3eb04120fbd0 to your computer and use it in GitHub Desktop.
A tiny serverless plugin to check the current branch before deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const exec = require('child_process').exec; | |
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is not working for me. I tried with
serverless
v2.7 and now with v3.17.What I did so far.
To print it nicer in v3:
.serverless_plugins/check-git-branch-before-deploy.js
Also, in the
serverless.yml
,checkGitBranchBeforeDeploy
should go undercustom:
(this is not right here) andstage: ${opt:stage}
will throw an error insls v3
, so I did this for theyaml
and in theJS
code:serverless.yml
.serverless_plugins/check-git-branch-before-deploy.js
What's happing now:
Good!
Hmmm... not good :-(, nothing's happening.
Neither :-(
If I disable the
CheckGitBranchBeforeDeploy
plugin, all works fine.