I have a project that I'm trying to automate tests using Travis CI. I have a dev branch and a test branch, and they have different environment variables. For example, on the dev branch, I need to connect to a different API than the test branch, specified by an environment variable. So, when I run the build on the dev branch on Travis, how do I set it up so that it only tests with the dev set of environment variables, and likewise for build on test branch?
Asked
Active
Viewed 3,236 times
2 Answers
9
There is no great way to do this right now, but you can write a shell script that checks the Travis environmental variable TRAVIS_BRANCH
(Which returns the branch that Travis is testing) and sets respective environmental variables in response. Here is a short example (Please note that I'm not a expert in shell scripting so if I screwed this up or did something silly let me know and I'll fix it):
if [ ${TRAVIS_BRANCH} == development ]; then
TEST_MODE=dev stuff
else
TEST_MODE=master stuff
fi
export TEST_MODE

joshua-anderson
- 4,458
- 5
- 35
- 56
-
Can you extend your answer with a example of how to use this in your yaml file? I mean for example after running this script, can I just use $TEST_MODE in my yaml? – Nebulosar Jul 15 '19 at 10:19
4
Travis can have different .travis.yml
configs for each branch.
So modifying .travis.yml
on branch test
doesn't affect .travis.yml
on develop
or master
branch:
develop
branch.travis.yml
:env: - DEVELOP_BRANCH_VARIABLE=FOO script: - ./run-develop-branch ${DEVELOP_BRANCH_VARIABLE}
test
branch.travis.yml
:env: - TEST_BRANCH_VARIABLE=BOO script: - ./run-test-branch ${TEST_BRANCH_VARIABLE}
-
1Unless you go to merge from one branch to another, right? Every time you touch that file you'll have to be very careful to merge it correctly to preserve intentional differences between branches. – justin.m.chase Apr 28 '15 at 18:05
-
@justin.m.chase I see not so many alternatives here. If you will test `TRAVIS_BRANCH` you need to commit to branch that is not changing at all and merge to others. Usually my config files is just one script like `./build.py --verbose`, so they are not changing very often. Also that's the only way to test different OS'es. See http://stackoverflow.com/a/19829380/2288008 – Apr 29 '15 at 10:46
-
You can make a script that looks at the branch environment variable or gets the branch out of git like so: `function git.branch { br=`git branch | grep "*"` echo ${br/* /} }` – justin.m.chase Apr 30 '15 at 02:01
-
-
@running.t If you do that, the file wil not get updated on your git repo, so that wil not help the situation. – Nebulosar Jul 15 '19 at 09:22
-
@Nebulosar: The issue here is that you cant have everything at once. Either you use git to manage and merge your code and you have to carefully "automerge" of `.travis.yml` or you skip this file from beeing managed by git by adding it to `.gitignore` and then you have to manage it manually. There is no automagic solution that will work always... I just presented 2 alternatives. – running.t Jul 18 '19 at 09:02
-
Aah alright, I think I misunderstood you there. I agree on the fact that you can't have the gold shower from both sides. To bad you can't just have two different `.travis.yml` files and let travis pick the right one based on your branch, master or not – Nebulosar Jul 18 '19 at 09:59