0
0
Jenkinsdevops~5 mins

Why scripted pipelines offer flexibility in Jenkins - Why It Works

Choose your learning style9 modes available
Introduction
Sometimes you need your automation to do many different things depending on the situation. Scripted pipelines let you write code that can change how it works based on conditions, loops, or inputs. This helps you handle complex tasks in your software delivery process.
When your build steps need to change based on the branch or environment.
When you want to run different tests depending on the code changes.
When you need to add loops or conditional logic to your deployment process.
When you want to reuse code blocks or functions inside your pipeline.
When you want full control over the flow of your automation steps.
Config File - Jenkinsfile
Jenkinsfile
node {
    stage('Build') {
        echo 'Building the project'
    }
    stage('Test') {
        if (env.BRANCH_NAME == 'main') {
            echo 'Running full tests on main branch'
        } else {
            echo 'Running quick tests on feature branch'
        }
    }
    stage('Deploy') {
        for (int i = 1; i <= 3; i++) {
            echo "Deploy attempt ${i}"
        }
    }
}

This Jenkinsfile uses a scripted pipeline syntax.

node: Defines the agent where the pipeline runs.

stage: Groups steps for build, test, and deploy.

if: Runs different test commands based on the branch name.

for: Repeats the deploy step three times.

Commands
Check that Jenkins job builder is installed and ready to use.
Terminal
jenkins-jobs --version
Expected OutputExpected
1.0.0
Start the Jenkins pipeline job named 'my-pipeline' to run the scripted Jenkinsfile.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] node [Pipeline] stage [Pipeline] { (Build) Building the project [Pipeline] } [Pipeline] stage [Pipeline] { (Test) Running quick tests on feature branch [Pipeline] } [Pipeline] stage [Pipeline] { (Deploy) Deploy attempt 1 Deploy attempt 2 Deploy attempt 3 [Pipeline] } [Pipeline] End of Pipeline
View the console output of the last run of the 'my-pipeline' job to verify the steps executed.
Terminal
jenkins-cli console my-pipeline
Expected OutputExpected
Building the project Running quick tests on feature branch Deploy attempt 1 Deploy attempt 2 Deploy attempt 3
Key Concept

If you remember nothing else from this pattern, remember: scripted pipelines let you write flexible code with conditions and loops to control your automation flow.

Common Mistakes
Using declarative pipeline syntax inside a scripted pipeline block.
Declarative syntax is not valid inside scripted pipeline code and causes errors.
Write all steps using scripted pipeline Groovy code with node, stage, and control structures.
Not checking environment variables before using them in conditions.
If the variable is missing, the pipeline may fail or behave unexpectedly.
Always verify environment variables exist or provide defaults before using them.
Summary
Scripted pipelines use Groovy code to allow conditions, loops, and functions.
They give you full control over the order and logic of your automation steps.
You can change behavior based on branch, environment, or other inputs.