Skip to content

Instantly share code, notes, and snippets.

@timjrobinson
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timjrobinson/5eae031c796742269925 to your computer and use it in GitHub Desktop.
Save timjrobinson/5eae031c796742269925 to your computer and use it in GitHub Desktop.
Get git SHA of latest staging deploy from Jenkins and merge it into deploy-prod branch
#!/bin/bash
# This script requires the jq package to be installed (for JSON parsing)
JENKINS_API_CREDENTIALS=""
JENKINS_URL=""
get_last_build_info() {
local JOB_NAME=$1
local JS
local COMMAND="curl -fs -u $JENKINS_API_CREDENTIALS $JENKINS_URL/job/${JOB_NAME}/lastSuccessfulBuild/api/json"
JS=$($COMMAND)
local RC=$?
if [ $RC -ne 0 ]; then
echo "Failed to get build JSON" >&2
return 1;
fi
echo "$JS"
}
get_last_sha() {
local JOB_NAME=$1
local LAST_BUILD_INFO=$(get_last_build_info $JOB_NAME)
local TOTAL_ACTIONS=$(echo "$LAST_BUILD_INFO" | jq '.actions | length')
if [ $TOTAL_ACTIONS -lt 1 ]; then
echo "Could not find any actions for this build. Aborting." >&2
return 1
fi
local I
for ((I=0; I<$TOTAL_ACTIONS; I++)); do
local SHA=$(echo "$LAST_BUILD_INFO" | jq .actions[$I].lastBuiltRevision.SHA1)
if [ ${#SHA} -eq 42 ]; then
break
fi
done
if [ ${#SHA} -ne 42 ]; then
echo "Failed to get SHA" >&2
return 1
fi
echo "${SHA//\"}"
}
LATEST_SHA=`get_last_sha deploy-ide-staging`
if [ $? -ne 0 ]; then
echo "Failed to get latest SHA from deploy-ide-staging"
exit 1
fi
echo "Pushing latest tested SHA into deploy-prod"
git checkout $LATEST_SHA
git branch -f deploy-prod
git push origin +deploy-prod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment