Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Travis: different `script` for different branch?

Tags:

travis-ci

I'd like to setup deployment based on branches using Travis-CI and Github.

I.e. - if we made build from develop - then exec /deploy.rb with DEV env hostname, if master - then ./deploy.rb with PROD hostname and so on.

Only one idea I found - is to check $TRAVIS_BRANC variable and then execute script, like:

language: php
install:
  - true
script:
  - test $TRAVIS_BRANCH = "develop" && ./ci/deploy.rb envdev.tld 
  - test $TRAVIS_BRANCH = "master" && ./ci/deploy.rb envprod.tld

But this solution looks a bit weird as for me. Any other possibilities to realize that?

Any tips/links appreciated.

like image 202
setevoy Avatar asked Sep 09 '25 19:09

setevoy


2 Answers

Travis-CI always creates builds based on the .travis.yml in the branch you are pushing. As a solution, you could thus maintain different .travis.yml files in the different branches.

If you regularly merge between the branches, this could however lead to inadvertent changes between the branches (if you merge the changes of the .travis.yml of one branch over to the other). If this is a concern, your solution is probably safer.

To ensure that only specific branches (e.g. develop and master) are built, you can whitelist the branches in your .travis.yml.

When using your existing solution, you could simplify your travis.yml script though. It probably makes sense to move the logic for selecting the correct deploy target into your ci/deploy.rb script (or even add a separate wrapper script which you call from the .travis.yml). That way, you have only one script line in your .travis.yml which don't even needs to change if you change deployment targets.

Alternatively, to ensure you have no failing tests with your existing structure, you could even use something like this:

script:
  - if [ "$TRAVIS_BRANCH" = "develop" ]; then ./ci/deploy.rb envdev.tld; fi
  - if [ "$TRAVIS_BRANCH" = "master" ]; then ./ci/deploy.rb envprod.tld; fi
like image 156
Holger Just Avatar answered Sep 13 '25 06:09

Holger Just


Seems that the best solution now is to use stages. You can have something like:

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

See Travis documentation for more detail regarding stages.

like image 21
xji Avatar answered Sep 13 '25 07:09

xji