-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrigger.sh
executable file
·54 lines (47 loc) · 1.46 KB
/
trigger.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
#!/bin/bash
# variables
USER=$1
REPO=$2
BRANCH=$3
MESSAGE=$4
# login to travis and get token
travis login --skip-completion-check --github-token $TRAVIS_ACCESS_TOKEN
travis whoami --skip-completion-check
TOKEN=$(travis token --skip-completion-check)
IFS=' ' read -r -a array <<< "$TOKEN"
TOKEN=${array[${#array[@]}-1]}
# inspired from plume-lib, check arguments and add message
if [ $# -eq 4 ] ; then
MESSAGE=",\"message\": \"$4\""
elif [ -n "$TRAVIS_REPO_SLUG" ] ; then
MESSAGE=",\"message\": \"Triggered from upstream build of $TRAVIS_REPO_SLUG by commit "`git rev-parse --short HEAD`"\""
fi
# for debugging
echo "USER=$USER"
echo "REPO=$REPO"
echo "TOKEN: ${array[${#array[@]}-1]}"
echo "BRANCH=$BRANCH"
echo "MESSAGE=$MESSAGE"
# curl POST request content body
BODY="{
\"request\": {
\"branch\":\"$BRANCH\"
$MESSAGE
}}"
# make a POST request with curl (note %2F could be replaced with
# / and additional curl arguments, however this works too!)
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Travis-API-Version: 3" \
-H "Authorization: token ${TOKEN}" \
-d "$BODY" \
https://api.travis-ci.org/repo/${USER}%2F${REPO}/requests \
| tee /tmp/travis-request-output.$$.txt
if grep -q '"@type": "error"' /tmp/travis-request-output.$$.txt; then
cat /tmp/travis-request-output.$$.txt
exit 1
elif grep -q 'access denied' /tmp/travis-request-output.$$.txt; then
cat /tmp/travis-request-output.$$.txt
exit 1
fi