Open In App

How To Trigger Jenkins Builds Remotely And To Pass Parameters?

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Jenkins is an automation tool that is used to automate the build, test, and deploy stages of a software application. It allows the user to create a job and build the job by triggering remotely from any other external tool. Here in this guide, I will first discuss what is Jenkins. Then I will discuss why to trigger Jenkins to build a job remotely. After this, I will walk you through the different steps to trigger a Jenkins build remotely and pass parameters to it.

What Is Jenkins?

Jenkins is an open-source automation tool used to automate the build, test, and deploy stages of a software application. Jenkins helps developers to build and test and get build status more quickly. Basically, here, the moment new code is pushed to the version control system (like GitHub, GitLab, etc ) the pipeline automatically starts and Jenkins starts building and testing the code. If the new code is correctly integrating with the old code, then the , build status will be SUCCESS, or else the build status will be FAILURE. Due to this developers can write their code and fix the errors more quickly. Jenkins provides a very friendly user interface. It also provides many plugins which can be used in the automation of pipelines. Basically, Jenkins helps in automating the entire software development lifecycle. It automates the execution of various stages which helps in reducing any type of manual error and also helps in improving efficiency. With each new update, Jenkins is evolving and becoming more reliable for automating the software development process. Overall we can Jenkins has become a very important tool in the DevOps practice to accelerate the software development lifecycles.

Why To Trigger Jenkins Builds Remotely?

Trigger Jenkins build remotely means to start the building process of a job from any external tools scripts or browser. We can also pass custom parameters to the jobs while building them remotely. Triggering Jenkins build is important for the following reasons :

  • Trigger Jenkins build remotely will allow the users to start the pipeline on some specific event occurrence such as new commits in the git repo or any new pull requests.
  • This allows automation of the build process.
  • It can help users to integrate with external tools to start the build process. For example python scripts or bash scripts.
  • If someone wants to check whether their new code is working or not, they can just trigger the pipeline remotely.
  • Parameters can be passed remotely to customize the job building on Jenkins.

Pre-requisites

Before moving next section make sure that you have installed Jenkins on your system. If Jenkins is not installed then follow these detailed geeks for geeks articles to install Jenkins on your system.

Steps To Trigger Jenkins Builds Remotely And Pass Parameters

Step 1 : Open Jenkins dashboard and create a new item.

new-item

Step 2 : Give the new item a name and select pipeline .

item-name

Step 3 : Add some build parameters by selecting `This project is parameterized` . Here i have added two string type build parameters , one parameter is to give username who is building the job and second parameter is to give a filename .

params

Step 4 : Then select `Trigger build remotely` . This option will enable you to build the job by using the URL only other browsers or scripts .

token

Step 5 : Now write declarative Jenkins pipeline . Here in this pipeline i am printing the username who is building it and creating a file .

pipeline {
agent any

stages {
stage('Author name') {
steps {
echo "This job is executed by ${params.whoami}"
}
}
stage('Creating File') {
steps {
echo "Filename: ${params.filename}"
echo "Creating..."
sh "echo 'Hello geeks' > ${params.filename}"
echo "File successfully created."
}
}
}
}

pipeline-code

Step 6 : Now go to plugins . Install a plugin from available plugins called `Build Authorization Token Root`.

install-build-authenticator

Step 7 : Now use the URL provided below and open it in another browser . The moment you opened the URL , this will automatically trigger to start the build process of the job .

http://localhost:8080/buildByToken/buildWithParameters?token=54321&job=GFG-Demo-Job&whoami=pranit&filename=test.txt   (here you can give 
your own parameters whoami and filename )

open-url

You can see the building of job successfully completed on the Jenkins dashboard .

job-executed

Step 8 : Observe the outputs in console output .

console-output

In the workspace you will also observe that a filename `test.txt` is created .

filename

Conclusion

Here in this guide you have learned about what is Jenkins . Then you have learned why is it required to trigger a Jenkins build remotely . Then you have created a job on Jenkins . In this job you have created two build parameters and also created a authentication token . After this you have written the Jenkins declarative pipeline code and then installed a plugin which allow you to access the Jenkins server from any other browser . Finally you have used the URL to remotely start a build process of a job from another browser and also observed the pipeline output in the console output .

How to trigger Jenkins builds remotely and to pass parameters – FAQ’s

What is Jenkins build parameter ?

Jenkins build parameter is a value passed to a Jenkins job when a build process starts .

How to access parameters inside a Jenkins declarative pipeline ?

You can use `${params.<parameter-name>}` to access the parameter inside the Jenkins declarative pipeline .

What do you mean by trigger Jenkins build remotely ?

Trigger Jenkins build remotely means to start building a job from any external tools or browser or scripts.

What is the importance of trigger Jenkins build remotely ?

It is important to trigger Jenkins build remotely because it helps in enabling automation in building jobs by integrating with external tools like python scripts or bash scripts .

What is the difference between triggering Jenkins build remotely and scheduling Jenkins build ?

Basically triggering Jenkins build remotely means to start building a job from any other external tools or scripts , but scheduling Jenkins build means automatically start building a job at specific times or intervals .



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads