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

Issue

I would like to be able to create 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

First, create a job using the UI. This job can be used to create a base config that can be used to create new jobs.

To retrieve the job config.xml that you made via the UI, to use for creating new jobs:

curl -X GET http://developer:developer@localhost:8080/job/test/config.xml -o mylocalconfig.xml

Obviously, replace:

  • developer:developer with your username:password
  • localhost:8080 with your Jenkins URL
  • test with the name of the job that you created via the UI

You can now modify this config file locally to suit for needs as required.

Once ready, to use this config to create a new job:

curl -s -XPOST 'http://developer:developer@localhost:8080/createItem?name=yourJobName' --data-binary @mylocalconfig.xml -H "Content-Type:text/xml"

Obviously, replace:

  • developer:developer with your username:password
  • localhost:8080 with your Jenkins URL
  • yourJobName with the name you would like to give the new job name

See Remote Access API for more.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.