Last active
May 3, 2021 17:37
-
-
Save jqtruong/e4502033f9e5b818ac876ae4a26995a6 to your computer and use it in GitHub Desktop.
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 | |
# | |
# | |
# Example usage: | |
# . jenkins-crop && post_build_job_with_parameters release 9.9.9 dt ff crop-9536-atats-validation | |
# | |
# The order of the parameters do not matter, except the comma-separated feature | |
# flags do need to come right after `ff`. | |
######### | |
# FUNCS # | |
######### | |
_build_query() { | |
local type=release \ | |
version=9.9.9 \ | |
downtime=false \ | |
feature_flags= \ | |
major= minor= patch= \ | |
params= q= | |
while [[ -n "$1" ]] | |
do | |
case "$1" in | |
release|hotfix) | |
type=$1 | |
;; | |
dt|downtime) | |
downtime=true | |
;; | |
ff|feature-flags) | |
shift | |
feature_flags=$1 | |
;; | |
*) | |
if [[ "$1" =~ [0-9]+\.[0-9]+\.[0-9]+ ]] | |
then version=$1 | |
else echo >&2 "unknown option '$1'" | |
fi | |
;; | |
esac | |
shift | |
done | |
# split dotted version into components | |
{ | |
OLD_IFS="$IFS" | |
IFS='.' | |
read major minor patch <<< "$version" | |
IFS="$OLD_IFS" | |
} | |
params=( | |
isDowntime="$downtime" | |
cropBranchType="$type" | |
majorVersion="$major" | |
minorVersion="$minor" | |
patchVersion="$patch" | |
featureFlags="$feature_flags" | |
) | |
# join params with '&' | |
q=$(OLD_IFS="$IFS" | |
IFS='&' | |
echo "${params[*]}" | |
IFS="$OLD_IFS") | |
echo "$q" | |
} | |
# HELPERS # | |
# this does not need my password, unless i can pull the console in, but | |
# typically would want to see it run live. | |
job_console() { | |
echo >&2 "job num: $1" | |
open -a "Google Chrome" "$base_url/$job_path/$1/console" | |
} | |
set_crumb_header() { | |
CRUMB_HEADER=$(curl -s -X GET \ | |
-u "$JENKINS_USER":"$JENKINS_PASSWD" \ | |
--cookie-jar "$cookie_jar" \ | |
"$base_url/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)") | |
} | |
# HTTP REQUESTS # | |
get_job() { | |
echo >&2 "job num: $1" | |
curl -s -X GET \ | |
-u "$JENKINS_USER":"$JENKINS_PASSWD" \ | |
--cookie "$cookie_jar" \ | |
-H "$CRUMB_HEADER" \ | |
"$base_url/$job_path/$1/api/json" | |
} | |
get_job_console() { | |
echo >&2 "job num: $1" | |
curl -s -S \ | |
-u "$JENKINS_USER":"$JENKINS_PASSWD" \ | |
--cookie "$cookie_jar" \ | |
-H "$CRUMB_HEADER" \ | |
"$base_url/$job_path/$1/logText/progressiveText" | |
} | |
get_jobs() { | |
curl -s -X GET \ | |
-u "$JENKINS_USER":"$JENKINS_PASSWD" \ | |
--cookie "$cookie_jar" \ | |
-H "$CRUMB_HEADER" \ | |
"$base_url/$job_path/api/json" | |
} | |
post_build_job_with_parameters() { | |
local query_params=$(_build_query "$@") | |
echo >&2 "QUERY PARAMS: $query_params" | |
curl -D- -X POST \ | |
-u "$JENKINS_USER":"$JENKINS_PASSWD" \ | |
--cookie "$cookie_jar" \ | |
-H "$CRUMB_HEADER" \ | |
"$base_url/$job_path/buildWithParameters?$query_params" | |
} | |
############# | |
# INIT VARS # | |
############# | |
CRUMB_HEADER= | |
JENKINS_USER=${JENKINS_USER-jtruong} | |
JENKINS_PASSWD=${JENKINS_PASSWD-"$(gpg -q --decrypt <path to password file>.gpg)"} | |
base_url="https://base.url" | |
echo >&2 "BASE_URL: $base_url" | |
# default job - temporarily - is the current branch i'm testing with. | |
job_path=$(echo "job/${1-jqt-test-crop-deployment-branch-creation}" | | |
# only spaces are escaped for now. it gets complicated quickly | |
# but a good answer is to use jq, but it might not be installed, | |
# so not sure if there will be any other characters to escaped, | |
# but if it's too much: jq -rn --arg x 'encode this' '$x|@uri'. | |
sed 's/ /%20/g') | |
echo >&2 "JOB_PATH: $job_path" | |
cookie_jar=$(mktemp /tmp/test-build-job.XXX) | |
echo >&2 "COOKIE_JAR: $cookie_jar" | |
######## | |
# MAIN # | |
######## | |
set_crumb_header | |
echo >&2 "CRUMB HEADER: $CRUMB_HEADER" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment