Only build tags that are on the master branch

Hi,
I want to limit builds to tags which made on the master branch.

My .travis.yml :

dist: trusty

services:
  - docker

if: branch = master AND tag IS present

script:
  - export SHORT_TAG=${TRAVIS_TAG}
  - REPO=(${TRAVIS_REPO_SLUG//\// }) && export IMAGE_NAME=${REPO[0]//-}/${REPO[1]}
  - echo building image -\> ${IMAGE_NAME}:${SHORT_TAG}
  - docker build --no-cache -t ${IMAGE_NAME}:${SHORT_TAG} .
  - docker tag ${IMAGE_NAME}:${SHORT_TAG} ${IMAGE_NAME}:latest
  - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
  - docker push ${IMAGE_NAME}:${SHORT_TAG}
  - docker push ${IMAGE_NAME}:latest

But travis skip builds on pushed tags in the master branch with this message:

Skipped as per condition: IF branch = master AND tag IS present

I found this link, but it didn’t help me (:

Hey @mehran-prs,

Is the script your own logic?

Thanks,
Montana

In a tag build, branch is equal to the tag name so your current condition is false.

See Using `branches:` filter unexpectedly prevents tags from being built, too and Travis-ci.com pointlessly builds tags for why that is.

Use OR to build both master and tags:

if: branch = master OR tag IS present

If you want to additionally check if the tag is on the master branch, you need to do that by hand early in the build:

if [[ -n $TRAVIS_TAG ]]; then
  # Travis does a shallow clone by default
  # so `master` is not present in the local metadata
  # in a build for another branch
  git fetch origin master:master   
  if ! (git branch --contains "$TRAVIS_TAG" | grep -qxE '. master'); then
    travis_terminate 0    # quit the build early
  fi
fi
1 Like

Hey @Montana
Yes, it’s my own logic

Very thanks @native-api,
This is what I needed :heart:

Imprint