Post-build actions in Jenkins - Time & Space Complexity
We want to understand how the time taken by post-build actions changes as the number of actions grows.
How does adding more post-build steps affect the total execution time?
Analyze the time complexity of the following Jenkins post-build actions snippet.
post {
success {
echo 'Build succeeded'
archiveArtifacts artifacts: '**/target/*.jar'
}
failure {
mail to: 'team@example.com', subject: 'Build Failed', body: 'Please check the logs.'
}
}
This snippet runs different actions after the build depending on success or failure.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Executing each post-build action once per build result.
- How many times: Each action runs exactly once per build, no loops or recursion.
Execution time grows linearly as more post-build actions are added.
| Input Size (number of post-build actions) | Approx. Operations |
|---|---|
| 1 | 1 action executed |
| 5 | 5 actions executed |
| 10 | 10 actions executed |
Pattern observation: Doubling the number of post-build actions roughly doubles the total execution time.
Time Complexity: O(n)
This means the total time grows directly in proportion to the number of post-build actions.
[X] Wrong: "Post-build actions run all at once, so adding more doesn't affect time much."
[OK] Correct: Each action runs one after another, so more actions add more time overall.
Understanding how post-build actions scale helps you design efficient Jenkins pipelines that run smoothly as they grow.
What if post-build actions were run in parallel? How would the time complexity change?
Practice
Post-build actions in Jenkins?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 CQuick Check:
Post-build actions = tasks after build [OK]
- Confusing post-build actions with build steps
- Thinking post-build actions start the build
- Assuming post-build actions configure Jenkins server
Solution
Step 1: Recall Jenkins pipeline post syntax
In declarative pipelines,postblock defines post-build actions with conditions likealways.Step 2: Match syntax options
post { always { echo 'Build finished' } } uses correctpost { always { ... } }syntax. Others use invalid blocks likebuildorpostbuild.Final Answer:
post { always { echo 'Build finished' } } -> Option DQuick Check:
Post block syntax = post { always { echo 'Build finished' } } [OK]
- Using 'postbuild' instead of 'post'
- Placing post block inside 'steps'
- Using 'build' block incorrectly
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
}
post {
success {
echo 'Build succeeded!'
}
failure {
echo 'Build failed!'
}
}
}Solution
Step 1: Analyze build steps
The pipeline prints 'Building...' during the build stage.Step 2: Check post-build actions on success
On successful build, thesuccessblock runs, printing 'Build succeeded!'.Final Answer:
Building... Build succeeded! -> Option BQuick Check:
Success post block runs after build [OK]
- Confusing failure block with success block
- Ignoring initial build echo
- Assuming post block runs before build
post {
failure {
mail(to: 'team@example.com', subject: 'Build Failed')
}
}
What is the likely problem?Solution
Step 1: Understand the mail step requirements
Themailstep 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 AQuick Check:
Mail needs SMTP config [OK]
- Not installing Email Extension plugin
- Not setting SMTP server details
- Missing 'from' address or credentials
Solution
Step 1: Identify conditions for each action
Archiving should happen only on success, so it goes insidesuccess. Email should always send, so it goes insidealways.Step 2: Match options with conditions
post { success { archiveArtifacts 'build/*.jar' } always { mail(to: 'devs@example.com', subject: 'Build Notification') } } correctly separates archive insuccessand mail inalways. Others mix conditions incorrectly.Final Answer:
post { success { archiveArtifacts 'build/*.jar' } always { mail(to: 'devs@example.com', subject: 'Build Notification') } } -> Option AQuick Check:
Use success for archive, always for mail [OK]
- Putting archive in always block
- Sending mail only on success
- Archiving on failure instead of success
Ready to prove it?
Pass the test and this pattern moves unknown
