-->
  • Recent Articles

    How to trigger a Jenkins job from scripts

    How to trigger a Jenkins job from scripts?

    Jenkins

    When it comes to CI/CD automation, Jenkins Build Trigger using remote access API is a key ingredient. In this post we will describe how to create a Jenkins Job, configure API token, enable Jenkins REMOTE API and trigger it from remote. This API can be used as webhook in the Source Control Management Product like Jenkins. 

    You can create a "freestyle" or "Pipeline" Project as per your needs. Let's do it:

    Create a user:

    Login to Jenkins as Admin User -> Manage Jenkins -> Manage Users -> Create User

    Give the user name and password for the user which will be used as the Build User. 

    Create Authentication token :

    Lets create the Authentication token for the newly create Build user. Which will be used when we call the remote API.  Login as the user "jenkinsXXX" user and go to Configure as shown in the figure.

    In the configuration page, Go to the API Token Section and Add New Token



    Enter Name for the Token "token"  and click on Generate. Then the API token will be generated. Click on the copy button in the right and copy the token. 

    Now Login  as Admin user and go to  Manage Jenkins -> Configure Global Security -> Authorization -> Click on Add User or Group.

    Give the read access to the user as shown below. 


    Crete the API Token:

    In the Job Configuration page go to  the Build Triggers section

    Select the "Trigger builds remotely (e.g., from scripts)" and Enter some Authentication Token as shown below. Then click on the save button below.


    In my case the Authentication Token is "dataxxxx"

    As given the Remote API build trigger URL is "JENKINS_URL/job/my-controller-pipeline/build?token=dataxxxx

    Test the Remote API Build Trigger:

    We will use curl to test the build trigger:

    curl -I -u jenkinsXXX:11aa6524a15c315dffxxxxxxxxx7b69cxxxx "http://JENKINS_URL/job/my-controller-pipeline/build?token=dataxxxx"



    In the response you get a  HTTP 201  which represents a SUCCESS. The Job is created now you can go to the Jenkins Web page to check if the job is triggered or not.


    Jenkins remote build




    No comments