Skip to content

Instantly share code, notes, and snippets.

@romain-grecourt
Created May 22, 2019 22:04
Show Gist options
  • Save romain-grecourt/05b4203793cba5877521cd71b14b42b3 to your computer and use it in GitHub Desktop.
Save romain-grecourt/05b4203793cba5877521cd71b14b42b3 to your computer and use it in GitHub Desktop.
Wercker trigger script
#!/bin/bash -e
trap 'echo "ERROR: Error occurred at ${BASH_SOURCE}:${LINENO} command: ${BASH_COMMAND}"' ERR
set -eo pipefail
usage(){
echo ""
echo "Usage: `basename ${0}` [OPTIONS] --owner=OWNER --app-name=APP_NAME --branch=BRANCH --pipeline=PIPELINE..."
echo ""
echo "Trigger wercker job(s)."
echo ""
echo "OWNER: Wercker username or organization (e.g. helidon)"
echo "APP_NAME: Wercker application name (e.g. helidon-site, helidon-build-tools, helidon)"
echo "BRANCH: Branch to trigger the job(s) against"
echo "PIPELINE: name of a pipeline to trigger"
echo ""
echo "OPTIONS:"
echo " --help print the usage and exit"
echo " --debug enable debug output"
echo ""
echo "ENVIRONMENT:"
echo " WERCKER_API_TOKEN: API token used to authenticate"
echo ""
echo "EXAMPLE:"
echo "`basename ${0}` --owner=helidon --app-name=helidon --branch=master build copyright checkstyle"
echo ""
}
SETUP_OK=true
echo ""
# parse command line arguments
_PIPELINES=()
for ((i=1;i<=${#*};i++))
{
arg="${!i}"
case "${arg}" in
"--owner="*)
readonly WERCKER_USERNAME="${arg#*=}"
;;
"--app-name="*)
readonly WERCKER_APPNAME="${arg#*=}"
;;
"--branch="*)
readonly BRANCH="${arg#*=}"
;;
"--debug")
readonly DEBUG="true"
;;
"--help")
usage
exit 0
;;
*)
if [[ $a =~ ^--([0-9a-zA-Z]+)=([0-9a-zA-Z]+)$ ]] ; then
echo "ERROR: Unknown option: ${arg}"
else
_PIPELINES=(${_PIPELINES[*]} ${arg})
fi
;;
esac
}
readonly PIPELINES=(${_PIPELINES[*]})
if [ -z "${WERCKER_USERNAME}" ] ; then
echo "ERROR: Missing required parameter --owner"
SETUP_OK=false
fi
if [ -z "${WERCKER_APPNAME}" ] ; then
echo "ERROR: Missing required parameter --app-name"
SETUP_OK=false
fi
if [ -z "${BRANCH}" ] ; then
echo "ERROR: Missing required parameter --branch"
SETUP_OK=false
fi
if [ ${#PIPELINES[*]} -eq 0 ] ; then
echo "ERROR: No pipeline specified (--pipeline)"
SETUP_OK=false
fi
if ! type curl > /dev/null 2>&1; then
echo "ERROR: curl not found in PATH"
SETUP_OK=false
fi
if ! type jq > /dev/null 2>&1; then
echo "ERROR: jq not found in PATH"
SETUP_OK=false
fi
if [ -z "${WERCKER_API_TOKEN}" ] ; then
echo "ERROR: WERCKER_API_TOKEN env variable not set"
SETUP_OK=false
fi
if ! ${SETUP_OK} ; then
usage
exit 1
fi
if [ "${DEBUG}" = "true" ] ; then
readonly CURL_OPTS="-vvv"
else
exec 2> /dev/null
fi
readonly API_URL="https://app.wercker.com/api/v3"
readonly AUTH_HEADER="Authorization: Bearer ${WERCKER_API_TOKEN}"
APPID=$(curl ${CURL_OPTS} -H "${AUTH_HEADER}" "${API_URL}/applications/${WERCKER_USERNAME}" | \
jq -r ".[] | select (.name == \"${WERCKER_APPNAME}\").id")
for pipeline in ${PIPELINES[*]} ; do
echo "INFO: Triggering pipeline ${pipeline} against branch ${BRANCH}"
pipelineId=$(curl ${CURL_OPTS} -H "${AUTH_HEADER}" "${API_URL}/workflows?applicationId=${APPID}" | \
jq -r "first(.[].items[].data | select (.targetName == \"${pipeline}\").pipelineId)")
url=$(curl ${CURL_OPTS} -H "${AUTH_HEADER}" -H "Content-Type: application/json" "${API_URL}/runs" \
-d "{ \"pipelineId\": \"${pipelineId}\", \"branch\": \"${BRANCH}\" }" | \
jq -r '.url')
echo "INFO: ${url}"
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment