If / Else to Define Environment Variables in Travis

3

I'm using Travis for javascript (Grunt / Karma) testing and I set some variables in the .travis.yml file to run an array of tests. What I am missing is a means of defining 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 variable% travis $TRAVIS_PULL_REQUEST to control whether the test is triggered by a Pull Request in GitHub or not. In the case of a Pull Request I want to test the code only with PhantomJS.

Using the above variables without If / Else works, but I wanted to avoid using tests in browsers because they are done in SauceLabs and because the password is encrypted in the%% error log the test fails if the Request Request is from another repository on GitHub for security reasons.

    
asked by anonymous 14.03.2014 / 12:56

1 answer

0

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

This Travis variable is a string, even when it passes false , hence 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);
    
10.07.2014 / 22:41