-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrigger_build.sh
executable file
·73 lines (65 loc) · 2.13 KB
/
trigger_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
#!/usr/bin/env bash
set -e
# this script expects 3 inputs:
# 1. SDK environment variable should be set
# 2. first argument: repo_slug (repository to trigger a travis build on)
# 3. second argument: branch (branch of the above repo)
repo_slug=$1
#branch="${2:-master}"
branch="${2:-jtong/travis_clean}"
# why travis creates two builds for every commit push:
# https://stackoverflow.com/questions/34974925/travis-ci-creates-two-builds-for-each-github-commit-push
#
# one build is event type "push", the other is event type "pull request" we can only use the latter type
if [ "$TRAVIS_EVENT_TYPE" == "push" ]; then
echo "INFO: TRAVIS_EVENT_TYPE=push so TRAVIS_PULL_REQUEST_SHA and TRAVIS_PULL_REQUEST_SLUG are empty."
echo "INFO: without these values, this is going to be a noop (build wont be triggered)"
exit 0
elif [ "$TRAVIS_EVENT_TYPE" == "pull_request" ]; then
echo "INFO: TRAVIS_EVENT_TYPE=pull_request. Triggering build..."
else
echo "ERROR: i do not understand TRAVIS_EVENT_TYPE=$TRAVIS_EVENT_TYPE"
exit 2
fi
body=$(cat <<EOF
{
"request": {
"message": "Override the commit message: this is an api request",
"branch": "${branch}",
"config": {
"sudo": "required",
"language": "generic",
"merge_mode": "deep_merge",
"env": {
"global": {
"UPSTREAM_SHA": "${TRAVIS_PULL_REQUEST_SHA}",
"UPSTREAM_REPO": "${TRAVIS_PULL_REQUEST_SLUG}",
"RUN_ALL": false,
"run_SDK": "${SDK}",
"SDK_BRANCH": "${TRAVIS_PULL_REQUEST_BRANCH}"
}
},
"after_success": "STATE=success scripts/update_build_status.sh",
"after_failure": "STATE=failure scripts/update_build_status.sh"
}
}
}
EOF
)
REPO="https://api.travis-ci.com/repo/$repo_slug/requests"
output=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Travis-API-Version: 3" \
-H "Authorization: token $TRAVIS_COM_TOKEN" \
-d "$body" \
$REPO
)
if [[ "$output" == *"error"* ]]; then
echo "ERROR: curl did not succeed: $output"
echo "Things to check:"
echo "is TRAVIS_COM_TOKEN defined?"
echo "is this valid json?"
echo "$body"
exit 1
fi