How to trigger a Jenkins Pipeline remotely

How to trigger a Jenkins Pipeline remotely

jenkins_pipeline_remote_trigger

Here in this article will see how we can remotely trigger a jenkins pipeline. We will be creating a very basic pipeline in jenkins and try to trigger it using the authentication token and the user credentials who has access to this pipeline job.

Test Environment

Fedora 37 workstation
Jenkins LTS

Remote Jenkins build trigger

Remote jenkins build trigger is a useful feature which can be used in the scripts to trigger a build remotely and also it can be used with version control system to trigger the remote build whenever a code change is committed. Authorization token is required for anybody who want to trigger a remtoe jenkins build.

If you are interested in watching the video. Here is the YouTube video on the same step by step procedure outlined below.

Procedure

Step1: Ensure Docker and Docker Compose installed and running

As a first step ensure that you have docker and docker compose installed and running as shown below. Please follow the offical documentation from Docker to install these components.

[admin@fedser jenkins-docker]$ docker -v
Docker version 23.0.4, build f480fb1

[admin@fedser jenkins-docker]$ docker-compose -v
docker-compose version 1.29.2, build 5becea4c

[admin@fedser jenkins-docker]$ sudo systemctl start docker.service

Step2: Create Docker Compose file for Jenkins service

Here we are going to use the following docker-compose file with volume mounts for jenkins_home for data persistence.

[admin@fedser jenkins-docker]$ cat docker-compose.yml 
version: "3"
services:

  jenkins:
    image: jenkins/jenkins:lts
    privileged: true
    user: root
    ports:
      - "8082:8080"
      - "2224:2224"
    volumes:
      - /apps/jenkins/user:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - jenkins2-net

networks:
  jenkins2-net:

Step3: Start Jenkins service

Let’s start our jenkins service and access it using the URL provided below. Please change the FQDN of the URL to your Host FQDN or localhost as per your convinence.

[admin@fedser jenkins-docker]$ docker-compose up -d

URL – http://fedser.stack.com:8082/login?from=%2F

Step4: Create a Pipeline

Create a new pipeline from jenkins portal and name it “remotepipeline” and in the pipeline definition select Pipeline script and copy the below script.

pipeline {
    agent any 
    stages {
        stage('Stop Application') { 
            steps {
                echo "Stop Application"
            }
        }
        stage('Reset Secrets') { 
            steps {
                echo "Reset Secrets " 
            }
        }
        stage('Test Connection') { 
            steps {
                echo "Test Connection" 
            }
        }
        stage('Redeploy Application') { 
            steps {
                echo "Redeploy Application" 
            }
        }
    }
}

Also enable the checkbox “Trigger builds remotely” from Build Triggers section. Once you enable this checkbox it will ask to provide the “Authentication Token”.

Let’s generate a random token of 16 characters using python as shown below and use it in the Authentication token field and save the pipeline.

>>> import random
>>> ''.join(random.choice('0123456789ABCDEF') for i in range(16))
'70F5D2D156F6314C'

Any user who has read access to this jenkins pipeline and with the valid authentication token would be able to build the pipeline by triggering it remotely

Step5: Trigger Pipeline remotely using curl

We cam trigger the pipeline using the following URL “JENKINS_URL/job/remotepipeline/build?token=TOKEN_NAME or /buildWithParameters?token=TOKEN_NAME”.

Let’s use curl to remotely trigger the jenkins pipeline as shown below by passing the token.

[admin@fedser jenkins-docker]$ curl -k http://fedser.stack.com:8082/job/remotepipeline/build?token=70F5D2D156F6314C

<html><head><meta http-equiv='refresh' content='1;url=/login?from=%2Fjob%2Fremotepipeline%2Fbuild%3Ftoken%3D70F5D2D156F6314C'/><script>window.location.replace('/login?from=%2Fjob%2Fremotepipeline%2Fbuild%3Ftoken%3D70F5D2D156F6314C');</script></head><body style='background-color:white; color:white;'>


Authentication required
<!--
-->

</body></html>

We have got a message that authentication is required. So we need to pass the credentials of the user who has access to this job. I am going to use my admin user who has access to all the jobs for this demo.

[admin@fedser jenkins-docker]$ curl -k http://fedser.stack.com:8082/job/remotepipeline/build?token=70F5D2D156F6314C -u admin:admin@1234

Now you can see from the above screenshot that the pipeline has been triggered. You can also check the console log for this pipeline which provides us with the information on the remote host that triggered this pipeline as shown below.

Hope you enjoyed reading this article. Thank you..