How to trigger a Jenkins job remotely from Python script

In this article, I will explain how you can trigger a Jenkins job remotely from a Python script. There would be situations when you want to run a particular Jenkins job from a Python scripts remotely. The steps are very easy. Let’s get started.

Step 1: Setup the Jenkins Job to be triggered remotely.

Go to the Jenkins job and Click  Configure   button.

Under  Build Triggers  Section, check box for  Trigger builds remotely (e.g., from scripts)   and add an  Authentication Token . An authentication token is required to make sure not everybody can trigger your job. You can generate a random token from here.

In the above screenshot, you can see the URL to trigger the build is shown in the descriptions. We will use this url in the Python script to trigger the job.

Step 2: Write Python script to trigger job remotely.

I have already written the Python script to trigger a Jenkins job remotely. Here you go.

#Python2 and 3 compatible

import requests

jenkins_job_name = "test"              
Jenkins_url = "http://localhost:8081"
jenkins_user = "nijil"
jenkins_pwd = "123456"
buildWithParameters = True
jenkins_params = {'token': 'e0EzNMlS53', 
                  'result1': 'success'}

	auth= (jenkins_user, jenkins_pwd)
	crumb_data= requests.get("{0}/crumbIssuer/api/json".format(Jenkins_url),auth = auth,headers={'content-type': 'application/json'})
	if str(crumb_data.status_code) == "200":

		if buildWithParameters:
			data = requests.get("{0}/job/{1}/buildWithParameters".format(Jenkins_url,jenkins_job_name),auth=auth,params=jenkins_params,headers={'content-type': 'application/json','Jenkins-Crumb':crumb_data.json()['crumb']})
			data = requests.get("{0}/job/{1}/build".format(Jenkins_url,jenkins_job_name),auth=auth,params=jenkins_params,headers={'content-type': 'application/json','Jenkins-Crumb':crumb_data.json()['crumb']})

		if str(data.status_code) == "201":
		 	print ("Jenkins job is triggered")
		 	print ("Failed to trigger the Jenkins job")

		print("Couldn't fetch Jenkins-Crumb")

except Exception as e:
	print ("Failed triggering the Jenkins job")
	print ("Error: " + str(e))

Just copy/paste the code to your box and give proper values to the variables given below.

 jenkins_job_name  =  Give your Jenkins job name as a string

 Jenkins_url  = Give your Jenkins URL as a string.

 jenkins_user  = Give the Jenkins username as string

 jenkins_pwd  = Give the Jenkins password as string

 buildWithParameters  = Give boolean True if your Jenkins job has parameters, else False.

 jenkins_params  = This variable accepts dictionary. The value of  token  should be the authentication token you generated in the step1. If your Jenkins job is parameterised, add the parameters as key/value in this dictionary.

If you also want to check the build status of the Jenkins job using Python script, read this article.


Congratulations, you can now trigger your Jenkins from a Python script! Thanks for the time taken to read my blog.

Subscribe to this blog so that you don’t miss out anything useful   (Checkout Right Sidebar for the Subscription Form and Facebook follow button)  . Please also put your thoughts as comments .

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top