Guest User

Untitled

a guest
Aug 25th, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. jenkins=https://user:pass@jenkins.mydomain.com:8080
  2. jenkins_job=MyApp-Deploy
  3.  
  4. environment=dev
  5. application=myapp
  6. revision=9fd71f63b351b8208264daf86d292ced580a2f60
  7.  
  8. ./jenkins_remote_trigger.sh
  9. -h ${jenkins}
  10. -j ${jenkins_job}
  11. -p "ENVIRONMENT=${environment}&APPLICATION=${application}&REVISION=${revision}"
  12.  
  13. -h HOST | --host=HOST Jenkins host
  14. -j JOBNAME | --jobname=test-build-job The name of the jenkins job to trigger
  15. -p JOBPARAM | --jobparam=environment=uat&test=1 Jenkins job paramiters
  16. -q | --quiet Don't output any status messages
  17.  
  18. #!/bin/bash
  19. # Trigger a promotion and wait for its completion
  20. #
  21. # For triggering jobs jenkins cli is sufficient: https://support.cloudbees.com/hc/en-us/articles/228392127-How-to-wait-for-build-to-finish-when-triggering-from-CLI-
  22. #
  23. # The script is dependent on the current jenkins implementation of:
  24. # - the promotion web page (links for triggering/re-executing the promotions)
  25. # - the behaviour of the XML of the promotion status
  26. #
  27. # The behaviour of the job run status XML is:
  28. # - if the the promotion is not yet been triggered than the response is 404 not found
  29. # - is the the promotion has been triggered
  30. # - ... but it's still waiting for an executor: the response is 404 not found or <duration> is empty
  31. # - ... and has started: <duration> is empty or 0
  32. # - ... and it's finished: <duration> is a non-zero number
  33. #
  34. #
  35. # run syntax:
  36. # ./trigger_promotion_and_wait_for_completion.sh
  37. # <job_name>
  38. # <job_run_number_to_promote>
  39. # <promotion_name>
  40. # <jenkins_user>
  41. # <jenkins_pwd>
  42. # <jenkins_url>
  43. # <script_workspace_folder>
  44. # '{"name": "prom_param_1", "value": "stringvalue" } , {"name": "prom_param_2", "value": true }'
  45. #
  46. # example:
  47. # ./trigger_promotion_and_wait_for_completion.sh
  48. # job1
  49. # 22
  50. # promotion1
  51. # admin
  52. # password
  53. # http://localhost:8080/jenkins/
  54. # .
  55. # '{"name": "prom_param_1", "value": "stringvalue" } , {"name": "prom_param_2", "value": true }'
  56.  
  57.  
  58.  
  59. set -uexo pipefail
  60.  
  61. #other debug options:
  62. #PS4='+t '
  63. #set -v
  64.  
  65. JOB_NAME="${1}"
  66. JOB_RUN_NUMBER="${2}"
  67. DEPLOY_PROMOTION_NAME="${3}"
  68. BUILDER_USER="${4}"
  69. BUILDER_PASSWORD="${5}"
  70. JENKINS_URL="${6}"
  71. WORKSPACE_FOLDER="${7}"
  72. PROMOTION_ARGUMENTS="${8}"
  73.  
  74. TIMEOUT=900
  75.  
  76.  
  77. echo "retrieving the promotion nextBuildNumber (so we can poll the promotion status and check if it's finished)..."
  78. PROMOTION_RUN_NUMBER="$( curl -s -u${BUILDER_USER}:${BUILDER_PASSWORD} "${JENKINS_URL}job/${JOB_NAME}/promotion/process/${DEPLOY_PROMOTION_NAME}/api/xml" | grep -P '<nextBuildNumber>(.*)</nextBuildNumber' | sed -re 's/.*<nextBuildNumber>(.*)</nextBuildNumber.*/1/g' )"
  79.  
  80.  
  81.  
  82.  
  83. echo "running the promotion..."
  84. echo 'only the first promotion can be triggered with "Approve", while subsequent promotions are triggered with "Re-execute promotion"'
  85. echo 'so we check in the web page if the approve link is present'
  86.  
  87. #the link to search in the web page
  88. PROMOTION_APPROVE_STRING="promotionProcess/${DEPLOY_PROMOTION_NAME}/promotionCondition/hudson.plugins.promoted_builds.conditions.ManualCondition/approve"
  89.  
  90. PROM_STATUS_URL="${JENKINS_URL}job/${JOB_NAME}/${JOB_RUN_NUMBER}/promotion/"
  91.  
  92. if curl -s -vvv -u${BUILDER_USER}:${BUILDER_PASSWORD} "${PROM_STATUS_URL}" | grep "${PROMOTION_APPROVE_STRING}" ; then
  93. echo "The job has not yet been promoted, triggering it with 'Approve'"
  94. WEB_PATH="job/${JOB_NAME}/${JOB_RUN_NUMBER}/promotion/promotionProcess/${DEPLOY_PROMOTION_NAME}/promotionCondition/hudson.plugins.promoted_builds.conditions.ManualCondition/approve"
  95. SUBMIT="Approve"
  96. else
  97. echo "The job has already been promoted, triggering it with 'Re-execute promotion'"
  98. WEB_PATH="job/${JOB_NAME}/${JOB_RUN_NUMBER}/promotion/${DEPLOY_PROMOTION_NAME}/build"
  99. SUBMIT="Re-execute+promotion"
  100. fi
  101.  
  102.  
  103. #note for the troubleshooting: in case the following curl fails then the error cause can be found near the string "stack trace"
  104. CURL_OUTPUT="$( curl -s -vvv -XPOST -u${BUILDER_USER}:${BUILDER_PASSWORD} "${JENKINS_URL}${WEB_PATH}"
  105. --data 'json={
  106. "parameter": [
  107. '"${PROMOTION_ARGUMENTS}"'
  108. ]
  109. }&Submit='"${SUBMIT}" 2>&1 )"
  110.  
  111. if ( echo "${CURL_OUTPUT}" | grep -P "< HTTP/1.1 5dd" ) || ( echo "${CURL_OUTPUT}" | grep -P "< HTTP/1.1 4dd" ) ; then
  112. echo 'error in triggering the job/promotion! exiting...'
  113. exit 1
  114. else
  115. echo 'curl good'
  116. fi
  117.  
  118.  
  119.  
  120. echo "checking promotion status until promotion is finished"
  121. FINISHED=no
  122.  
  123.  
  124. INITIAL_TIME="$(date +%s)"
  125. while [ "${FINISHED}" != "ok" ]
  126. do
  127. sleep 2
  128.  
  129.  
  130. #checking if promotion is finished (we check the value of <duration> XML element in the job run status)
  131. ERROR="" ; DURATION="$(curl -s -u${BUILDER_USER}:${BUILDER_PASSWORD} "${JENKINS_URL}job/${JOB_NAME}/${JOB_RUN_NUMBER}/promotion/${DEPLOY_PROMOTION_NAME}/promotionBuild/${PROMOTION_RUN_NUMBER}/api/xml" | grep -Po '<duration>.*</duration>' | sed -re 's/<duration>(.*)</duration>/1/g' )" || ERROR="yes"
  132. if [[ $ERROR == "yes" ]] ; then
  133. echo " the promotion has been queued but not yet started, waiting for it to start..."
  134. curl -s -u${BUILDER_USER}:${BUILDER_PASSWORD} "${JENKINS_URL}job/${JOB_NAME}/${JOB_RUN_NUMBER}/promotion/${DEPLOY_PROMOTION_NAME}/promotionBuild/${PROMOTION_RUN_NUMBER}/api/xml"
  135. ERROR=""
  136. continue
  137. fi ; ERROR=""
  138.  
  139.  
  140.  
  141.  
  142. #we interrupt the polling of the job/promotion status if the promotion
  143. # - is terminated
  144. # - is taking too long (there is some problem)
  145. #(in the XML of the job run status the <duration> XML element value is initially empty, than it is 0, and eventually is the number of seconds of the run duration )
  146.  
  147. POLLING_TIME="$(date +%s)"
  148. let "ELAPSED_TIME=POLLING_TIME-INITIAL_TIME"
  149. echo "ELAPSED_TIME=${ELAPSED_TIME}"
  150.  
  151. if (( ${ELAPSED_TIME} > $TIMEOUT )) ; then
  152. echo "error: the promotion has taken too long... exiting"
  153. exit 1
  154. fi
  155.  
  156. if [[ "${DURATION}" != "" ]] ; then
  157. re='^[0-9]+$'
  158. if [[ $DURATION =~ $re ]] ; then
  159. if (( "${DURATION}" > "0" )) ; then
  160. FINISHED=ok
  161. else
  162. : #do nothing (the value of <duration> is 0 , that is the job/promotion has been started in a slave and is still running)
  163. fi
  164. else
  165. echo "error: the promotion duration is not a number. exiting..."
  166. exit 1
  167. fi
  168. else
  169. : # the job/promotion has not yet started
  170. fi
  171.  
  172. echo "waiting for the promotion to finish..."
  173. done
  174.  
  175. echo "Promotion finished"
  176.  
  177.  
  178.  
  179. echo "Promotion output:"
  180. curl -s -u${BUILDER_USER}:${BUILDER_PASSWORD} "${JENKINS_URL}job/${JOB_NAME}/${JOB_RUN_NUMBER}/promotion/${DEPLOY_PROMOTION_NAME}/promotionBuild/${PROMOTION_RUN_NUMBER}/consoleText" > ${WORKSPACE_FOLDER}/promotionOutput
  181.  
  182. cat ${WORKSPACE_FOLDER}/promotionOutput
  183.  
  184. if [[ ! "$(tail -n1 ${WORKSPACE_FOLDER}/promotionOutput)" =~ "SUCCESS" ]] ; then
  185. echo "Promotion did not successfully terminate"
  186. exit 1
  187. else
  188. echo "Promotion successfully terminated"
  189. fi
Add Comment
Please, Sign In to add comment