Bird
Raised Fist0
Jenkinsdevops~5 mins

Why scripted pipelines offer flexibility in Jenkins - Why It Works

Choose your learning style10 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.

Practice

(1/5)
1. Why do scripted pipelines in Jenkins offer more flexibility compared to declarative pipelines?
easy
A. Because they only support predefined stages without customization.
B. Because they have a simpler syntax with fewer features.
C. Because they allow using loops, conditions, and custom error handling.
D. Because they automatically generate pipeline code without user input.

Solution

  1. Step 1: Understand scripted pipeline features

    Scripted pipelines allow writing code with loops, conditions, and error handling, giving more control.
  2. Step 2: Compare with declarative pipelines

    Declarative pipelines have a simpler, fixed structure and less flexibility for complex logic.
  3. Final Answer:

    Because they allow using loops, conditions, and custom error handling. -> Option C
  4. Quick Check:

    Scripted pipelines = flexible code control [OK]
Hint: Look for options mentioning loops and conditions for flexibility [OK]
Common Mistakes:
  • Confusing simplicity with flexibility
  • Thinking declarative pipelines allow all custom code
  • Assuming scripted pipelines auto-generate code
2. Which of the following is the correct syntax to start a scripted pipeline block in Jenkins?
easy
A. node {
B. stage {
C. pipeline {
D. scripted {

Solution

  1. Step 1: Identify scripted pipeline block syntax

    Scripted pipelines start with node { to define the workspace and steps.
  2. Step 2: Eliminate other options

    pipeline { is for declarative pipelines; stage { defines a stage; scripted { is invalid syntax.
  3. Final Answer:

    node { -> Option A
  4. Quick Check:

    Scripted pipeline starts with node block [OK]
Hint: Scripted pipelines start with 'node {' block [OK]
Common Mistakes:
  • Using 'pipeline {' which is declarative syntax
  • Confusing 'stage {' as pipeline start
  • Assuming 'scripted {' is valid syntax
3. What will be the output of this scripted pipeline snippet?
node {
  for (int i = 1; i <= 3; i++) {
    echo "Count: ${i}"
  }
}
medium
A. Count: 1\nCount: 2\nCount: 3
B. Count: 0\nCount: 1\nCount: 2
C. Syntax error due to for loop
D. No output because echo is invalid

Solution

  1. Step 1: Analyze the for loop range

    The loop runs from i=1 to i=3 inclusive, so it iterates 3 times.
  2. Step 2: Check the echo output each iteration

    Each iteration prints "Count: " followed by the current i value, producing lines for 1, 2, and 3.
  3. Final Answer:

    Count: 1\nCount: 2\nCount: 3 -> Option A
  4. Quick Check:

    Loop 1 to 3 prints counts 1,2,3 [OK]
Hint: Check loop start and end values carefully [OK]
Common Mistakes:
  • Starting loop at 0 instead of 1
  • Thinking echo is invalid in scripted pipeline
  • Assuming syntax error in for loop
4. Identify the error in this scripted pipeline snippet:
node {
  if (env.BRANCH_NAME == 'main')
    echo 'Deploying to production'
  else
    echo 'Deploying to staging'
}
medium
A. node block cannot contain if statements
B. Missing curly braces {} around if and else blocks
C. echo command cannot be used inside if
D. Incorrect use of env variable syntax

Solution

  1. Step 1: Check if-else syntax in scripted pipeline

    In scripted pipelines, if and else blocks must use curly braces {} when spanning multiple lines or statements.
  2. Step 2: Identify missing braces

    The snippet lacks braces around the echo statements, which can cause syntax errors or unexpected behavior.
  3. Final Answer:

    Missing curly braces {} around if and else blocks -> Option B
  4. Quick Check:

    If-else blocks need braces in scripted pipelines [OK]
Hint: Always use braces {} for if-else blocks in scripted pipelines [OK]
Common Mistakes:
  • Assuming env.BRANCH_NAME syntax is wrong
  • Thinking echo can't be inside if
  • Believing node block forbids if statements
5. You want to retry a build step up to 3 times if it fails, using a scripted pipeline. Which snippet correctly implements this retry logic?
hard
A. node { for (int i=0; i<3; i++) { sh 'run-build.sh' } }
B. node { while (true) { sh 'run-build.sh' } }
C. node { try { sh 'run-build.sh' } catch (err) { retry(3) } }
D. node { retry(3) { sh 'run-build.sh' } }

Solution

  1. Step 1: Understand Jenkins retry step

    The retry(n) { ... } block retries the enclosed steps up to n times on failure automatically.
  2. Step 2: Evaluate each option

    node { retry(3) { sh 'run-build.sh' } } correctly uses retry(3) around the build command. node { for (int i=0; i<3; i++) { sh 'run-build.sh' } } runs the command 3 times without retry on failure. node { try { sh 'run-build.sh' } catch (err) { retry(3) } } misuses retry inside catch. node { while (true) { sh 'run-build.sh' } } loops infinitely.
  3. Final Answer:

    node { retry(3) { sh 'run-build.sh' } } -> Option D
  4. Quick Check:

    Use retry(n) block for automatic retries [OK]
Hint: Use retry(n) block to retry steps automatically [OK]
Common Mistakes:
  • Using loops instead of retry block
  • Placing retry inside catch block incorrectly
  • Creating infinite loops without exit
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard