Set Environment Variable Based on Branch


#1

Our Capybara tests can run on PhantomJS and on Selenium. I want to have a situation when

  • master branch builds run them on Selenium
  • staging / feature branches builds run them on Phantom JS

I can do that by setting environment variable CAPYBARA_DRIVER. However, I don’t know how to set this variable conditionally (by checking CIRCLE_BRANCH). What I tried is this

test:
  override:
    - case $CIRCLE_BRANCH in "master") CAPYBARA_DRIVER=selenium bundle exec rake test:subset ;; *) CAPYBARA_DRIVER=poltergeist bundle exec rake test:subset ;; esac:
      parallel: true
        files:
          - test/capybara/*_test.rb

But it of course doesn’t work (since rake test:subset is inside case). I don’t want to use test:post because I want to use Circle CI’s method of distributing tests across nodes.

Any help?


#2

Did you find out how? Same question over here.


#3

Partially. We kick of nightly builds on master using API. We scheduled this script to run at the same time everyday and it’s passing name of driver in build parameters

#!/bin/bash

_branch=$1
_circle_token=$2

trigger_build_url=https://circleci.com/api/v1/project/your-group/your-project/tree/${_branch}?circle-token=${_circle_token}

post_data=$(cat <<EOF
{
    "build_parameters": {
    "CAPYBARA_DRIVER": "selenium"
    }
}
EOF)

curl \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data "${post_data}" \
--request POST

#4

That looks like a great solution @rience, using the build parameters API is perfect for adding any additional constraints to your nightly build.

For anyone looking for more info, please refer to our nightly build docs.


#5