Skip to content

Jenkins REST API

1. Run job

a. Job with no parameter

curl -XPOST <http://IP:8080/jenkins/job/plugin%20demo/build> --user admin:admin

b. Job with parameter

  • b-1. Using default parameters
curl -XPOST <http://IP:8080/jenkins/job/commandTest/buildWithParameters> --user admin:admin
  • b-2. Set parameter method 1
curl -XPOST <http://IP:8080/jenkins/job/commandTest/buildWithParameters> -d port=80
  • b-3. Set parameter method 2
curl -XPOST <http://IP:8080/jenkins/job/commandTest/buildWithParameters> -d port=80 \
    --data-urlencode json='"{\"parameter\": [{\"name\": \"port\", \"value\": \"80\"}]}"'
  • b-4. Multiple parameters
curl -XPOST <http://IP:8080/jenkins/job/commandTest/buildWithParameters> \
     -d param1=value1&param2=value

2. Create job

a. Directory needs to be created

  1. Create job directory: ~/.jenkins/jobs/jobfromcmd
  2. Create config.xml file (can be copied from other projects)
  3. Run command
curl -XPOST <http://IP:8080/jenkins/createItem?name=jobfromcmd> --user admin:admin \
     --data-binary "@config.xml" -H "Content-Type: text/xml"

b. Directory does not need to be created

  1. Create config.xml file (can be copied from other projects)
  2. Run command (in the same directory as config.xml)
curl -XPOST <http://IP:8080/jenkins/createItem?name=jobfromcmd> --user admin:admin \
     --data-binary "@config.xml" -H "Content-Type: text/xml"

3. Delete job

curl -XPOST <http://IP:8080/jenkins/job/jobfromcmd/doDelete>

4. Check job status

curl -XGET <http://IP:8080/job/JOB_NAME/lastBuild/api/json>

5. Disable job

curl -XPOST --data disable <http://IP:8080/job/JOBNAME/disable>

6. Get job build number

curl -XGET <http://IP:8080/job/JOB_NAME/lastBuild/buildNumber>

7. Get the build number of the most recent successful build

curl -XGET <http://IP:8080/job/JOB_NAME/lastStableBuild/buildNumber>

Q & A: RestAPI call with Error 403

Jenkins RestAPI call with Error 403 No valid crumb was included in the request

When making a RestAPI call to Jenkins, a crumb is required to authenticate the request. A crumb is a unique token generated by Jenkins to prevent cross-site request forgery (CSRF) attacks. CSRF attacks are malicious activities that exploit the user’s session to perform unauthorized actions on their behalf. Jenkins uses crumb tokens to ensure that the request is coming from a trusted source.

Method 1: Disable Cross-Site Request Forgery (CSRF) Protection

Under “Configure Global Security” in Jenkins, uncheck the “Prevent Cross Site Request Forgery exploits” option.

Method 2: Include CRUMB in Request

Obtain the user’s API token

http://Jenkins_IP:8080/user/USER/configure

Click “show API Token” to view the token, which we will call API_TOKEN.

Calculate the CRUMB

CRUMB=$(curl -s 'http://USER:API_TOKEN@Jenkins_IP:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')

Include the CRUMB information in the request

curl -X POST -H "$CRUMB" http://USER:API_TOKEN@Jenkins_IP:8080/reload

Reference

  • https://www.jenkins.io/doc/book/using/remote-access-api/
  • https://jenkinsapi.readthedocs.io/en/latest/
Feedback