Bird
Raised Fist0
Jenkinsdevops~10 mins

Idempotent pipeline steps in Jenkins - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a Jenkins pipeline step that runs a shell command idempotently.

Jenkins
stage('Build') {
  steps {
    sh '[1]'
  }
}
Drag options to blanks, or click blank then click option'
Aecho 'Building...'
Brm -rf *
Cexit 1
Dmkdir /tmp
Attempts:
3 left
💡 Hint
Common Mistakes
Using commands that delete or modify files like 'rm -rf *' which are not idempotent.
Using commands that exit with error causing pipeline failure.
2fill in blank
medium

Complete the code to check if a directory exists before creating it, ensuring idempotency.

Jenkins
stage('Prepare') {
  steps {
    sh 'if [ ! -d /app ]; then [1] /app; fi'
  }
}
Drag options to blanks, or click blank then click option'
Amkdir
Brm -rf
Ctouch
Dcd
Attempts:
3 left
💡 Hint
Common Mistakes
Using rm -rf which deletes files and is not idempotent.
Using touch which creates files, not directories.
3fill in blank
hard

Fix the error in the Jenkins pipeline step to make the shell command idempotent.

Jenkins
stage('Deploy') {
  steps {
    sh '[1]'
  }
}
Drag options to blanks, or click blank then click option'
Arm -rf /var/www/html/*
Brsync -a /src/ /var/www/html/
Ccp -r /src/* /var/www/html/
Dexit 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using rm -rf which deletes all files and is not idempotent.
Using cp -r which copies files but may overwrite unnecessarily.
4fill in blank
hard

Fill both blanks to create an idempotent Jenkins pipeline step that installs a package only if missing.

Jenkins
stage('Install') {
  steps {
    sh 'if ! dpkg -l | grep -q [1]; then sudo [2] [1]; fi'
  }
}
Drag options to blanks, or click blank then click option'
Acurl
Bapt-get install
Cgit
Dvim
Attempts:
3 left
💡 Hint
Common Mistakes
Using commands that do not check package presence before installing.
Using package names that are not installed by apt-get.
5fill in blank
hard

Fill all three blanks to create an idempotent Jenkins pipeline step that updates a config file only if a setting is missing.

Jenkins
stage('Configure') {
  steps {
    sh '''
      if ! grep -q '[1]' /etc/app/config.conf; then
        echo '[1]=[2]' >> [3]
      fi
    '''
  }
}
Drag options to blanks, or click blank then click option'
Amax_connections
B100
C/etc/app/config.conf
Dtimeout
Attempts:
3 left
💡 Hint
Common Mistakes
Appending the setting without checking if it exists, causing duplicates.
Using wrong file path or key names.

Practice

(1/5)
1. What does it mean for a Jenkins pipeline step to be idempotent?
easy
A. It always creates new resources every time it runs
B. It can run multiple times without changing the result or causing errors
C. It runs only once and then stops the pipeline
D. It requires manual approval before running again

Solution

  1. Step 1: Understand idempotency in pipelines

    Idempotent means running a step many times does not change the outcome after the first run.
  2. Step 2: Compare options with definition

    Only It can run multiple times without changing the result or causing errors describes this behavior correctly; others describe non-idempotent or manual steps.
  3. Final Answer:

    It can run multiple times without changing the result or causing errors -> Option B
  4. Quick Check:

    Idempotent = safe to repeat without side effects [OK]
Hint: Idempotent means safe to run repeatedly without side effects [OK]
Common Mistakes:
  • Thinking idempotent means running only once
  • Confusing idempotent with manual approval
  • Assuming idempotent always creates new resources
2. Which of the following Jenkins pipeline step snippets correctly checks if a file exists before creating it to ensure idempotency?
easy
A. if (!fileExists('output.txt')) { writeFile file: 'output.txt', text: 'data' }
B. writeFile file: 'output.txt', text: 'data'
C. sh 'touch output.txt'
D. deleteFile 'output.txt'; writeFile file: 'output.txt', text: 'data'

Solution

  1. Step 1: Identify idempotent file creation

    Checking if the file exists before creating it avoids duplicates or errors.
  2. Step 2: Analyze each option

    if (!fileExists('output.txt')) { writeFile file: 'output.txt', text: 'data' } uses fileExists to check before writing, making it idempotent. Others either always write or delete first, which is not idempotent.
  3. Final Answer:

    if (!fileExists('output.txt')) { writeFile file: 'output.txt', text: 'data' } -> Option A
  4. Quick Check:

    Check before create = idempotent step [OK]
Hint: Use fileExists() to check before creating files [OK]
Common Mistakes:
  • Not checking file existence before writing
  • Deleting files before writing causing side effects
  • Assuming shell touch is idempotent without checks
3. Given this Jenkins pipeline snippet, what will be the output if the file build.log already exists?
if (!fileExists('build.log')) {
  writeFile file: 'build.log', text: 'Build started'
} else {
  echo 'File already exists'
}
medium
A. File already exists
B. File build.log deleted
C. Error: fileExists not found
D. Build started

Solution

  1. Step 1: Understand the condition logic

    The code checks if 'build.log' does not exist, then writes to it; else it echoes a message.
  2. Step 2: Apply the condition with existing file

    Since 'build.log' exists, the else branch runs, printing 'File already exists'.
  3. Final Answer:

    File already exists -> Option A
  4. Quick Check:

    Existing file triggers else echo [OK]
Hint: If file exists, else branch runs printing message [OK]
Common Mistakes:
  • Assuming writeFile runs even if file exists
  • Confusing fileExists with file creation
  • Expecting an error when file exists
4. You have this Jenkins pipeline step:
sh 'mkdir /app/data'
sh 'echo "Hello" > /app/data/greeting.txt'
Why might this step fail when run multiple times, and how can you fix it to be idempotent?
medium
A. echo command overwrites file; fix by appending with >>
B. No fix needed; commands are already idempotent
C. mkdir needs sudo; fix by running as root
D. mkdir fails if directory exists; fix by adding -p flag: mkdir -p /app/data

Solution

  1. Step 1: Identify why mkdir fails on repeats

    Without -p, mkdir errors if directory exists, breaking idempotency.
  2. Step 2: Fix mkdir command for idempotency

    Adding -p makes mkdir create directory only if missing, no error if exists.
  3. Final Answer:

    mkdir fails if directory exists; fix by adding -p flag: mkdir -p /app/data -> Option D
  4. Quick Check:

    mkdir -p avoids error if directory exists [OK]
Hint: Use mkdir -p to avoid errors if directory exists [OK]
Common Mistakes:
  • Ignoring mkdir error on existing directory
  • Thinking echo causes failure
  • Assuming commands are idempotent without checks
5. You want to create a Jenkins pipeline step that installs a package only if it is not already installed, making the step idempotent. Which scripted pipeline snippet achieves this correctly?
hard
A. sh 'apt-get remove curl; apt-get install -y curl'
B. sh 'apt-get install -y curl'
C. sh 'dpkg -s curl || apt-get install -y curl'
D. sh 'apt-get update && apt-get install curl'

Solution

  1. Step 1: Understand idempotent package installation

    Installing only if missing avoids repeated installs and errors.
  2. Step 2: Analyze each option for idempotency

    sh 'dpkg -s curl || apt-get install -y curl' checks if package is installed with dpkg -s, installs only if missing. Others always install or remove, not idempotent.
  3. Final Answer:

    sh 'dpkg -s curl || apt-get install -y curl' -> Option C
  4. Quick Check:

    Check before install = idempotent package step [OK]
Hint: Check package before install to keep step idempotent [OK]
Common Mistakes:
  • Always running install causing unnecessary work
  • Removing package before install causing downtime
  • Skipping package existence check
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