0
0
Jenkinsdevops~5 mins

Script blocks for Groovy in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Script blocks in Groovy let you run multiple commands or complex logic inside Jenkins pipelines. They help you group steps that need to run together or use Groovy features not directly supported by pipeline syntax.
When you want to run several Groovy commands as one unit inside a Jenkins pipeline stage
When you need to use Groovy control structures like loops or conditionals inside your pipeline
When you want to define variables or functions inside your pipeline script
When you want to execute Groovy code that is not part of the declarative pipeline syntax
When you want to manipulate strings, lists, or maps using Groovy methods inside your pipeline
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                script {
                    def message = 'Hello from Groovy script block'
                    echo message
                    for (int i = 1; i <= 3; i++) {
                        echo "Counting: ${i}"
                    }
                }
            }
        }
    }
}

This Jenkinsfile defines a pipeline with one stage called 'Example'. Inside the steps, the script block allows running Groovy code.

The def message line creates a variable. The echo commands print messages to the Jenkins console. The for loop runs three times, showing how you can use Groovy control flow inside the script block.

Commands
Check that Jenkins Job Builder or Jenkins CLI is installed to run Jenkins pipelines.
Terminal
jenkins-jobs --version
Expected OutputExpected
jenkins-jobs 3.10.0
Trigger the Jenkins pipeline job named 'example-pipeline' that uses the Jenkinsfile with script blocks.
Terminal
jenkins-cli build example-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Example) [Pipeline] script [Pipeline] { [Pipeline] echo Hello from Groovy script block [Pipeline] echo Counting: 1 [Pipeline] echo Counting: 2 [Pipeline] echo Counting: 3 [Pipeline] } [Pipeline] } [Pipeline] End of Pipeline Finished: SUCCESS
View the console output of the last run of the 'example-pipeline' to verify the script block executed correctly.
Terminal
jenkins-cli console example-pipeline
Expected OutputExpected
Started by user admin Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Example) [Pipeline] script [Pipeline] { [Pipeline] echo Hello from Groovy script block [Pipeline] echo Counting: 1 [Pipeline] echo Counting: 2 [Pipeline] echo Counting: 3 [Pipeline] } [Pipeline] } [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from script blocks, remember: they let you run full Groovy code inside Jenkins pipelines to add flexibility beyond declarative syntax.

Common Mistakes
Trying to use Groovy code outside a script block in a declarative pipeline
Declarative pipelines only allow limited Groovy expressions outside script blocks, so the code will fail to parse or run.
Always wrap complex Groovy code inside a script { } block in declarative pipelines.
Using pipeline steps like 'echo' inside Groovy code without proper syntax
Pipeline steps must be called as methods inside script blocks, not as plain Groovy commands, or they will cause errors.
Call pipeline steps like echo as methods inside script blocks, e.g., echo 'message'.
Defining variables inside script blocks but trying to use them outside
Variables defined inside script blocks have local scope and are not accessible outside the block.
Define variables at the pipeline or stage level if you need to use them outside script blocks.
Summary
Use script blocks in Jenkins pipelines to run multiple Groovy commands together.
Script blocks allow loops, conditionals, and variable definitions inside pipelines.
Always wrap complex Groovy code inside script { } in declarative pipelines to avoid errors.