CI/CD with Travis CI & Terraform
Everything you need to setup a CI/CD pipeline for FREE with TravisCI and Terraform to automate the creation of your infrastructure
What is CI/CD
Continuous Integration (CI) & Continuous Deployment (CD) is a set of operational principles (most popular in Agile methodologies) that allow you to deliver code changes more frequently and reliably without the need for additional interaction. What this means for development teams is that it allows you to focus on providing business value rather than focussing on the operational parts of the software lifecycle.
The process in a nut shell:
CI: Build -> Package -> Test
CD: Deploy output from CI across all environments
Authorising Travis CI to GitHub
First things first, head over to and click on the SIGN IN WITH GITHUB
button (note: your repository must be public for you to use this service). You should be presented with an authorisation screen asking you to give Travis access to read data form your GitHub account
Once you have authorised Travis, you will be taken to your Dashboard and you can begin connecting your repositories.
Connecting your repositories
At this point you will need an AWS IAM user setup with programmatic access and the associated credentials. If you haven’t got those, check out last weeks tutorial on how to Setup Terraform with AWS.
On the left most side of the Travis Dashboard you will see a My Repositories
pane with a +
button, click it to add a new repository
If your repository doesn’t show up in the list (now or in future), you might have to do a one-off sync to get it added
Finally, we have to add our AWS IAM Credentials to the builds environment variables. Select your repository from your Dashboard
-> More options
-> Settings
Scroll down to the Environment Variables
section and add your Access/Secret Key here (note: keys must match the below names)
Adding Travis YML Config
Travis gives us the ability to define your pipeline using YML config so that you never have to worry about dealing with manual interactions again. This gives you the flexibility to define a new pipeline for every repository you own through code as part of your development process (DevOps)
Create a new file in the root of your repository called .travis.yml
and populate it with the following (full list of commands can be found )
Now that we have our YML in place, lets push our code to GitHub and then head over to start creating our Pull Request (PR)
git add .
git commit -m "Initial commit of Travis CI configuration"
git push -u origin feature/travis-ci-build
Creating a Pull Request
In the GitHub UI for your repository click on Compare & pull request
It should automatically default to the branch which you have most recently committed but in the event that it doesn’t, go ahead and select it from the drop down. Next, Give your PR a title, description and then click the Create pull request
button.
Once the Pull Request is created, GitHub will send a notification to Travis which will trigger a new build step as per our YML definition.
You can see above that the merge will only complete if our CI process has validated a successful build. This stops you from merging code that doesn’t work in production systems — so it’s good practice to get used to this style of handling pull requests. If you head over to the Travis Dashboard now you will see your build running and you can then follow the output in the logs!
Deploying to AWS
So we have defined a build plan that we successfully setup to run when we create a new PR. Once you have validated the output of Terraform’s Plan
stage and are happy with the changes that are going to be applied… Click the Merge pull request
button.
Now remember, we only wanted to execute the Plan
phase for PR’s in our build pipeline and the Apply
phase comes when we merge. So head over to your Travis Dashboard again and you should see it executing another build but this time actually deploying the changes!
Conclusion
Now that you have integrated your repositories, you can do so for every project you start! You can already see how much time it will save you in the future building, testing and deploying your code, so try to make it part of your development lifecycle from here on.
Let me know what you thought of this style of tutorial in the comments below and please do give suggestions on any improvements that can be made for future tutorials. See you next week!
Resources
Travis CI:
Travis Build Commands:
Setting up Terraform with AWS: http://jeetwincasinos.com/startupchronicles/setting-up-terraform-with-amazon-web-services-aws-afb2fde6cf18
Travis YML Config: