github – If / Else para defenir environment variables no Travis

Question:

I'm using Travis for javascript tests (Grunt/Karma) and I set some variables in the .travis.yml file to run an array of tests. What I'm missing is a way to define variables using an if/else.

I'm looking for something like this logic:

language: node_js
node_js:
  - 0.11
env:
  matrix:
    if ($TRAVIS_PULL_REQUEST == 'false') {
      - BROWSER='chrome_linux'    BUILD='default'
      - BROWSER='chrome_linux'    BUILD='nocompat'
      - BROWSER='firefox_linux'   BUILD='default'
      - BROWSER='firefox_linux'   BUILD='nocompat'
   }
   else {
     - BROWSER='phantomjs'    BUILD='default'
   }

I use the travis variable $TRAVIS_PULL_REQUEST to control whether the test is triggered by a Pull Request on GitHub or not. In case it is a Pull Request I want to test the code only with PhantomJS.

Using the variables above without If/Else works, but I wanted to avoid using tests in browsers because these are done in SauceLabs and as the password is embedded in the .yml .yml the test fails if the Pull Request is from another repository on GitHub for reasons of security.

Answer:

This is apparently not possible in Travis. The solution I found was to use this variable that travis gives ( $TRAVIS_PULL_REQUEST ) and use it inside my gruntfile where it is provided by grunt in process.env.TRAVIS_PULL_REQUEST .

This Travis variable is a string, even when I pass false , so I use pullRequest != 'false' .

var pullRequest = process.env.TRAVIS_PULL_REQUEST;
var tasks = ['clean', 'packager:all', 'packager:specs'];
tasks =  pullRequest != 'false' ? tasks.concat('karma:continuous') : tasks.concat('karma:sauceTask');

grunt.registerTask('default:travis', tasks);
Scroll to Top