How to Run Jenkins Job From Groovy Script?

10 minutes read

To run a Jenkins job from a Groovy script, you can use the Jenkins API to trigger a build on a specific job. You can do this by using the Jenkins API endpoint for triggering a build, passing in the job name and any parameters that the job may require. You can also authenticate with Jenkins using API tokens or username/password credentials to ensure proper authorization for triggering the job. Once you have the necessary information and authentication set up, you can use a Groovy script to make an HTTP POST request to the Jenkins API endpoint for triggering a build on the desired job. This will start the job and execute the defined tasks within it.

Best Groovy Books to Read of May 2024

1
Groovy in Action: Covers Groovy 2.4

Rating is 5 out of 5

Groovy in Action: Covers Groovy 2.4

2
Groovy Programming: An Introduction for Java Developers

Rating is 4.9 out of 5

Groovy Programming: An Introduction for Java Developers

3
Programming Groovy: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

Rating is 4.8 out of 5

Programming Groovy: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

4
Programming Groovy 2: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

Rating is 4.7 out of 5

Programming Groovy 2: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

5
Mastering GROOVY: A Comprehensive Guide To Learn Groovy Programming

Rating is 4.6 out of 5

Mastering GROOVY: A Comprehensive Guide To Learn Groovy Programming

6
Making Java Groovy

Rating is 4.5 out of 5

Making Java Groovy

7
Mastering Groovy Programming: Essential Techniques

Rating is 4.4 out of 5

Mastering Groovy Programming: Essential Techniques

8
Learning Groovy 3: Java-Based Dynamic Scripting

Rating is 4.3 out of 5

Learning Groovy 3: Java-Based Dynamic Scripting

9
Groovy 2 Cookbook

Rating is 4.2 out of 5

Groovy 2 Cookbook


How to trigger a Jenkins job using Groovy script?

To trigger a Jenkins job using a Groovy script, you can use the Jenkins Script Console. Here is an example of how you can trigger a Jenkins job using Groovy script:

  1. Open the Jenkins Script Console by navigating to Manage Jenkins -> Script Console.
  2. In the Script Console, enter the following Groovy script code to trigger a Jenkins job by name:
1
2
3
def jobName = "Your_Job_Name"
def job = Jenkins.instance.getItem(jobName)
def build = job.scheduleBuild2(0)


  1. Replace "Your_Job_Name" with the actual name of the Jenkins job that you want to trigger.
  2. Click on the Run button to execute the Groovy script.


The Groovy script above retrieves the Jenkins job by name, schedules a build for the job, and triggers the build. It is a simple and efficient way to trigger a Jenkins job using a Groovy script.


How to monitor the progress of a Jenkins job running from a Groovy script?

If you are running a Jenkins job from a Groovy script and want to monitor its progress, you can use the Jenkins API to retrieve information about the job status.


Here is an example of how you can monitor the progress of a Jenkins job running from a Groovy script:

  1. Get the Jenkins job object by its name:
1
def job = Jenkins.instance.getItemByFullName('YourJobName')


  1. Get the last build of the job:
1
def lastBuild = job.getLastBuild()


  1. Check the status of the last build:
1
def buildStatus = lastBuild.getResult()


  1. Get more detailed information about the build:
1
def buildInfo = lastBuild.details()


  1. Print the status and other information about the build:
1
2
println "Build status: ${buildStatus}"
println "Build info: ${buildInfo}"


By using these steps in your Groovy script, you can monitor the progress of a Jenkins job and retrieve information about its status, duration, and other details. This can help you track the progress of the job and take appropriate actions based on its status.


How to schedule Jenkins jobs with Groovy scripts?

To schedule Jenkins jobs with Groovy scripts, you can use the cron syntax to define a schedule for when the job should run. Here's a step-by-step guide on how to do this:

  1. Go to your Jenkins dashboard and navigate to the job you want to schedule.
  2. Click on "Configure" to go to the job's configuration page.
  3. Scroll down to the "Build Triggers" section and select "Build periodically."
  4. In the "Schedule" field, enter a cron expression that defines when the job should run. Here's an example of a cron expression that runs the job every day at 10:00 AM:
1
H 10 * * *


  1. If you want to use a Groovy script to set the cron expression dynamically, you can add a "Execute Groovy script" build step to your job. Here's an example of how you can set the cron expression using Groovy:
1
2
3
4
5
def job = hudson.model.Hudson.instance.getItem('YourJobName')
def cron = 'H 10 * * *' // Set your desired cron expression here
job.getTriggers().clear()
job.addTrigger(new hudson.triggers.TimerTrigger(cron))
job.save()


  1. Save your changes and run the job to check if it's scheduled to run at the specified time.


By following these steps, you can schedule Jenkins jobs with Groovy scripts using cron expressions to define when the job should run.


What are the advantages of running Jenkins jobs from Groovy scripts?

  1. Automation: Groovy scripts allow for automating repetitive tasks in Jenkins, reducing the need for manual intervention and speeding up the process.
  2. Flexibility: Groovy scripts offer a high level of flexibility in customizing Jenkins job configurations and workflows, enabling developers to tailor the job execution to specific requirements.
  3. Reusability: Groovy scripts can be reused across multiple Jenkins jobs, saving time and effort in recreating the same configurations for each job.
  4. Testing: Groovy scripts can be unit tested to ensure reliability and consistency in job execution, providing more confidence in the automation process.
  5. Integration: Groovy scripts can easily integrate with other tools and systems, allowing for a smooth workflow between Jenkins and various external services.


What is the difference between running Jenkins jobs manually and using Groovy scripts?

Running Jenkins jobs manually involves a user triggering and executing the job through the Jenkins web interface or via CLI commands. On the other hand, using Groovy scripts involves programmatically configuring and triggering Jenkins jobs through scripts written in the Groovy scripting language.


The main differences between running Jenkins jobs manually and using Groovy scripts include:

  1. Manual Execution: When running Jenkins jobs manually, a user initiates the job execution by clicking on a button, entering parameters, or selecting options in the Jenkins interface. With Groovy scripts, the job execution can be automated and triggered based on specific conditions or events defined in the script.
  2. Job Configuration: When running Jenkins jobs manually, users can customize job configurations and settings interactively through the Jenkins UI. Groovy scripts, on the other hand, allow for advanced job configuration and automation by programmatically defining job parameters, build steps, triggers, and other job-related details.
  3. Reusability: Groovy scripts offer the advantage of reusability by enabling users to create and maintain reusable scripts that can be applied to multiple Jenkins jobs. This helps in standardizing job configurations and enhancing the efficiency of managing Jenkins jobs.
  4. Automation: Groovy scripts enable users to automate various aspects of Jenkins job management, such as job creation, configuration updates, and job triggering. This automation can simplify repetitive tasks and streamline the execution of Jenkins jobs.


In summary, while manual execution of Jenkins jobs provides a straightforward and user-friendly approach, using Groovy scripts offers enhanced customization, automation, and reusability benefits for managing Jenkins jobs more efficiently.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To integrate SonarQube with Jenkins, follow these steps:Install and configure Jenkins on your system. Download and install the SonarQube scanner plugin in Jenkins. This plugin allows Jenkins to run the SonarQube analysis during the build process. Set up SonarQ...
The Groovy GDK (Groovy Development Kit) provides a set of convenience methods and enhancements to the standard Java libraries. To use the Groovy GDK, you need to import the GDK classes into your Groovy script or application.You can import the GDK classes by us...
To integrate Groovy with Java, you can leverage the interoperability features provided by both languages. Groovy can seamlessly work with Java libraries and frameworks, allowing you to use existing Java code in your Groovy projects.One way to integrate Groovy ...