Bird
Raised Fist0
Jenkinsdevops~20 mins

Pipeline utility functions in Jenkins - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pipeline Utility Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of readYaml in Jenkins Pipeline
Given the following Jenkins pipeline snippet, what is the output of the echo step?
Jenkins
pipeline {
  agent any
  stages {
    stage('Read YAML') {
      steps {
        script {
          def config = readYaml text: '''\nname: Jenkins\nversion: 2.0\n''' 
          echo "${config.name}"
        }
      }
    }
  }
}
AJenkins
Bversion
C2.0
Dnull
Attempts:
2 left
💡 Hint
The readYaml step parses YAML text into a map. Access keys by their names.
🧠 Conceptual
intermediate
1:30remaining
Purpose of stash and unstash in Jenkins Pipeline
What is the main purpose of using stash and unstash steps in a Jenkins pipeline?
ATo clean the workspace before starting a new build
BTo permanently store build artifacts on the Jenkins master
CTo send notifications to users after a build completes
DTo save files temporarily and retrieve them in another stage or node
Attempts:
2 left
💡 Hint
Think about sharing files between different parts of the pipeline.
🔀 Workflow
advanced
2:30remaining
Correct order of steps to archive artifacts and clean workspace
Arrange the following Jenkins pipeline steps in the correct order to archive build artifacts and then clean the workspace:
A4,2,3,1
B4,3,2,1
C3,4,2,1
D2,4,3,1
Attempts:
2 left
💡 Hint
Think about the logical flow: get code, build, save output, then clean.
❓ Troubleshoot
advanced
2:00remaining
Error caused by missing library in pipeline utility step
A Jenkins pipeline uses readJSON from the pipeline utility steps plugin but fails with groovy.lang.MissingMethodException. What is the most likely cause?
AThe pipeline utility steps plugin is not installed or not loaded
BThe pipeline syntax is missing a <code>script</code> block
CThe Jenkins agent does not have internet access
DThe JSON file path is incorrect
Attempts:
2 left
💡 Hint
MissingMethodException usually means the method is unknown to Jenkins.
✅ Best Practice
expert
3:00remaining
Best practice for handling secrets with pipeline utility functions
Which approach is the safest and recommended way to handle secrets in Jenkins pipelines when using pipeline utility functions?
AUse environment variables set globally on the Jenkins master
BHardcode secrets directly in pipeline scripts for easy access
CStore secrets in Jenkins credentials and access them via <code>withCredentials</code> block
DSave secrets in plain text files in the workspace and read them with <code>readFile</code>
Attempts:
2 left
💡 Hint
Think about security and avoiding exposure of secrets.

Practice

(1/5)
1. What is the main purpose of Jenkins pipeline utility functions?
easy
A. To manage Jenkins user permissions
B. To create new Jenkins jobs automatically
C. To simplify file and data handling within Jenkins pipelines
D. To monitor Jenkins server performance

Solution

  1. Step 1: Understand the role of pipeline utility functions

    These functions help with tasks like reading files, writing files, and handling data formats inside pipelines.
  2. Step 2: Compare options with the main purpose

    Only To simplify file and data handling within Jenkins pipelines describes simplifying file and data handling, which matches the purpose of these functions.
  3. Final Answer:

    To simplify file and data handling within Jenkins pipelines -> Option C
  4. Quick Check:

    Pipeline utility functions = simplify file/data handling [OK]
Hint: Focus on file and data tasks in pipelines [OK]
Common Mistakes:
  • Confusing pipeline utilities with job creation
  • Thinking they manage user permissions
  • Assuming they monitor server performance
2. Which of the following is the correct syntax to read a JSON file using Jenkins pipeline utility functions?
easy
A. def data = readJSON file: 'config.json'
B. def data = readJson('config.json')
C. def data = read_json file: 'config.json'
D. def data = readJSON('config.json')

Solution

  1. Step 1: Recall the correct function name and syntax

    The Jenkins pipeline utility function to read JSON is readJSON with a named parameter file.
  2. Step 2: Match the syntax with options

    def data = readJSON file: 'config.json' uses readJSON file: 'config.json', which is the correct syntax. Others use incorrect function names or parameter styles.
  3. Final Answer:

    def data = readJSON file: 'config.json' -> Option A
  4. Quick Check:

    Correct syntax = readJSON file: 'filename' [OK]
Hint: Use exact function name and named parameter 'file' [OK]
Common Mistakes:
  • Using wrong function name casing
  • Passing filename as positional argument
  • Using underscores in function names
3. Given the following Jenkins pipeline snippet:
def content = readFile 'example.txt'
writeFile file: 'copy.txt', text: content
What will be the result after running this pipeline?
medium
A. The file 'copy.txt' will contain the same content as 'example.txt'
B. The file 'example.txt' will be deleted
C. An error occurs because writeFile requires a closure
D. The file 'copy.txt' will be empty

Solution

  1. Step 1: Understand readFile usage

    The readFile function reads the entire content of 'example.txt' into the variable content.
  2. Step 2: Understand writeFile usage

    The writeFile function writes the text stored in content into a new file named 'copy.txt'.
  3. Final Answer:

    The file 'copy.txt' will contain the same content as 'example.txt' -> Option A
  4. Quick Check:

    readFile + writeFile = copy content [OK]
Hint: readFile reads, writeFile writes content to file [OK]
Common Mistakes:
  • Assuming writeFile deletes files
  • Thinking writeFile needs a closure
  • Expecting writeFile to append by default
4. You have this Jenkins pipeline code:
unzip zipFile: 'archive.zip', dir: 'output'

The pipeline fails with an error. What is the most likely cause?
medium
A. The 'unzip' function requires the 'zipFile' parameter to be a File object, not a string
B. The 'unzip' function is not available without the Pipeline Utility Steps plugin installed
C. The 'dir' parameter must be an absolute path, not a relative one
D. The 'unzip' function requires a closure to specify files to extract

Solution

  1. Step 1: Check plugin requirement for 'unzip'

    The unzip function is part of the Pipeline Utility Steps plugin, which must be installed for it to work.
  2. Step 2: Analyze error cause

    If the plugin is missing, the function call will fail regardless of parameters. Other options are incorrect because strings are accepted and relative paths work.
  3. Final Answer:

    The 'unzip' function is not available without the Pipeline Utility Steps plugin installed -> Option B
  4. Quick Check:

    Missing plugin = unzip fails [OK]
Hint: Check if Pipeline Utility Steps plugin is installed [OK]
Common Mistakes:
  • Assuming parameter types are wrong
  • Thinking relative paths are invalid
  • Expecting unzip to need a closure
5. You want to check if a file named 'config.yaml' exists in your workspace before reading it in a Jenkins pipeline. Which pipeline utility function and approach should you use?
hard
A. Use readJSON file: 'config.yaml' to check and read the file
B. Use readFile('config.yaml') directly and catch exceptions if file missing
C. Use writeFile file: 'config.yaml' to create the file before reading
D. Use fileExists('config.yaml') to check existence, then readFile('config.yaml') to read

Solution

  1. Step 1: Identify function to check file existence

    The fileExists function returns true if the file is present, false otherwise.
  2. Step 2: Use conditional logic to read file safely

    Check with fileExists first, then read with readFile only if the file exists to avoid errors.
  3. Final Answer:

    Use fileExists('config.yaml') to check existence, then readFile('config.yaml') to read -> Option D
  4. Quick Check:

    Check fileExists before readFile [OK]
Hint: Always check fileExists before reading files [OK]
Common Mistakes:
  • Trying to read file without checking existence
  • Using readJSON on a YAML file
  • Creating file before reading unnecessarily
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