Last active
August 17, 2020 08:24
-
-
Save ricardocabral/a33ed72dac40312d815c226a5c297306 to your computer and use it in GitHub Desktop.
trigger a downstream travis build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## brief: trigger a downstream travis build | |
## see: travis API documentation | |
## forked from https://raw.githubusercontent.com/stephanmg/travis-dependent-builds/master/trigger-travis.sh | |
# variables | |
USER=$1 | |
REPO=$2 | |
TRAVIS_ACCESS_TOKEN=$3 | |
BRANCH=$4 | |
MESSAGE=$5 | |
# login to travis and get token | |
travis login --skip-completion-check --pro --github-token $TRAVIS_ACCESS_TOKEN | |
travis whoami --skip-completion-check | |
TOKEN=$(travis token --pro --skip-completion-check) | |
IFS=' ' read -r -a array <<< "$TOKEN" | |
TOKEN=${array[${#array[@]}-1]} | |
echo $TOKEN | |
echo $USER%2F$REPO | |
# BUILD_NUM=$(curl -s 'https://api.travis-ci.com/repo/3762845/builds' | grep -o '^\[{"id":[0-9]*,' | grep -o '[0-9]' | tr -d '\n') | |
# echo $BUILD_NUM | |
# curl -X POST https://api.travis-ci.com/builds/$BUILD_NUM/restart --header "Authorization: token "$TOKEN | |
# inspired from plume-lib, check arguments and add message | |
if [ $# -eq 5 ] ; then | |
MESSAGE=",\"message\": \"$5\"" | |
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=$TOKEN" | |
echo "BRANCH=$BRANCH" | |
echo "MESSAGE=$MESSAGE" | |
echo " https://api.travis-ci.com/repo/${USER}%2F${REPO}/requests " | |
# curl POST request content body | |
BODY="{ | |
\"request\": { | |
\"branch\":\"$BRANCH\" | |
$MESSAGE | |
}}" | |
echo $BODY | |
# 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.com/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment