Build, tag and push Docker images using Travis CI
Docker Hub has Automated Builds capability to automatically build an image when you push changes to repository. It does its job perfectly. But sometimes you need more control over this process like tagging or triggering build of dependent images. You can do this by using Travis CI and adding following .travis.yaml
file in your repository:
sudo: required
services:
- docker
install:
- docker login --email=$DOCKER_HUB_EMAIL --username=$DOCKER_HUB_USERNAME --password=$DOCKER_HUB_PASSWORD
script:
- docker build -t $DOCKER_IMAGE_NAME .
- if [ ! -z "$TRAVIS_TAG" ]; then docker tag $DOCKER_IMAGE_NAME:latest $DOCKER_IMAGE_NAME:$TRAVIS_TAG; fi && docker push $DOCKER_IMAGE_NAME
env:
- DOCKER_IMAGE_NAME=suda/my-image
Next you have to add three environment variables to your Travis CI repository settings: DOCKER_HUB_EMAIL
, DOCKER_HUB_USERNAME
and DOCKER_HUB_PASSWORD
which are your Docker Hub credentials.
After this, every time you push new code it will build an image and if you tag a commit this image will be tagged in Docker Hub too.