Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins trigger a job from API

Tags:

html

jenkins

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 ?

like image 225
Thomas thomas Avatar asked Jan 06 '16 12:01

Thomas thomas


People also ask

How do I trigger a job in Jenkins?

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.

How to trigger a Jenkins job remotely using API?

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?

How do I generate a Jenkins API Token?

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.

How do I initiate a Jenkins build trigger?

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.

What are the available services in Jenkins?

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.


2 Answers

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:

enter image description here

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 :)

like image 78
Bruno Lavit Avatar answered Sep 22 '22 04:09

Bruno Lavit


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

like image 42
gajanan malvade Avatar answered Sep 19 '22 04:09

gajanan malvade