Bird
Raised Fist0
Jenkinsdevops~5 mins

Keeping pipelines fast in Jenkins - Commands & Configuration

Choose your learning style10 modes available
Introduction
Long-running pipelines slow down development and waste resources. Keeping pipelines fast helps deliver changes quickly and keeps your team productive.
When your build takes too long and blocks other work
When you want to get quick feedback on code changes
When you want to save cloud or server costs by reducing pipeline runtime
When you want to run multiple tests in parallel to speed up validation
When you want to avoid unnecessary steps for small changes
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  options {
    timeout(time: 10, unit: 'MINUTES')
    skipStagesAfterUnstable()
  }
  stages {
    stage('Checkout') {
      steps {
        checkout scm
      }
    }
    stage('Build') {
      steps {
        sh 'make build'
      }
    }
    stage('Test') {
      parallel {
        stage('Unit Tests') {
          steps {
            sh 'make test-unit'
          }
        }
        stage('Integration Tests') {
          steps {
            sh 'make test-integration'
          }
        }
      }
    }
    stage('Deploy') {
      when {
        branch 'main'
      }
      steps {
        sh 'make deploy'
      }
    }
  }
}

This Jenkinsfile defines a pipeline with these speed-focused features:

  • timeout: Stops the pipeline if it runs longer than 10 minutes to avoid wasting time.
  • skipStagesAfterUnstable: Skips later stages if an earlier stage fails or is unstable, saving time.
  • parallel: Runs unit and integration tests at the same time to reduce total test time.
  • when branch 'main': Runs deployment only on the main branch to avoid unnecessary deploys on feature branches.
Commands
This command updates the Jenkins pipeline configuration with the new Jenkinsfile that includes speed optimizations.
Terminal
jenkins-jobs --conf jenkins.ini update my-pipeline
Expected OutputExpected
Job updated successfully: my-pipeline
Starts the pipeline build and waits for it to finish, so you can see the results immediately.
Terminal
jenkins-cli build my-pipeline -s
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage: Checkout [Pipeline] Checkout scm [Pipeline] stage: Build [Pipeline] sh [Pipeline] stage: Test [Pipeline] parallel [Pipeline] stage: Unit Tests [Pipeline] sh [Pipeline] stage: Integration Tests [Pipeline] sh [Pipeline] stage: Deploy [Pipeline] sh [Pipeline] End of Pipeline Finished: SUCCESS
→
-s - Waits for the build to complete before returning output
Shows the console output of the last pipeline run to verify stages ran quickly and in parallel.
Terminal
jenkins-cli console my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage: Checkout [Pipeline] Checkout scm [Pipeline] stage: Build [Pipeline] sh [Pipeline] stage: Test [Pipeline] parallel [Pipeline] stage: Unit Tests [Pipeline] sh [Pipeline] stage: Integration Tests [Pipeline] sh [Pipeline] stage: Deploy [Pipeline] sh [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: run tests in parallel and skip unnecessary steps to keep pipelines fast.

Common Mistakes
Running all tests sequentially without parallel stages
This makes the pipeline take longer than needed because tests wait for each other.
Use Jenkins 'parallel' to run independent tests at the same time.
Not setting timeouts on pipeline stages
A stuck or slow stage can block the entire pipeline and waste resources.
Use the 'timeout' option to stop stages that run too long.
Deploying on every branch instead of only main or release branches
Deploying unnecessary builds wastes time and can cause confusion.
Use 'when' conditions to deploy only on specific branches.
Summary
Use 'parallel' stages to run tests or tasks at the same time and reduce total pipeline time.
Set timeouts to stop slow or stuck stages and save resources.
Use conditions to skip deployment or other steps on branches where they are not needed.

Practice

(1/5)
1. Why is it important to keep Jenkins pipelines fast?
easy
A. To make the pipeline logs longer
B. To deliver code quickly and improve team efficiency
C. To increase the number of pipeline steps
D. To use more server resources

Solution

  1. Step 1: Understand pipeline speed impact

    Fast pipelines reduce wait time for developers and speed up delivery.
  2. Step 2: Identify the benefit of fast pipelines

    Faster pipelines improve team efficiency and allow quicker feedback.
  3. Final Answer:

    To deliver code quickly and improve team efficiency -> Option B
  4. Quick Check:

    Fast pipelines = faster delivery [OK]
Hint: Fast pipelines help teams deliver code quickly [OK]
Common Mistakes:
  • Thinking more steps make pipelines faster
  • Believing longer logs improve speed
  • Assuming more resource use equals speed
2. Which Jenkins pipeline syntax correctly sets a timeout of 10 minutes for a stage?
easy
A. timeout(time: 10, unit: 'MINUTES') { /* steps */ }
B. timeout(10 minutes) { /* steps */ }
C. timeout(10) { /* steps */ }
D. timeout(time=10, unit='minutes') { /* steps */ }

Solution

  1. Step 1: Recall Jenkins timeout syntax

    The correct syntax uses named parameters: time and unit.
  2. Step 2: Match correct syntax

    timeout(time: 10, unit: 'MINUTES') { /* steps */ } uses timeout(time: 10, unit: 'MINUTES') which is valid Groovy syntax.
  3. Final Answer:

    timeout(time: 10, unit: 'MINUTES') { /* steps */ } -> Option A
  4. Quick Check:

    timeout(time: 10, unit: 'MINUTES') = correct syntax [OK]
Hint: Use named parameters time and unit for timeout [OK]
Common Mistakes:
  • Using timeout(10 minutes) without quotes
  • Missing unit parameter
  • Using equal signs instead of colons
3. What will be the output of this Jenkins pipeline snippet?
pipeline {
  agent any
  stages {
    stage('Parallel') {
      parallel {
        stage('A') { steps { echo 'Running A' } }
        stage('B') { steps { echo 'Running B' } }
      }
    }
  }
}
medium
A. Runs stages A and B at the same time
B. Runs stages A and B one after another
C. Runs only stage A
D. Pipeline fails due to syntax error

Solution

  1. Step 1: Analyze parallel block syntax in Jenkins Declarative Pipeline

    The parallel block can contain nested stages inside it in Declarative Pipeline syntax.
  2. Step 2: Identify the behavior

    Stages A and B inside parallel run concurrently, so both run at the same time.
  3. Final Answer:

    Runs stages A and B at the same time -> Option A
  4. Quick Check:

    parallel runs stages concurrently [OK]
Hint: parallel block runs nested stages concurrently [OK]
Common Mistakes:
  • Thinking stages can not be nested inside parallel
  • Assuming parallel runs stages sequentially
  • Believing the syntax is invalid
4. You have a Jenkins pipeline that runs slowly. Which change will help fix this issue?
stage('Build') {
  steps {
    sh 'sleep 60'
  }
}
medium
A. Add more sleep commands to wait longer
B. Increase the timeout to 120 minutes
C. Change sleep to a non-blocking command
D. Remove the sleep command or reduce its duration

Solution

  1. Step 1: Identify slow step

    The 'sleep 60' command pauses the pipeline for 60 seconds, slowing it down.
  2. Step 2: Fix by removing or reducing sleep

    Removing or reducing sleep will speed up the pipeline.
  3. Final Answer:

    Remove the sleep command or reduce its duration -> Option D
  4. Quick Check:

    Removing delays = faster pipeline [OK]
Hint: Remove unnecessary waits like sleep to speed up pipelines [OK]
Common Mistakes:
  • Adding more sleep commands
  • Increasing timeout instead of fixing delay
  • Thinking sleep is non-blocking
5. You want to speed up a Jenkins pipeline by skipping tests when a commit message contains '[skip tests]'. Which pipeline step correctly implements this logic?
hard
A. if (env.GIT_COMMIT_MESSAGE.contains('[skip tests]')) { sh 'run-tests.sh' } else { echo 'Skipping tests' }
B. if (env.GIT_COMMIT_MESSAGE == '[skip tests]') { sh 'run-tests.sh' } else { echo 'Skipping tests' }
C. if (env.GIT_COMMIT_MESSAGE.contains('[skip tests]')) { echo 'Skipping tests'; } else { sh 'run-tests.sh' }
D. if (env.GIT_COMMIT_MESSAGE.contains('skip tests')) { sh 'run-tests.sh' } else { echo 'Skipping tests' }

Solution

  1. Step 1: Understand the condition to skip tests

    Tests should be skipped if commit message contains '[skip tests]'.
  2. Step 2: Match correct logic and commands

    if (env.GIT_COMMIT_MESSAGE.contains('[skip tests]')) { echo 'Skipping tests'; } else { sh 'run-tests.sh' } checks for '[skip tests]' and skips tests by echoing message; else runs tests.
  3. Final Answer:

    if (env.GIT_COMMIT_MESSAGE.contains('[skip tests]')) { echo 'Skipping tests'; } else { sh 'run-tests.sh' } -> Option C
  4. Quick Check:

    Correct condition and action = if (env.GIT_COMMIT_MESSAGE.contains('[skip tests]')) { echo 'Skipping tests'; } else { sh 'run-tests.sh' } [OK]
Hint: Use contains('[skip tests]') to conditionally skip tests [OK]
Common Mistakes:
  • Using equality instead of contains
  • Reversing the if-else logic
  • Missing brackets in string match
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