Skip to content

Instantly share code, notes, and snippets.

@jamesarosen
Last active June 5, 2021 18:39
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesarosen/e29076bd81a099f0f72e to your computer and use it in GitHub Desktop.
Save jamesarosen/e29076bd81a099f0f72e to your computer and use it in GitHub Desktop.
Running Two Very Different Travis Builds

I have a project that's been happily chugging along on Travis for a while. Its .travis.yml looks something like

script:
  - node_modules/ember-cli/bin/ember test

I wanted to add a second parallel build that did something very different. I didn't want to run ember test with a different Ember version or some other flag. I wanted to run a completely different command. Specifically, I wanted to run LicenseFinder's audit.

Travis has great docs on customizing parallel builds, but nothing describes how to do two completely different commands.

I poked at Bash for a while and finally came up with

env:
  - TEST_COMMAND="node_modules/ember-cli/bin/ember test"
  - TEST_COMMAND="bundle exec license_finder"

script:
  - (eval "$TEST_COMMAND")

The eval inherits the current shell's environment, and the (...) wrapping creates a sub-shell to isolate this shell from the effects of TEST_COMMAND.

Things That Didn't Work

This was my first attempt. I still don't quite understand why it doesn't work.

script:
  - $TEST_COMMAND

A second go breaks because I didn't wrap the argument to bash -c in quotes, meaning any arguments I tried to pass to ember got lost:

script:
  - bash -c $TEST_COMMAND

But even wrapping it in quotes failed because bash -c loses too much of the environment:

script:
  - bash -c "$TEST_COMMAND"
@fh
Copy link

fh commented Jun 22, 2015

We at @easybiblabs solved the same problem slightly different by wrapping it in a TO_TEST envvar with multiple values, which helps if you are running multiple commands after each other per build, or have to use different setup statements in the install section. It roughly works along those lines:

matrix:
  include:
    - php: 5.6
      env: TO_TEST=FRONTEND
    - php: 5.6
      env: TO_TEST=BACKEND
  fast_finish: true

install:
  - if [ "$TO_TEST" = "BACKEND" ]; then some_backend_command; fi
  - if [ "$TO_TEST" = "FRONTEND" ]; then some_frontend_command; fi

script:
  - if [ "$TO_TEST" = "BACKEND" ]; then some_backend_command; fi
  - if [ "$TO_TEST" = "FRONTEND" ]; then some_frontend_command; fi
  - if [ "$TO_TEST" = "FRONTEND" ]; then some_other_frontend_command; fi

@Nicofuma
Copy link

We at @phpbb we have a problem similar (we need different setup and test suite depending on the environment) and we solved it like @fh.
In the matrix we define a few envvar and we use them to select and configure our scripts.
This way we can do whatever we want and run exactly what we want in each configuration.

https://github.com/phpbb/phpbb/blob/3.1.x/.travis.yml

@alper
Copy link

alper commented Mar 27, 2018

I think I'm going to end up using this, but isn't this a terrible kludge? Anything on the horizon to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment