AWS CodeDeploy

This page documents deployments using dpl v1 which currently is the default version. The next major version dpl v2 will be released soon, and we recommend starting to use it. Please see our blog post for details. dpl v2 documentation can be found here.

Travis CI can automatically trigger a new Deployment on AWS CodeDeploy after a successful build.

For a minimal configuration with S3, add the following to your .travis.yml:

deploy:
  - provider: s3
    # rest of S3 deployment for MyApp.zip
    # ⋮
  - provider: codedeploy
    access_key_id: "YOUR AWS ACCESS KEY"
    secret_access_key: "YOUR AWS SECRET KEY"
    bucket: "S3 Bucket"
    key: latest/MyApp.zip
    application: MyApp
    deployment_group: MyDeploymentGroup

Note that in this example, Travis CI will attempt to deploy to an existing CodeDeploy Application called MyApp in AWS Region us-east-1.

A complete example can be found here.

You can find your AWS Access Keys here. It is recommended to encrypt that key.

If your CodeDeploy application lives in any region other than us-east-1 please add a region field to .travis.yml (see AWS-region-to-deploy-to).

Assuming you have the Travis CI command line client installed, you can do it like this:

travis encrypt --add deploy.secret_access_key

You will be prompted to enter your api key on the command line.

You can also have the travis tool set up everything for you:

travis setup codedeploy

Keep in mind that the above command has to run in your project directory, so it can modify the .travis.yml for you.

This command will also offer to set up S3 deployment, if you want to bundle to be uploaded from the Travis CI build.

Branch to deploy from #

You can explicitly specify the branch to deploy from with the on option:

deploy:
  provider: codedeploy
  access_key_id: "YOUR AWS ACCESS KEY"
  secret_access_key: "YOUR AWS SECRET KEY"
  bucket: "S3 Bucket"
  key: latest/MyApp.zip
  bundle_type: zip
  application: MyApp
  deployment_group: MyDeploymentGroup
  on:
    branch: production

Alternatively, you can also configure Travis CI to deploy from all branches:

deploy:
  provider: codedeploy
  access_key_id: "YOUR AWS ACCESS KEY"
  secret_access_key: "YOUR AWS SECRET KEY"
  bucket: "S3 Bucket"
  key: latest/MyApp.zip
  bundle_type: zip
  application: MyApp
  deployment_group: MyDeploymentGroup
  on:
    all_branches: true

Builds triggered from Pull Requests will never trigger a release.

S3 deployment or GitHub deployment #

For a minimal configuration with GitHub, add the following to your .travis.yml:

deploy:
  - provider: codedeploy
    revision_type: github
    access_key_id: "YOUR AWS ACCESS KEY"
    secret_access_key: "YOUR AWS SECRET KEY"
    application: "Your Codedeploy application"
    deployment_group: "The Deployment group associated with the codedeploy application"
    region: "Region in which your ec2 instance is."

Please note that region should match the instance region on which codedeploy is configured.

Also note that GitHub deployments target a specific commit, and any unversioned files will not be deployed. If you need to add unversioned files to your deployment, use an S3 deployment instead.

Waiting for Deployments #

By default, the build will continue immediately after triggering a CodeDeploy deploy. To wait for the deploy to complete, use the wait_until_deployed option:

deploy:
  provider: codedeploy
  wait_until_deployed: true
  # ⋮

Travis CI will wait for the deploy to complete, and log whether it succeeded.

Bundle Types #

The bundleType of your application is inferred from the file extension of key or s3_key set in your .travis.yml.

If your .travis.yml contains both, and they do not match, set bundle_type explicitly to the correct value.

Conditional deployments #

You can deploy only when certain conditions are met. See Conditional Releases with on:.

Note on .gitignore #

As this deployment strategy relies on git, be mindful that the deployment will honor .gitignore.

If your .gitignore file matches something that your build creates, use before_deploy to change its content.

Running commands before and after deploy #

Sometimes you want to run commands before or after deploying. You can use the before_deploy and after_deploy stages for this. These will only be triggered if Travis CI is actually deploying.

before_deploy: "echo 'ready?'"
deploy:
  # ⋮
after_deploy:
  - ./after_deploy_1.sh
  - ./after_deploy_2.sh

AWS region to deploy to #

You can explicitly specify the AWS region to deploy to with the region option:

deploy:
  provider: codedeploy
  access_key_id: "YOUR AWS ACCESS KEY"
  secret_access_key: "YOUR AWS SECRET KEY"
  bucket: "S3 Bucket"
  key: latest/MyApp.zip
  bundle_type: zip
  application: MyApp
  deployment_group: MyDeploymentGroup
  region: us-west-1