Use Jenkins REST API to resume a paused pipeline?
Asked Answered
A

1

12

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.

Arquebus answered 15/2, 2018 at 3:11 Comment(6)
This article should help: Waiting for Remote Systems in a Jenkins PipelineNaseberry
Is this you full pipeline?Jacqui
No, just a snippet.Arquebus
Ok, can't these inputs simply be job parameters that you'd use to start the job with?Jacqui
Approval is conditional. So some jobs may be auto-approved, others will require manual approval. Would a job parameter work in this case?Arquebus
@Arquebus Please share exact solution for your issue, If you did successGorga
H
22

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.

Hydroxyl answered 30/5, 2018 at 13:40 Comment(5)
This worked, wfapi is called when a button is pressed from Jenkins UI. I tried sending json in body but that din't work.Oldworld
@Oldworld - You can send json in the body and it will work. However, it's not plain json, but rather something like json={"parameter":[{"name":"param1","value":"valueOfParam1"},{"name":"param2","value":"valueOfParam2"}]}Flap
It's also worth mentioning that the input names can be retrieved from the /wfapi/nextPendingInputAction endpoint. This will give you the input parameter names that you need to include in your json={"parameter":[...]} name/value pairs.Flap
@Hydroxyl How to setup INPUT_ID in jenkinsfile ???Gorga
@Gorga In your jenkinsfile, you need to set an id attribute to the input declaration. Then you can use that identifier to build the request URL. The aforementioned INPUT_ID means nothing, it's just a placeholder variable with an obvious name, just for the example. If you only have one input in your pipeline, you can simply hardcode the actual ID instead of using a variable.Hydroxyl

© 2022 - 2024 — McMap. All rights reserved.