Build Stages: Sharing a Docker image

This example has 2 build stages:

  • One job builds and pushes a Docker image
  • Two jobs that pull and test the image

Here’s what the .travis.yml config could look like:

sudo: true
dist: trusty

jobs:
  include:
    - stage: build docker image
      script:
      - docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"
      - docker build -t travis-ci-build-stages-demo .
      - docker images
      - docker tag travis-ci-build-stages-demo $DOCKER_USERNAME/travis-ci-build-stages-demo
      - docker push $DOCKER_USERNAME/travis-ci-build-stages-demo
    - stage: test
      script: docker run --rm $DOCKER_USERNAME/travis-ci-build-stages-demo cat hello.txt
    - script: docker run --rm $DOCKER_USERNAME/travis-ci-build-stages-demo cat hello.txt

This is how the build matrix might look:

image

You can find the code for this example on our demo repository: