📖 Scenario: You are setting up a Jenkins job to run a simple shell script. To improve the build environment, you want to add a build wrapper that timestamps the console output. This helps you see when each line of the build log was generated, which is useful for troubleshooting and monitoring.
🎯 Goal: Create a Jenkins pipeline script that uses a build wrapper to add timestamps to the console output, then runs a simple shell command.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Add a timestamps() build wrapper inside the options block
Add a stage named Build that runs a shell command echo "Hello, Jenkins!"
Print the output with timestamps in the Jenkins console
💡 Why This Matters
🌍 Real World
In real Jenkins jobs, build wrappers like timestamps help developers and DevOps engineers monitor build logs with time information, making it easier to debug and analyze build performance.
💼 Career
Knowing how to add build wrappers in Jenkins pipelines is a key skill for DevOps roles, as it improves build visibility and helps maintain reliable CI/CD pipelines.
Progress0 / 4 steps
1
Create basic Jenkins pipeline skeleton
Create a Jenkins pipeline script with a pipeline block and an empty agent any section.
Jenkins
Hint
Start with the pipeline block and specify agent any to run on any available agent.
2
Add build wrapper for timestamps
Inside the pipeline block, add an options block with the timestamps() build wrapper.
Jenkins
Hint
Use the options block to add build wrappers like timestamps().
3
Add a build stage with shell command
Add a stage named Build inside the pipeline block with a steps section that runs sh 'echo "Hello, Jenkins!"'.
Jenkins
Hint
Define a stage with the name Build and use sh to run the shell command.
4
Print the build output with timestamps
Run the pipeline script and observe the Jenkins console output showing timestamps before each line.
Jenkins
Hint
After running the pipeline, the Jenkins console log should show timestamps before the output lines.
Practice
(1/5)
1. What is the main purpose of build wrappers in Jenkins?
easy
A. To prepare the environment before a build starts
B. To deploy the application after the build
C. To write test cases for the build
D. To monitor server health during the build
Solution
Step 1: Understand build wrappers role
Build wrappers are used to set up or modify the environment before the build runs.
Step 2: Differentiate from other build steps
Deployment, testing, and monitoring happen after or outside the build wrapper scope.
Final Answer:
To prepare the environment before a build starts -> Option A
Quick Check:
Build wrappers = environment setup [OK]
Hint: Build wrappers run before build steps start [OK]
Common Mistakes:
Confusing build wrappers with deployment steps
Thinking build wrappers run after the build
Assuming build wrappers handle testing
2. Which syntax correctly adds a build wrapper to mask passwords in a Jenkins pipeline?
easy
A. properties { buildWrappers { maskPasswords() } }
B. properties { buildWrappers { mask_passwords() } }
C. buildWrappers { properties { maskPasswords() } }
D. buildWrappers { maskPasswords() }
Solution
Step 1: Recall correct Jenkins pipeline syntax
Build wrappers are added inside the properties block using buildWrappers { maskPasswords() }.
Step 2: Identify syntax errors in options
properties { buildWrappers { mask_passwords() } } uses underscores which is incorrect. Options C and D misplace the blocks.
Final Answer:
properties { buildWrappers { maskPasswords() } } -> Option A
Build wrappers must be inside properties { buildWrappers { ... } } to work properly.
Step 2: Verify both wrappers are included
properties {
buildWrappers {
cleanWs()
timeout(time: 15, unit: 'MINUTES')
}
} includes cleanWs() and timeout() inside buildWrappers correctly.
Step 3: Eliminate incorrect options
properties {
buildWrappers {
timeout(time: 15, unit: 'MINUTES')
cleanWs()
}
} has incorrect order but is syntactically valid; however, the question implies the order in B is preferred. Options C and D have incorrect block structure.