Last active
June 4, 2018 14:39
-
-
Save cristianoliveira/f4bc5a7bb196dd0c9a6a0d407d70048d to your computer and use it in GitHub Desktop.
jenkins-deploy-command.sh
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 -l | |
set -e # fail on error | |
set -u # do not allow unset variables | |
# use the correct exit code, when piping commands, e.g. | |
# here you use curl ... | python... so if curl fails, python will still | |
# continure working | |
set -o pipefail | |
JENKINS_SERVER_URL='https://myjenkins.com' | |
USAGE=" | |
Jenkins commands | |
A helper command line for avoiding the ugly Jenkins UI when deploying. | |
AUTHENTICATION | |
In order to authenticate into Jenkins, you need to generate your API TOKEN on | |
USER> Configure> Show API Token | |
After set the environment variable JENKINS_USER_TOKEN: | |
export JENKINS_USER_TOKEN=myuser:mytoken | |
USAGE: | |
${0} [PROJECT_NAME] [ENVIRONMENT] [BRANCH default develop] | |
ex: | |
${0} ze-dashboard beta | |
${0} ze-dashboard theta | |
" | |
TRUE=0 | |
FALSE=1 | |
ITERATION_SLEEP=10 | |
GREEN='\033[0;32m' | |
RED='\033[0;31m' | |
NC='\033[0m' | |
# ARGUMENTS | |
PROJECT_NAME=${1?"${USAGE}"} | |
ENV=${2?"${USAGE}"} | |
BRANCH="${3:-develop}" | |
if [ -z "$JENKINS_USER_TOKEN" ]; then | |
echo "AUTHENTICATION ERROR | |
In order to authenticate into Jenkins generate your API TOKEN on | |
USER>Configure>Show API Token | |
After set the environment variable ${RED}JENKINS_USER_TOKEN${NC}: | |
export JENKINS_USER_TOKEN=myuser:mytoken\n | |
" | |
exit 1 | |
fi | |
JENKINS_JOB_URI="$JENKINS_SERVER_URL/job/$PROJECT_NAME" | |
function isBuilding { | |
building=$(curl --silent "$JENKINS_JOB_URI/lastBuild/api/json" --user $JENKINS_USER_TOKEN | \ | |
python -c "import sys, json; print json.load(sys.stdin).get('building')") | |
if [ "$building" = "True" ]; then | |
return $TRUE | |
fi | |
return $FALSE | |
} | |
function abort { | |
echo "💥 - Aborting..." | |
buildid=$(curl --silent "$JENKINS_JOB_URI/lastBuild/api/json" --user $JENKINS_USER_TOKEN | \ | |
python -c "import sys, json; print json.load(sys.stdin)['id']") | |
echo "Building $buildid will be aborted." | |
curl --silent -XPOST "$JENKINS_JOB_URI/$buildid/stop" --user $JENKINS_USER_TOKEN | |
} | |
function showConsoleStatus { | |
curl --silent "$JENKINS_JOB_URI/lastBuild/consoleText" --user $JENKINS_USER_TOKEN | |
} | |
function watchStatus { | |
while isBuilding; do | |
echo "👀 - Waiting for jenkins..." | |
sleep $ITERATION_SLEEP | |
showConsoleStatus | |
done | |
} | |
function deploy { | |
crumb=$(curl --silent "$JENKINS_SERVER_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,%22:%22,//crumb)" \ | |
--user $JENKINS_USER_TOKEN) | |
curl "$JENKINS_JOB_URI/buildWithParameters" \ | |
--user $JENKINS_USER_TOKEN \ | |
--data "name=BUILD_BRANCH&value=$BRANCH&name=ENVIRONMENT&value=$ENV&name=DEPLOY_HOST&value=&$crumb" --compressed | |
} | |
function git_commits { | |
echo "Fetching last commits:" | |
$(git fetch) | |
git log origin/$BRANCH -n 10 | |
} | |
if isBuilding; then | |
echo "The pipeline is been used..." | |
echo "${RED}Do you want to abort it? [y/n]${NC}" | |
read answer | |
if [ "$answer" != "${answer#[Yy]}" ] ;then | |
abort | |
fi | |
exit 0 | |
fi | |
echo "\n" | |
echo "${NC}Deploying ${RED}$BRANCH${NC} branch of ${RED}$PROJECT_NAME${NC} to ${RED}$ENV${NC}..." | |
echo "" | |
git_commits | |
echo "" | |
echo "Are you sure? [y/n]" | |
read answer | |
if [ "$answer" != "${answer#[Yy]}" ] ;then | |
echo "🚀 - Deploying..." | |
deploy | |
echo "Waiting for response..." | |
watchStatus | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment