If / Else to defenir Environment variables in Travis

Asked

Viewed 73 times

3

I am using Travis for javascript testing (Grunt/Karma) and set some variables in the file .travis.yml to run a test matrix. What I’m missing is a means of defining variables using an if/Else.

I’m looking for something similar to 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'
   }

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 browser tests because these are done in Saucelabs and how the password is surmounted in the database .yml the test fails if Pull Request is from another repository on Github for security reasons.

1 answer

0


This is apparently not possible in Travis. The solution I found was to use that variable that Travis gives ($TRAVIS_PULL_REQUEST) and use it inside my gruntfile where it is made available by Grunt at 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.