Travis conditional on branch after_success

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, specifically the base branch? I’ve been trying to figure this out for some days now.

Hey @TheDFury, seems as if I answered this on GitHub as well but just in case you don’t check one or the other.

You should use the test command in the after_success section of your build instruction file. So your .travis.yml file would look like this:

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

Alternatively you can deploy using a bash script in after_success and the script will check the branch using Travis environment variables:

#!/bin/sh

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

Add the script somewhere in the build instruction, like so:

after_success:
- ./scripts/deploy_to_s3.sh

Then you’ll be on your way to a successful deployment, and just like in any *nix distro, don’t forget to change permissions via chmod u+x so it’s read and writable. If you run into further problems try reading the the official Travis Documentation about environment variables.

Please stop back by if you have any issues whatsoever, myself or another talented member of the Travis CI staff will get back to you.

-Montana (Travis CI Staff)

Thanks the bash script worked

Yeah of course @TheDFury,

Further questions please post on the community forum from now on. It’s a lot easier for people like myself and more helpful community members to take a look at your problem and help you.

-Montana (Travis CI Staff)

Imprint