Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Travis-CI Docker Image Build and Push to AWS ECR
sudo: required #is required to use docker service in travis
language: php #can be any language, just php for example
services:
- docker # required, but travis uses older version of docker :(
install:
- echo "install nothing!" # put your normal pre-testing installs here
script:
- echo "no tests!" # put your normal testing scripts here
after_success:
- docker --version # document the version travis is using
- pip install --user awscli # install aws cli w/o sudo
- export PATH=$PATH:$HOME/.local/bin # put aws in the path
- eval $(aws ecr get-login --region us-east-1) #needs AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY envvars
- docker build -t my_example_app .
- docker tag my_example_app:latest [your_ecr_account].dkr.ecr.us-east-1.amazonaws.com/my_example_app:latest
- docker push [your_ecr_account].dkr.ecr.us-east-1.amazonaws.com/my_example_app:latest
# sadly, travis doesn't cache docker builds yet, so each build will take a while
@AcidLeroy

This comment has been minimized.

Copy link

commented Jul 3, 2016

Very helpful, thank you!

@thinktainer

This comment has been minimized.

Copy link

commented Jul 21, 2016

Awesome, Thank You for sharing this!

@timhaak

This comment has been minimized.

Copy link

commented Aug 26, 2016

Thanks :)

@mediba-Kitada

This comment has been minimized.

Copy link

commented Oct 13, 2016

👍

@nikhilo

This comment has been minimized.

Copy link

commented Dec 30, 2016

Problem with putting docker build in after_success step is, the travis build doesn't fail if docker build fails.

@xxx

This comment has been minimized.

Copy link

commented Jan 9, 2017

You an also use language: generic if you want a blank slate, which is nice with containers. Undocumented, so buyer beware etc.

@vineus

This comment has been minimized.

Copy link

commented Mar 20, 2017

@nikhilo: you can use travis script deployment: https://docs.travis-ci.com/user/deployment/script/ (just put all the docker operations in that script and make it return non 0 in case of failure).
That way you can also select which branch to deploy where

@georgschlenkhoff

This comment has been minimized.

Copy link

commented Feb 9, 2018

@BretFisher how do you export env variables? I tried with

travis encrypt AWS_SECRET_ACCESS_KEY=ABCDEFG --add env.matrix
travis encrypt AWS_ACCESS_KEY_ID=ABCDEFG --add env.matrix

but travis says: Partial credentials found in env, missing: AWS_SECRET_ACCESS_KEY.

Any clue?

@tdmalone

This comment has been minimized.

Copy link

commented Feb 15, 2018

You can also use export AWS_ACCOUNT_ID="$( aws sts get-caller-identity --output text --query 'Account' )" to automatically get your account ID. Then replace [your_ecr_account] with "${AWS_ACCOUNT_ID}".

@tdmalone

This comment has been minimized.

Copy link

commented Feb 15, 2018

@georgschlenkhoff I think you'll need to add them as global env vars, otherwise Travis will run the build once with AWS_SECRET_ACCESS_KEY and once with AWS_ACCESS_KEY_ID, which is not what you want. See https://docs.travis-ci.com/user/environment-variables/#Global-Variables

@matheusmessora

This comment has been minimized.

Copy link

commented May 31, 2018

Thank you so much!

Just a tip, if someone is receiving the error unknown shorthand flag: 'e' in -e after the command aws ecr get-login, it's because the missing --no-include-email flag, see the docs.

Also, I improved the script with env variables.

sudo: required #is required to use docker service in travis

language: java

env:
  - APP_NAME=my_example_app
  - AWS_ECR_ACCOUNT=09500000000

jdk:
  - oraclejdk8

#services: mongodb
services:
  - docker
  - mongodb

script:
  - gradle build

before_cache:
  - rm -f  $HOME/.gradle/caches/modules-2/modules-2.lock
  - rm -fr $HOME/.gradle/caches/*/plugin-resolution/
cache:
  directories:
    - $HOME/.gradle/caches/
    - $HOME/.gradle/wrapper/

after_success:
  - docker --version  # document the version travis is using
  - pip install --user awscli # install aws cli w/o sudo
  - export PATH=$PATH:$HOME/.local/bin # put aws in the path
  - eval $(aws ecr get-login --no-include-email --region us-east-1) #needs AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY envvars
  - docker build -t $APP_NAME:$TRAVIS_BUILD_ID .
  - docker tag $APP_NAME:$TRAVIS_BUILD_ID $AWS_ECR_ACCOUNT.dkr.ecr.us-east-1.amazonaws.com/$APP_NAME:$TRAVIS_BUILD_ID
  - docker push $AWS_ECR_ACCOUNT.dkr.ecr.us-east-1.amazonaws.com/$APP_NAME:$TRAVIS_BUILD_ID
@fefine

This comment has been minimized.

Copy link

commented Oct 9, 2018

@matheusmessora your env setings is wrong, the correct is

env:
     - APP_NAME=my_example_app AWS_ECR_ACCOUNT=09500000

I referenced this: https://docs.travis-ci.com/user/environment-variables/#defining-multiple-variables-per-item

@treystout

This comment has been minimized.

Copy link

commented Jun 3, 2019

@matheusmessora do you find tagging with the build number more valuable than the git commit hash or PR title?

For those interested here's the list of Travis-provided env vars possibly suitable for tagging images:
https://docs.travis-ci.com/user/environment-variables/#convenience-variables

@matheusmessora

This comment has been minimized.

Copy link

commented Jun 3, 2019

Old thread for me. But the build is incremental. That helped a lot in some troubleshooting.

But nice link, I'll give a try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.