Last active
May 15, 2018 13:24
-
-
Save gorshunovr/626dcac6f4f8b7eeea31654bf2070e79 to your computer and use it in GitHub Desktop.
Tracks whether Jenkins SuperSeed has picked up new push in Gerrit, and if yes, then launches Jenkind 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 | |
## | |
# | |
# Script tracks whether Jenkins SuperSeed job has picked up new push in Gerrit, | |
# and if yes and Successful, then launches Jenkind Build. | |
# Launch example: | |
# $ git commit -a --amend --reuse-message HEAD && git review && ../jenkins_job_submit.sh | |
# | |
# SuperSeed: https://github.com/att-comdev/cicd/tree/master/cicd/SuperSeed | |
# | |
# fail on errors | |
set -e | |
gerrit_url=https://review.gerrithub.io | |
gerrit_user=user1 | |
gerrit_pass=pass1 | |
jenkins_url=http://ip:port | |
jenkins_user=user2 | |
jenkins_token=token2 | |
jenkins_form_data="param1=value1" | |
jenkins_job_path=jobpath | |
jenkins_job_name=jobname | |
changeID=$(git log -1 --format=%b | awk '/Change-Id/ {print $2}') | |
num_tries=10 # to check Gerrit | |
sleep_seconds=5 # between checks | |
function endProgressBar { | |
echo -n "]" | |
echo | |
} | |
function submitBuild { | |
crumb=$(curl -s -u${jenkins_user}:${jenkins_token} \ | |
${jenkins_url}/crumbIssuer/api/json | jq --raw-output '.crumb') | |
curl -s -u${jenkins_user}:${jenkins_token} \ | |
-F ${jenkins_form_data} \ | |
-X POST -H "Jenkins-Crumb:${crumb}" \ | |
${jenkins_url}/${jenkins_job_path}/${jenkins_job_name}/build && \ | |
echo "Launched Jenkins build" | |
} | |
echo -n "[" | |
for (( n=1; n<=${num_tries}; n++ )); do | |
# last message in gerrit's change log for your Change ID | |
lastMessage=$(curl -s -u$gerrit_user:$gerrit_pass \ | |
-H "Accept: application/json" \ | |
${gerrit_url}/a/changes/${changeID}/detail | tail -n+2 | jq '.messages|.[-1].message') | |
echo -n "#" | |
# SuperSeed messages contain either SUCCESS or FAILURE string | |
if [[ ${lastMessage} =~ SUCCESS ]]; then | |
endProgressBar | |
echo "Seed Build Successful" | |
submitBuild | |
break | |
elif [[ ${lastMessage} =~ FAILURE ]]; then | |
endProgressBar | |
echo "Seed Build Failure. Check logs: "${lastMessage} | |
exit 1 | |
else | |
if (( ${n}==${num_tries} )); then | |
endProgressBar | |
echo "Timed out waiting for SuperSeed message" | |
exit 1 | |
fi | |
fi | |
sleep ${sleep_seconds} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment