How to build a job using the REST API and cURL?

Issue

I would like to be able to build a job remotely using the Jenkins REST API and cURL. This provides a workaround for JENKINS-12543, that doesn’t require SSH Key Authentication.

Environment

  • CloudBees Jenkins Enterprise
  • Remote Access API
  • cURL

Resolution

Example build:

curl -X POST http://developer:developer@localhost:8080/job/test/build

Obviously, replace:

  • developer:developer with your username:password
  • localhost:8080 with your Jenkins URL
  • test with your job name

Example build with String parameter:

curl -X POST http://developer:developer@localhost:8080/job/test/build --data-urlencode json='{"parameter": [{"name":"paramA", "value":"123"}]}'

Obviously, replace:

  • developer:developer with your username:password
  • localhost:8080 with your Jenkins URL
  • test with your job name
  • paramA with your parameter name
  • 123 with your parameter value

See Remote Access API for more.

Have more questions? Submit a request

7 Comments

  • 0
    Avatar
    Gus Reiber

    Very helpful. Thx.

  • 1
    Avatar
    Miruna Popescu

    Hi,

    Is there any way to remotely build a job using the REST API and to add a custom header in the request? Before the build starts to verify this header and to continue or fail the build according to the value of the header?

    Thanks

  • 0
    Avatar
    Denys Digtiar

    Hi Miruna,

    What are you trying to achieve by this?

    Build Parameter support are explained. The endpoint requires authentication and, if enabled, the CSRF token. Therefore, I am not sure what other problem you are trying to solve.

  • 0
    Avatar
    Self Learning

    Hi,

    this works fine when we use plain (Without @, _, .) username.

    When use email id as username, it is not working

    any suggestions?

     

  • 1
    Avatar
    Denys Digtiar

    You can pass the authentication parameter using the curl's `--user` argument instead of embedding it into the URL, e.g. `--user developer:apiToken` or `--user developer` to get a password prompt see curl documentation for more details.

  • 0
    Avatar
    Nagesh Vishtamsetty

    How can I trigger a release build of a project, using cURL approach?

  • 0
    Avatar
    Alberto Martín

    I'm trying to achieve the same as Miruna.

    The thing is that, I need to trigger a build using the remote access API but, before starting the build, I need to verify that one of the parameters is the needed one and its value matches what I need. If not, I don't want to trigger the build. Is there a way to achieve this? Can I achieve this somehow configuring and/or defining parameters? 

    Edited by Alberto Martín
Please sign in to leave a comment.