Last active
August 3, 2020 02:15
-
-
Save createvibe/0feefac90e5b3f7067e637e051a36406 to your computer and use it in GitHub Desktop.
Gitlab Jenkins Build Job
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
image: "docker:19.03.0-dind" | |
stages: | |
- build | |
variables: | |
JENKINS_USER: JENKINS_USER_NAME | |
JENKINS_TOKEN: JENKINS_USER_ACCESS_TOKEN | |
JENKINS_JOB_URL_DEV: JENKINS_JOB_URL | |
INSTANCE_IP: SOME_DEPLOY_INSTANCE | |
before_script: | |
- df | |
- cat /etc/resolv.conf | |
- cat /etc/hosts | |
- uname -a | |
- docker -v | |
- docker-compose --version | |
- git --version | |
jenkins-dev-build: | |
stage: 'build' | |
only: | |
refs: | |
- develop | |
- ci | |
tags: | |
- "jenkins" | |
script: | |
- echo "Starting jenkins job..." | |
# get the last build's json output | |
- lastjson=$(curl --silent --user $JENKINS_USER:$JENKINS_TOKEN $JENKINS_JOB_URL_DEV/lastBuild/api/json) | |
- checkjson=$lastjson | |
- lastjobid=$(echo $lastjson | perl -nle 'm/"id":"?(\d+)"?,?/; print $1') | |
- jobid=$lastjobid | |
# create a new jenkins job, with parameters | |
- curl -i -X POST --user $JENKINS_USER:$JENKINS_TOKEN $JENKINS_JOB_URL_DEV/buildWithParameters?BPM_BRANCH=$CI_COMMIT_REF_NAME&Instance_Ip=$INSTANCE_IP | |
- sleep 3 | |
# check to see if the job has been started by checking the response from lastBuild every 1 second | |
# continue until we get a different response than what we have in $checkjson | |
- > | |
while [ "$checkjson" == "$lastjson" ] | [ "$lastjobid" == "$jobid" ]; | |
do | |
echo "Waiting for new job to register..." | |
lastjson=$(curl --silent --user $JENKINS_USER:$JENKINS_TOKEN $JENKINS_JOB_URL_DEV/lastBuild/api/json) | |
jobid=$(echo $lastjson | perl -nle 'm/"id":"?(\d+)"?,?/; print $1') | |
sleep 1 | |
done | |
# fetch the json from the jobid we found | |
# repeat every 10 seconds until the job is finished (or has a different result value other than null) | |
# every iteration, fetch the console output for this job id and show the text not previously shown in the console | |
- echo "Jenkins job id ${jobid} is running..." | |
- echo "Waiting for jenkins job id ${jobid} output..." | |
- currentoutput="" | |
- loopval=null | |
- > | |
while [ "$loopval" == "null" ]; | |
do | |
sleep 10 | |
lastjson=$(curl --silent --user $JENKINS_USER:$JENKINS_TOKEN $JENKINS_JOB_URL_DEV/${jobid}/api/json) | |
loopval=$(echo $lastjson | perl -nle 'm/"result":"?([^,"\s]+)"?,?/; print $1') | |
outputidx=${#currentoutput} | |
currentoutput=$(curl --silent -i -X POST --user $JENKINS_USER:$JENKINS_TOKEN $JENKINS_JOB_URL_DEV/${jobid}/consoleText) | |
if [ ${#currentoutput} > $outputidx ]; then | |
echo -e "${currentoutput:$outputidx:$((${#currentoutput} - outputidx))}" | |
fi | |
done | |
- > | |
if [ "$loopval" != "SUCCESS" ]; then | |
echo "Invalid result from jenkins job id ${jobid}: ${loopval}" | |
echo "Build Failed!" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment