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:

1
2
3
4
5
6
7
8
9
sudo apt-get update
sudo apt-get install python-pip
sudo apt-get install ruby2.0
sudo pip install awscli
aws configure
cd /home/ubuntu
aws s3 cp s3://aws-codedeploy-[region]/latest/install . --region [region]
chmod +x ./install
sudo ./install auto

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.

1
2
3
4
5
6
7
8
9
10
version: 0.0
os: linux
files:
- source: /
destination: /var/www/html/
hooks:
AfterInstall:
- location: scripts/post_install.sh
timeout: 300
runas: root

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
before_deploy:
- zip -r project *
- mkdir -p codedeploy_zip
- mv trips.zip codedeploy_zip/project.zip
deploy:
- provider: s3
access_key_id: xxxxxxxxxxxxxxxxxxxx
Secret_access_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
local_dir: codedeploy_zip
skip_cleanup: true
bucket: cd-bucket-dev
region: [region]
on:
branch: dev
- provider: codedeploy
access_key_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
secret_access_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
bucket: cd-bucket-dev
key: project.zip
bundle_type: zip
application: MyApp
deployment_group: Development
region: [region]
on:
branch: dev

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.

1
2
3
4
if [ "$DEPLOYMENT_GROUP_NAME" == "Development" ]
then
{{ conditional code here }}
fi

If you like what you read and want to work with me, contact me on LinkedIn