I m actually trying to make some API calls to jenkins to trigger a build.
Actually, I m facing two problems :
In a non-restricted environnement, where I don't need to be connected to trigger a job, I should send a POST request on :
http://address/job/jobId/build?delay=0sec
When making this, I get the following output telling me that I don't have a token value :
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 401 Invalid password/token for user: </title>
</head>
<body>
<h2>HTTP ERROR 401</h2>
<p>Problem accessing /job/Di%20Injector/build. Reason:
<pre> Invalid password/token for user: </pre>
</p>
<hr />
<i>
<small>Powered by Jetty://</small>
</i>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
</body>
</html>
Where could I get this information ?
In a restricted environnement, I have to login before make any thing like getting job, or trigger new build. The fact is that I don't know, and I can't find the url / verb / params to send to the server to get an access right.
Can you help me with this too ?
Developers can follow these three steps to implement a remote Jenkins build trigger: Create a Jenkins build job and enable the Trigger builds remotely checkbox. Provide an authentication token; This can be any text string of your choice. Invoke the Jenkins build URL to remotely trigger the build job.
Any Jenkins Job can be triggered remotely through an API call. We can extract the API output either in XML (SOAP) or in JSON (REST) format. 2.Under API token Click on the Show Legacy API Token. See images below. API: https://JENKINS_URL/job_path/buildwithParemters?
2 Answers 2. The solution is to generate an API token for your Jenkins user (or the Jenkins build user). Go to the following link for your user: Copy the user_id and token from this section: And launch the following command to trigger a build: I did a quick test on my https Jenkins server.
Jenkins provides a number of functionalities, however, what we are going to focus today on is Jenkins builds. Jenkins builds can be triggered in a couple of ways one popular way is when we push code to a git-hub repository, today we are going to see how to initiate a Jenkins build trigger by sending a request to our Jenkins server using python.
Services offered currently include: Jenkins API Client is an object oriented ruby wrapper project that consumes Jenkins’s JSON API and aims at providing access to all remote API Jenkins provides. It is available as a Rubygem and can be useful to interact with the Job, Node, View, BuildQueue, and System related functionalities.
The solution is to generate an API token for your Jenkins user (or the Jenkins build user).
Go to the following link for your user:
http://YOUR_JENKINS_URL/user/YOUR_JENKINS_USER_ID/configure
Copy the user_id and token from this section:
And launch the following command to trigger a build:
curl -X POST http://YOUR_JENKINS_USER_ID:YOUR_API_TOKEN@YOUR_JENKINS_URL/job/YOUR_JENKINS_JOB/build
I did a quick test on my https Jenkins server.
Without the token, I got this message:
Authentication required
<!--
You are authenticated as: anonymous
Groups that you are in:
Permission you need to have (but didn't): hudson.model.Hudson.Read
... which is implied by: hudson.security.Permission.GenericRead
... which is implied by: hudson.model.Hudson.Administer
-->
With the user_id/token, the build is OK with the curl command :)
If you are building with parameters then here it is:
Lets say your jenkins build accept two parameters and you want to make the api call using shell script:
You can put following command in shell script:
curl -X POST --data "package_name=ABC.tar.gz" --data "release_notes=none" --data "delay=0sec" https://USERID:TOKEN@JENKINS_URL/job/SOMETHING/SOMETHING/SOMETHING//buildWithParameters
Just use the address bar to make the link. Make sure the link ends with buildWithParameters
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With