kharandziuk
kharandziuk

Reputation: 12890

Travis-ci: enviroment variables per branch in .travis.yml

I am able to create specific variables for a branch with travis-ci settings. Is there a way to achieve the same behavior with .travis.yml?

The prior answer was no(see the answer). But looks like it can be outdated nowadays.

Upvotes: 1

Views: 1214

Answers (2)

user12944897
user12944897

Reputation:

What DannyB is saying certainly makes sense and you should be familiar with it. However, in my setup, I am currently doing it this way, taking advantage of the env section in the .travis.yml file:

env:
  - PATH_TO_MY_BINARY=/tmp/bin

As currently seen in the dev/nightly version of my repo

I hope that would be helpful, in spite of bringing this post back from the dead.

Upvotes: 0

DannyB
DannyB

Reputation: 14776

Reading the documentation on environment variables, it seems like the answer is stlil no.

They say:

  • if it does not contain sensitive information and should be available to forks – add it to your .travis.yml
  • if it does contain sensitive information, and is the same for all branches – encrypt it and add it to your .travis.yml
  • if it does contain sensitive information, and might be different for different branches – add it to your Repository Settings

Keep in mind that the definition of environment variables in the Travis UI (as opposed to the .travis.yml) is intended to keep secrets that are not stored in source control.


It would seem like you have at least two options to play with:

  1. If these branches you are talking about are long-running branches, they can have a different .travis.yml file.
  2. If you wish these branches to always be mergable (and therefore, contain the definitions for each of the other branches), you may want to simply take care of these environment variables on your own. There are multiple ways to do this. It can be done as part of your test code initializer (in whatever language you are using), or as a shell script that can be sourceed in before_script, and have this script set different variables based on the $TRAVIS_BRANCH environment variable (or any other logic, or any other travis environment variable).

Something like that (untested, but I believe a variation of this should work):

# .travis.yml
before_script: source env-vars.sh

# env-vars.sh
if [[ "$TRAVIS_BRANCH" == "master" ]]; then
  export MY_VAR=master
else
  export MY_VAR=not-master
fi

Upvotes: 1

Related Questions