📖 Scenario: You are setting up a Jenkins job to automate a simple build process. After the build completes, you want Jenkins to perform some actions automatically, like archiving build artifacts and sending notifications.
🎯 Goal: Learn how to configure post-build actions in a Jenkins job using the Jenkinsfile syntax.
📋 What You'll Learn
Create a Jenkinsfile with a basic pipeline
Add a post-build action to archive artifacts
Add a post-build action to send a notification
Print confirmation that post-build actions are configured
💡 Why This Matters
🌍 Real World
In real projects, Jenkins post-build actions automate tasks like saving build outputs and notifying teams, saving time and reducing errors.
💼 Career
Understanding Jenkins post-build actions is essential for DevOps engineers to automate continuous integration and delivery pipelines effectively.
Progress0 / 4 steps
1
Create a basic Jenkins pipeline
Create a Jenkinsfile with a pipeline block that has an empty agent any and an empty stages block.
Jenkins
Hint
Start with the basic Jenkins pipeline structure: pipeline { agent any stages { } }.
2
Add a post-build action to archive artifacts
Add a post block inside the pipeline with a always condition that uses archiveArtifacts to archive files matching "**/*.jar".
Jenkins
Hint
Use post { always { archiveArtifacts artifacts: '**/*.jar' } } to archive all JAR files after every build.
3
Add a post-build action to send a notification
Inside the same always block in the post section, add a echo step that prints "Build completed - notifications sent".
Jenkins
Hint
Add echo 'Build completed - notifications sent' inside the always block to simulate sending a notification.
4
Print confirmation of post-build actions
Add a print statement in the Jenkinsfile script that outputs exactly "Post-build actions configured successfully".
Jenkins
Hint
Use print('Post-build actions configured successfully') outside the pipeline block to confirm setup.
Practice
(1/5)
1. What is the main purpose of Post-build actions in Jenkins?
easy
A. To start the build process
B. To write the build script
C. To perform tasks automatically after the build finishes
D. To configure the Jenkins server settings
Solution
Step 1: Understand Jenkins build phases
Jenkins runs build steps first, then post-build actions after the build ends.
Step 2: Identify the role of post-build actions
Post-build actions automate tasks like notifications, archiving, or cleanup after the build completes.
Final Answer:
To perform tasks automatically after the build finishes -> Option C
Quick Check:
Post-build actions = tasks after build [OK]
Hint: Post-build actions run after build ends to automate tasks [OK]
Common Mistakes:
Confusing post-build actions with build steps
Thinking post-build actions start the build
Assuming post-build actions configure Jenkins server
2. Which of the following is the correct syntax to add a post-build action in a Jenkins pipeline script?
easy
A. build {
post {
echo 'Build finished'
}
}
B. pipeline {
postbuild {
echo 'Build finished'
}
}
C. steps {
post {
echo 'Build finished'
}
}
D. post {
always {
echo 'Build finished'
}
}
Solution
Step 1: Recall Jenkins pipeline post syntax
In declarative pipelines, post block defines post-build actions with conditions like always.
Step 2: Match syntax options
post {
always {
echo 'Build finished'
}
} uses correct post { always { ... } } syntax. Others use invalid blocks like build or postbuild.
Final Answer:
post { always { echo 'Build finished' } } -> Option D
Quick Check:
Post block syntax = post {
always {
echo 'Build finished'
}
} [OK]
Hint: Use 'post { always { ... } }' for post-build actions in pipeline [OK]
Common Mistakes:
Using 'postbuild' instead of 'post'
Placing post block inside 'steps'
Using 'build' block incorrectly
3. Given this Jenkins pipeline snippet, what will be printed after the build finishes successfully?
A. The mail step requires proper Jenkins email configuration
B. The failure block should be inside steps
C. The mail step requires a steps block inside failure
D. The post block must be inside steps
Solution
Step 1: Understand the mail step requirements
The mail step requires the 'Email Extension Plugin' to be installed and SMTP server configured in Manage Jenkins > Configure System > Extended E-mail Notification.
Step 2: Identify the likely problem
Without proper email configuration (SMTP host, port, credentials, etc.), the notification won't send even if the syntax is correct.
Final Answer:
The mail step requires proper Jenkins email configuration -> Option A
Quick Check:
Mail needs SMTP config [OK]
Hint: Configure SMTP in Jenkins for mail step [OK]
Common Mistakes:
Not installing Email Extension plugin
Not setting SMTP server details
Missing 'from' address or credentials
5. You want to archive build artifacts only if the build succeeds and always send a notification email regardless of build result. Which Jenkins pipeline post section correctly implements this?
hard
A. post {
success {
archiveArtifacts 'build/*.jar'
}
always {
mail(to: 'devs@example.com', subject: 'Build Notification')
}
}
B. post {
always {
archiveArtifacts 'build/*.jar'
mail(to: 'devs@example.com', subject: 'Build Notification')
}
}
C. post {
success {
archiveArtifacts 'build/*.jar'
mail(to: 'devs@example.com', subject: 'Build Notification')
}
}
D. post {
failure {
archiveArtifacts 'build/*.jar'
}
always {
mail(to: 'devs@example.com', subject: 'Build Notification')
}
}
Solution
Step 1: Identify conditions for each action
Archiving should happen only on success, so it goes inside success. Email should always send, so it goes inside always.
Step 2: Match options with conditions
post {
success {
archiveArtifacts 'build/*.jar'
}
always {
mail(to: 'devs@example.com', subject: 'Build Notification')
}
} correctly separates archive in success and mail in always. Others mix conditions incorrectly.
Final Answer:
post { success { archiveArtifacts 'build/*.jar' } always { mail(to: 'devs@example.com', subject: 'Build Notification') } } -> Option A
Quick Check:
Use success for archive, always for mail [OK]
Hint: Use 'success' for archive, 'always' for notifications [OK]
Common Mistakes:
Putting archive in always block
Sending mail only on success
Archiving on failure instead of success
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.