| #!/bin/bash |
| |
| # This script lives in each of the upstream repos. Add this to .travis.yml to |
| # run after each successful build (assuming that the script is in the root of |
| # the repo): |
| # after_success: |
| # - ./trigger-dependent-build |
| # |
| # There are three variables to set - `$auth_token`, `$endpoint`, and |
| # `$repo_id` - each is described below. |
| # |
| |
| # An authorization token generated by running: |
| # gem install travis |
| # travis login |
| # travis token |
| # |
| auth_token=$TRAVIS_TOKEN |
| |
| # The Travis API endpoint. .com and .org are the commercial and free versions, |
| # respectively; enterprise users will have their own hostname. |
| # |
| endpoint=https://api.travis-ci.org |
| |
| # The ID of the tests repo. To find the ID of a repo from its slug `$slug`, run: |
| # curl -H 'Authorization: token $auth_token' https://api.travis-ci.com/repos/$slug |
| # |
| repo_id=6629241 |
| |
| # Only run for master builds. Pull request builds have the branch set to master, |
| # so ignore those too. |
| # |
| if [ "${TRAVIS_BRANCH}" != "master" ] || [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then |
| exit 0 |
| fi |
| |
| # Make an API request using the auth token set above. First argument is the path |
| # of the API method, all later arguments are passed to curl directly. |
| # |
| function travis-api { |
| curl -s $endpoint$1 \ |
| -H "Authorization: token $auth_token" \ |
| -H 'Content-Type: application/json' \ |
| "${@:2}" |
| } |
| |
| # Create a new environment variable for the repo and return its ID. First |
| # argument is the environment variable name, and the second is the value. |
| # |
| function env-var { |
| travis-api /settings/env_vars?repository_id=$repo_id \ |
| -d "{\"env_var\":{\"name\":\"$1\",\"value\":\"$2\",\"public\":false}}" | |
| sed 's/{"env_var":{"id":"\([^"]*\)",.*/\1/' |
| } |
| |
| # Get the build ID of the last master build. |
| # |
| last_master_build_id=`travis-api /repos/$repo_id/branches/master | |
| sed 's/{"branch":{"id":\([0-9]*\),.*/\1/'` |
| |
| # Set the three environment variables needed, and capture their IDs so that they |
| # can be removed later. |
| # |
| env_var_ids=(`env-var DEPENDENT_BUILD true` |
| `env-var TRIGGER_COMMIT $TRAVIS_COMMIT` |
| `env-var TRIGGER_REPO $TRAVIS_REPO_SLUG` |
| `env-var GORM_VERSION "5.0.3.BUILD-SNAPSHOT"`) |
| |
| # Restart the last master build. |
| # |
| travis-api /builds/$last_master_build_id/restart -X POST |
| |
| # Wait for the build to start using the new environment variables. |
| # |
| until travis-api /builds/$last_master_build_id | grep '"state":"started"'; do |
| sleep 5 |
| done |
| |
| # Get the build ID of the last 3.1.x build. |
| # |
| last_threeone_build_id=`travis-api /repos/$repo_id/branches/3.1.x | |
| sed 's/{"branch":{"id":\([0-9]*\),.*/\1/'` |
| |
| # Restart the last 3.1.x build. |
| # |
| travis-api /builds/$last_threeone_build_id/restart -X POST |
| |
| # Wait for the build to start using the new environment variables. |
| # |
| until travis-api /builds/$last_threeone_build_id | grep '"state":"started"'; do |
| sleep 5 |
| done |
| |
| # Remove all of the environment variables set above. This does mean that if this |
| # script is terminated for whatever reason, these will need to be cleaned up |
| # manually. We can do this either through the API, or by going to Settings -> |
| # Environment Variables in the Travis web interface. |
| # |
| for env_var_id in "${env_var_ids[@]}"; do |
| travis-api /settings/env_vars/$env_var_id?repository_id=$repo_id -X DELETE |
| done |