Travis and CodeDeploy
This guide will cover how to deploy to an Ubuntu instance using Travis CI and CodeDeploy.
There are several guides out there that cover CodeDeploy and Travis separately, but going through it myself I thought I would make a guide that has everything in one place.
Inside the AWS Console
To use CodeDeploy, we need a service role. In IAM, create a new role and attach the policy named “AWSCodeDeployRole”. If you need more help see the official documentation.
Now what we need to do is create an application within CodeDeploy. The process is pretty simple. For application name I just use my website URL’s, but you can use whatever identifying name you like. For Deployment Group Name, I use Production, Development, Staging, etc. You will be able to make more Deployment Groups later if you wish. When you get to the last step asking for a service role, use the one you just created.
Install and configure CodeDeploy on your server
First you’ll need to install the CodeDeploy agent. Here is the full guide on how to do it.
TL;DR:
|
|
Run sudo service codedeploy-agent status
to ensure that the agent is running. If your server already has files within the target directory, you will have to delete it either manually, or in a before_install script.
Next we’ll make several files in your project. The first is appspec.yml which tells CodeDeploy what to do.
|
|
Next in .travis.yml add the following lines. What this does is zips your project, uploads the zip to S3, and then tells CodeDeploy to deploy the zip onto your server. Something to note is that this will only work when you commit to the “dev” branch. You can add more providers for different branches. This is how I separate deployments for testing and production.
|
|
Now whenever you push a commit to the dev branch, your code will be deployed onto your server!
This next step is for those of you who will be deploying to a test and production environment. Something useful you can do within your CodeDeploy hook scripts is separate commands between your Deployment Groups. The below code will only run when in the “Development” deployment group. This is useful for things like updating database, uploads etc. that don’t need to be done on your production server.
|
|