我正在尝试从Bamboo触发参数化的Jenkins构建,请执行以下操作:


POST-
http://jenkins-url.com/job/jobname/buildWithParameters?ENVIRONMENT=dev&APPLICATION=hello-world


但是我会立刻得到201,它告诉我该构建已创建。如何让这个请求等待并返回构建的成功状态,而不是一劳永逸?

显然,按照参数化远程触发插件的定义,这是可能的:



编辑:
最后创建了这个如果您需要它。
https://github.com/owenmorgan/jenkins-remote-builder

#1 楼

我创建了jenkins-remote-builder脚本,该脚本将按照您的远程构建进行操作。
有关它的更多详细信息(来自README.md):

示例
jenkins=https://user:pass@jenkins.mydomain.com:8080
jenkins_job=MyApp-Deploy

environment=dev
application=myapp
revision=9fd71f63b351b8208264daf86d292ced580a2f60

./jenkins_remote_trigger.sh \
            -h ${jenkins} \
            -j ${jenkins_job} \
            -p "ENVIRONMENT=${environment}&APPLICATION=${application}&REVISION=${revision}"

用法:
-h HOST     | --host=HOST                       Jenkins host
-j JOBNAME  | --jobname=test-build-job          The name of the jenkins job to trigger
-p JOBPARAM | --jobparam=environment=uat&test=1 Jenkins job paramiters
-q          | --quiet                           Don't output any status messages



评论


对其工作方式进行一些总结会更好;)

–滕西拜
17年3月16日在9:11

请查看我对您的答案(有趣的!)所做的修改,但是我认为确实存在删除(通过审核)的风险,因为这几乎是仅链接的答案(如@Tensibai的注释所指出的那样) )。

– Pierre.Vriens♦
17 Mar 20 '17在9:12

#2 楼

设置远程触发器时,必须单击“阻止,直到远程触发的项目完成其构建。”



评论


谢谢,我可能有错误的想法..该插件似乎是从jenkins本身触发远程构建的?在我的场景中,构建是从Bamboo的请求触发的,但是我们希望该请求能够保留到构建完成并返回构建状态。

– osmorgan
17 Mar 10 '17 at 9:14

啊,我现在明白了。不幸的是,我不认为API会那样工作,如果您查看Jenkins插件的开发方式,您会看到它触发了远程作业,然后轮询状态直到完成。

–特拉维斯·汤普森(Travis Thompson)
17 Mar 11 '17 at 17:47

谢谢,我最终制作了这个github.com/owenmorgan/jenkins-remote-builder

– osmorgan
17 Mar 12 '17 at 18:47



@osmorgan:您应该在答案中进行描述(带有一些摘要,而不仅仅是链接)并接受它。

–丹·科尼莱斯库(Dan Cornilescu)
17年3月14日在22:50

sheesh ..它在问题..并且在链接中有描述。在stackoverflow见。

– osmorgan
17年3月16日在9:02

#3 楼

如果您需要从CLI触发作业并等待其完成,则可以使用“ Jenkins CLI”(请参见此处)。

但是jenkins CLI不支持升级,因此我为他们提出了以下建议脚本:

#!/bin/bash
# Trigger a promotion and wait for its completion
#
# 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-
#
# The script is dependent on the current jenkins implementation of:
# - the promotion web page (links for triggering/re-executing the promotions)
# - the behaviour of the XML of the promotion status
#
# The behaviour of the job run status XML is:
#   - if the the promotion is not yet been triggered than the response is 404 not found
#   - is the the promotion has been triggered
#     - ... but it's still waiting for an executor:  the response is 404 not found or <duration> is empty
#     - ... and has started:                         <duration> is empty or 0
#     - ... and it's finished:                       <duration> is a non-zero number
#
#
#  run syntax:
#    ./trigger_promotion_and_wait_for_completion.sh \
#       <job_name> \
#       <job_run_number_to_promote> \
#       <promotion_name> \
#       <jenkins_user> \
#       <jenkins_pwd> \
#       <jenkins_url> \
#       <script_workspace_folder> \
#       '{"name": "prom_param_1", "value": "stringvalue" } , {"name": "prom_param_2", "value": true }'
#
#   example:
#    ./trigger_promotion_and_wait_for_completion.sh \
#       job1 \
#       22 \
#       promotion1 \
#       admin \
#       password \
#       http://localhost:8080/jenkins/ \
#       . \
#       '{"name": "prom_param_1", "value": "stringvalue" } , {"name": "prom_param_2", "value": true }'



set -uexo pipefail

#other debug options:
#PS4='+\t '
#set -v

JOB_NAME=""
JOB_RUN_NUMBER=""
DEPLOY_PROMOTION_NAME=""
BUILDER_USER=""
BUILDER_PASSWORD=""
JENKINS_URL=""
WORKSPACE_FOLDER=""
PROMOTION_ARGUMENTS=""

TIMEOUT=900


echo "retrieving the promotion nextBuildNumber (so we can poll the promotion status and check if it's finished)..."
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.*//g' )"




echo "running the promotion..."
echo 'only the first promotion can be triggered with "Approve", while subsequent promotions are triggered with "Re-execute promotion"'
echo 'so we check in the web page if the approve link is present'

#the link to search in the web page
PROMOTION_APPROVE_STRING="promotionProcess/${DEPLOY_PROMOTION_NAME}/promotionCondition/hudson.plugins.promoted_builds.conditions.ManualCondition/approve"

PROM_STATUS_URL="${JENKINS_URL}job/${JOB_NAME}/${JOB_RUN_NUMBER}/promotion/"

if curl -s -vvv -u${BUILDER_USER}:${BUILDER_PASSWORD}  "${PROM_STATUS_URL}" | grep "${PROMOTION_APPROVE_STRING}" ; then
    echo "The job has not yet been promoted, triggering it with 'Approve'"
    WEB_PATH="job/${JOB_NAME}/${JOB_RUN_NUMBER}/promotion/promotionProcess/${DEPLOY_PROMOTION_NAME}/promotionCondition/hudson.plugins.promoted_builds.conditions.ManualCondition/approve"
    SUBMIT="Approve"
else
    echo "The job has already been promoted, triggering it with 'Re-execute promotion'"
    WEB_PATH="job/${JOB_NAME}/${JOB_RUN_NUMBER}/promotion/${DEPLOY_PROMOTION_NAME}/build"
    SUBMIT="Re-execute+promotion"
fi


#note for the troubleshooting: in case the following curl fails then the error cause can be found near the string "stack trace"
CURL_OUTPUT="$(  curl -s -vvv -XPOST -u${BUILDER_USER}:${BUILDER_PASSWORD} "${JENKINS_URL}${WEB_PATH}"  \
                       --data 'json={
                                 "parameter": [
                                                '"${PROMOTION_ARGUMENTS}"'
                                              ]
                                    }&Submit='"${SUBMIT}" 2>&1 )"

if ( echo "${CURL_OUTPUT}" |  grep -P "< HTTP/1.1 5\d\d" ) || ( echo "${CURL_OUTPUT}" |  grep -P "< HTTP/1.1 4\d\d" ) ; then
  echo  'error in triggering the job/promotion! exiting...'
  exit 1
else
  echo  'curl good'
fi



echo "checking promotion status until promotion is finished"
FINISHED=no


INITIAL_TIME="$(date +%s)"
while [ "${FINISHED}" != "ok" ]
do
    sleep 2


    #checking if promotion is finished (we check the value of <duration> XML element in the job run status)
    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>//g' )"   || ERROR="yes"
    if [[ $ERROR == "yes" ]] ; then
      echo " the promotion has been queued but not yet started, waiting for it to start..."
      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"
      ERROR=""
      continue
    fi  ;   ERROR=""




    #we interrupt the polling of the job/promotion status if the promotion
    #  - is terminated
    #  - is taking too long (there is some problem)
    #(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 )

    POLLING_TIME="$(date +%s)"
    let "ELAPSED_TIME=POLLING_TIME-INITIAL_TIME"
    echo "ELAPSED_TIME=${ELAPSED_TIME}"

    if  (( ${ELAPSED_TIME} \> $TIMEOUT ))  ; then
      echo "error: the promotion has taken too long... exiting"
      exit 1
    fi

    if  [[ "${DURATION}" != "" ]] ; then
      re='^[0-9]+$'
      if [[ $DURATION =~ $re ]] ; then
        if (( "${DURATION}" \> "0" )) ; then
          FINISHED=ok
        else
          : #do nothing (the value of <duration> is 0 , that is the job/promotion has been started in a slave and is still running)
        fi
      else
        echo "error: the promotion duration is not a number. exiting..."
        exit 1
      fi
    else
      :  # the job/promotion has not yet started
    fi

    echo "waiting for the promotion to finish..."
done

echo "Promotion finished"



echo "Promotion output:"
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

cat ${WORKSPACE_FOLDER}/promotionOutput

if [[ ! "$(tail -n1 ${WORKSPACE_FOLDER}/promotionOutput)" =~ "SUCCESS"  ]] ; then
      echo "Promotion did not successfully terminate"
      exit 1
else
      echo "Promotion successfully terminated"
fi