15

In my travis script I have the following:

after_success:
- ember build --environment=production
- ember build --environment=staging --output-path=dist-staging

After both of these build, I conditionally deploy to S3 the one that is appropriate, based on the current git branch.

It works, but it would save time if I only built the one I actually need. What is the easiest way to build based on the branch?

4 Answers 4

35

use the test command as used here.

after_success:
  - test $TRAVIS_BRANCH = "master" &&
    ember build

All travis env variables are available here.

3
  • Thanks! I thought it must be possible without a separate bash script. Commented Jan 4, 2016 at 16:11
  • yeah I was having the same issue yesterday. forgot where I read about using that test command but a simple if didn't work for me. Commented Jan 4, 2016 at 16:12
  • 17
    When Travis builds a PR targeted at master, $TRAVIS_BRANCH will be set to "master". So to make sure this is really only executed for master, you need to use test $TRAVIS_BRANCH = "master" && test $TRAVIS_PULL_REQUEST = "false" && ember build
    – britter
    Commented Mar 16, 2017 at 10:06
8

You can execute shell script in after_success and check the current branch using travis environment variables:

#!/bin/sh

if [[ "$TRAVIS_BRANCH" != "master" ]]; then
  echo "We're not on the master branch."
  # analyze current branch and react accordingly
  exit 0
fi

Put the script somewhere in the project and use it like:

after_success:
- ./scripts/deploy_to_s3.sh

There might be other useful travis variables to you, they are listed here.

2
  • Also, don't forget to chmod +x deploy_to_s3.sh before pushing it, otherwise it will be not executable. Commented Aug 21, 2019 at 11:09
  • And, btw, [[ ... ]]`` seems to be not supported in sh, so I use bash` there instead. Commented Aug 21, 2019 at 11:12
1

With the following entry the script will only be executed if it is not a PR and the branch is master.

after_success:
  - 'if [ "$TRAVIS_PULL_REQUEST" = "false" -a "$TRAVIS_BRANCH" = "master" ]; then bash doit.sh; fi'

It is not enough to evaluate TRAVIS_BRANCH. TRAVIS_BRANCH is set to master when a PR against master is created by a fork.

See also the description of TRAVIS_BRANCH on https://docs.travis-ci.com/user/environment-variables/:

  • for push builds, or builds not triggered by a pull request, this is the name of the branch
  • for builds triggered by a pull request this is the name of the branch targeted by the pull request
  • for builds triggered by a tag, this is the same as the name of the tag (TRAVIS_TAG)

If you work with tags you have to consider TRAVIS_TAG as well. If TRAVIS_TAG is set, TRAVIS_BRANCH is set to the value of TRAVIS_TAG.

after_success:
  - if [ "$TRAVIS_PULL_REQUEST" = "false" -a \( "$TRAVIS_BRANCH" = "master" -o -n "$TRAVIS_TAG" \) ]; then doit.sh; fi
-2

I would say the above solutions are good because they would transfer to non-travis-ci build systems as well, but there is a feature in TravisCI for similar to this:

stages:
  - name: deploy
    # require the branch name to be master (note for PRs this is the base branch name)
    if: branch = master

Although I could not get it to work with after_success, the following page has a section on "Testing Conditions" which I didn't bother setting that up.

https://docs.travis-ci.com/user/conditional-builds-stages-jobs/

1
  • note that after_success is a build step within a job, whereas a travis stage is a different concept, it's a way to group jobs. So the docs about conditional build stages are not about job steps.
    – dragonx
    Commented Jan 30, 2019 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.