0
0
Jenkinsdevops~30 mins

Script blocks for Groovy in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Script blocks for Groovy in Jenkins
📖 Scenario: You are automating a Jenkins pipeline. You want to use Groovy script blocks to run custom code inside the pipeline.
🎯 Goal: Build a Jenkins pipeline script that uses Groovy script blocks to define variables, add configuration, and print output.
📋 What You'll Learn
Create a Groovy script block with a variable
Add a configuration variable inside the script block
Use a script block to run a loop
Print the final output from the script block
💡 Why This Matters
🌍 Real World
Jenkins pipelines often use Groovy <code>script</code> blocks to run custom code for build, test, and deployment automation.
💼 Career
Knowing how to write Groovy <code>script</code> blocks in Jenkins is essential for DevOps engineers and automation specialists to customize CI/CD pipelines.
Progress0 / 4 steps
1
Create a Groovy script block with a variable
Write a Jenkins pipeline script block that defines a variable called message with the value "Hello from Groovy".
Jenkins
Need a hint?

Use def message = "Hello from Groovy" inside the script block.

2
Add a configuration variable inside the script block
Inside the existing script block, add a variable called count and set it to 3.
Jenkins
Need a hint?

Define def count = 3 inside the script block.

3
Use a script block to run a loop
Inside the script block, write a for loop that runs from 1 to count and appends " Run " plus the loop number to a variable called result. Initialize result as an empty string before the loop.
Jenkins
Need a hint?

Initialize result as an empty string, then use a for loop with i from 1 to count. Append to result inside the loop.

4
Print the final output from the script block
Add a println statement inside the script block to print the combined message: the message variable followed by the result variable.
Jenkins
Need a hint?

Use println(message + result) to print the combined string.