Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A tiny serverless plugin to check the current branch before deploy
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;
@alanwilter
Copy link

alanwilter commented May 12, 2022

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

    if (currentBranch !== requiredBranch) {
      serverless.cli.log(`[CheckGitBranchBeforeDeploy]\n\n\
  Current branch "${currentBranch}" and required branch "${requiredBranch}" mismatch.\n\
  Do a "git fetch --all && git checkout ${requiredBranch}" before deploy :)`);
      throw "";
    }

Also, in the serverless.yml, checkGitBranchBeforeDeploy should go under custom: (this is not right here) and stage: ${opt:stage} will throw an error in sls v3, so I did this for the yaml and in the JS code:

serverless.yml

custom:
  myStage: ${sls:stage}
  checkGitBranchBeforeDeploy:
    beta-live: beta-live
    dev-live: dev-live
    prod-live: prod-live

.serverless_plugins/check-git-branch-before-deploy.js

const stage = serverless.service.custom.myStage;

What's happing now:

# on main branch
$ sls deploy -s beta-live
Running "serverless" from node_modules

Deploying private-eye-converter to stage beta-live (eu-west-2, "mep" provider)
[CheckGitBranchBeforeDeploy] Checking branch requirement "beta-live" for stage "beta-live"
[CheckGitBranchBeforeDeploy]

  Current branch "main" and required branch "beta-live" mismatch.
  Do a "git fetch --all && git checkout beta-live" before deploy :)

✖ Stack private-eye-converter-beta-live failed to deploy (0s)

Good!

# on beta-live branch
$ sls deploy -s beta-live
Running "serverless" from node_modules

Deploying private-eye-converter to stage beta-live (eu-west-2, "mep" provider)
[CheckGitBranchBeforeDeploy] Checking branch requirement "beta-live" for stage "beta-live"

✔ Service deployed to stack private-eye-converter-beta-live (0s)

Hmmm... not good :-(, nothing's happening.

# on beta-live branch
$ sls deploy -s test
Running "serverless" from node_modules

Deploying private-eye-converter to stage test (eu-west-2, "mep" provider)
[CheckGitBranchBeforeDeploy] No branch requirement for stage "test"

✔ Service deployed to stack private-eye-converter-test (0s)

Neither :-(

If I disable the CheckGitBranchBeforeDeploy plugin, all works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment