Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Jenkins REST API to resume a paused pipeline?

I have a Jenkins declarative pipeline with an input prompt.

stage('Approval') {
    when { 
        branch "qa"
    }
    input {
        message "Approve release?"
        ok "Yes"
        parameters {
            string(name: 'IS_APPROVED', defaultValue: 'Yes', description: 'Approve?')
        }
    }
    steps {
        echo "Commit to master"
    }
}

I have a 3rd party app that abstracts the use of Jenkins from business domain users. I want a button in the 3rd party app that when clicked, will approve the build for production release.

Is there a Jenkins REST API that I can call to provide the stage with input parameters and resume the build.

like image 591
Learner Avatar asked Feb 15 '18 03:02

Learner


1 Answers

Disclaimer: IMHO, this feature is poorly documented. I figured most of this out from a bunch of SO questions with partial answers and several blog articles, and very little from actual Jenkins docs. However, it seems to work nicely on Jenkins 2.73.2.

First, I think you need to add an id attribute to your input.

Then, you can send a POST request to one of these:

  • http://yourjenkins/job/${YOUR_PROJECT}/${BUILD_NUMBER}/input/${INPUT_ID}/abort

    This will cancel your job and ignore any parameter.

  • http://yourjenkins/job/${YOUR_PROJECT}/${BUILD_NUMBER}/input/${INPUT_ID}/proceedEmpty

    This will resume your job and ignore any parameter.

  • http://yourjenkins/job/${YOUR_PROJECT}/${BUILD_NUMBER}/input/${INPUT_ID}/submit

    This will resume your job, and you can send parameters. But:

    1. You will need to send a proceed parameter with the caption of the 'Proceed' button.
    2. You will need to send a json parameter with a URL-encoded JSON document with the form {"parameter":[{"name":"param1","value":"valueOfParam1"},{"name":"param2","value":"valueOfParam2"}]}, these will be your actual input parameters.
    3. If you fail to send a valid json parameter, your job will continue anyway, it just won't get any parameter.
    4. On success, this will return a '302 Found' and a redirection to user interface, which might interfere with your code and/or cause error handling issues.
  • http://yourjenkins/job/${YOUR_PROJECT}/${BUILD_NUMBER}/wfapi/inputSubmit

    This seems to be the right way. You need to send inputId and json (see previous point). On success, this will return a '200 OK' with an empty response. You can also check out /wfapi and /wfapi/nextPendingInputAction on a paused job for more information.

Keep in mind that you will need to send authentication credentials and CSRF token for each request. Also, for the use case you describe, you probably wouldn't need parameters for your input, but just the proceed/abort built-in action.

like image 156
willyjoker Avatar answered Nov 03 '22 05:11

willyjoker