forked from grails/grails-data-mapping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger-dependent-build.sh
executable file
·101 lines (86 loc) · 3.25 KB
/
trigger-dependent-build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/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