0

stage approval through API

I'm having a Jenkins pipeline consist of 3 stages. For promoting to next stage, it need someone to approve. The above pipeline consist of mulitple builds on progress. If someone want to promote 8 builds to Stage3, They need to open all 8 builds and need to click 'approve'. To avoid this repeated manual click, we thought to automate this by Jenkins API. By sending build number to shell script, the script have to promote the above 8 builds by API. Can I achieve this?

2 comments

  • 0
    Avatar
    Ray Kivisto

    Hi Jothi,

    Using an example pipeline with a human input step:
    https://github.com/rkivisto/pipeline-deployment-demo

    If I have this setup as a multibranch Pipeline on my instance, and build number 2 of the master branch is waiting for human input, I can approve this using a script:

    ./approveInput.sh https://jenkinsURL rkivisto myAPIToken pipeline-deployment-demo/job/master 2 Deploy

    Here is the script approveInput.sh:

    ```
    #!/bin/bash
    JENKINS_URL=$1
    USER=$2
    API_TOKEN=$3
    JOB_NAME=$4
    BUILD_ID=$5
    INPUT_ID=$6

    SUBMIT_CAPTION=Proceed
    JENKINS_CRUMB=$(wget -q --auth-no-challenge --user ${USER} --password ${API_TOKEN} --output-document - "${JENKINS_URL}/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)")
    curl -X POST --user ${USER}:${API_TOKEN} -H "${JENKINS_CRUMB}" -d proceed="${SUBMIT_CAPTION}" -d json="{}" "${JENKINS_URL}/job/${JOB_NAME}/${BUILD_ID}/input/${INPUT_ID}/submit"
    ```

    The INPUT_ID comes from your pipeline, in this case it has "id: 'Deploy'" as per https://github.com/rkivisto/pipeline-deployment-demo/blob/125969500cfdfa60e647f8bf9863a7bcb5e8164b/Jenkinsfile#L28

    Edited by Ray Kivisto
  • 0
    Avatar
    Jothi Laksh

    Thank you.

    It is working.

Please sign in to leave a comment.