Git Branch Control when Deploying on AWS with Serverless Framework

Use a Serverless Plugin to check your Git branch before deploying on AWS

Antoine Toubhans
Sicara's blog
3 min readJul 23, 2018

--

Read the full article on Sicara’s website here.

In this article, you will see how to write a Serverless plugin that checks your local Git branch before deployment so as to avoid deploying bad code.

If you are curious about the Serverless framework, you can read this excellent article explaning how to deploy a rest api with Serverless in 15 minutes.

Deploy to the Cloud!

We use the amazing Serverless toolkit to deploy to AWS. We managed the different environments using environment variables as explained in the Serverless documentation.

To deploy to the desired environment, we simply run:

where TARGET_ENV is the target environment e.g., development, staging or production.

The application environments

A development process typically includes the following environments:

  • The development environment: This is the environment where the developers run the code they are writing. Because the developers need to test intensively their code, the development environment can be broken sometime — and it is even meant to be.
  • The staging environment: This is where the product owner validates the new features. It should never be broken so that validation can be done at any time. The purpose of this environment is to see the developers daily work, so features may be partially implemented.
  • The production environment: This is the environment for the end-users.
    It should never be broken and features should be complete.

The problem: you could deploy bad/buggy code

When deploying, local files are zipped and uploaded directly to AWS.

There is no control of the local files before there are uploaded, which is fine when deploying to the development environment but wrong for staging or production environment.

Local files uploaded to the staging environment in the Cloud

In the screenshot above, the git branch sprint18/feature919/awesome-feature is deployed to the staging environment. This could be wrong for several reasons:

  • the branch sprint18/feature919/awesome-feature has not yet been merged into the master branch, meaning it was not reviewed by the other developers. Thus, it could break the staging environment.
  • the branch sprint18/feature919/awesome-feature is local, meaning it does not include features developed by other members of the team. Thus, it could induce regression by making these other features disappearing.
  • there are pending changes not yet committed. Thus, the deployed code is not versioned and could eventually be lost.

The solution: a Serverless plugin

To prevent us from deploying the wrong Git branch to the wrong environment, we added a hook to our deployment, by implementing a Serverless plugin.

You can learn how to write you own Serverless plugins in this blog post.

Here is the code:

… Read the full article on Sicara’s website here.

--

--