Triggers directive in Jenkins - Time & Space Complexity
When using the triggers directive in Jenkins pipelines, it is important to understand how often the trigger checks run as the number of jobs grows.
We want to know how the time Jenkins spends checking triggers changes when we add more jobs or triggers.
Analyze the time complexity of the following Jenkins pipeline snippet using triggers.
pipeline {
triggers {
cron('H/15 * * * *')
}
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
}
}
This pipeline uses a cron trigger to start the job every 15 minutes automatically.
Look for repeated checks or operations Jenkins performs related to triggers.
- Primary operation: Jenkins checks the cron schedule periodically to decide if the job should start.
- How many times: This check happens every minute for each job with a cron trigger configured.
As the number of jobs with triggers increases, Jenkins must check each trigger schedule regularly.
| Input Size (n jobs) | Approx. Operations per minute |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of trigger checks grows linearly with the number of jobs.
Time Complexity: O(n)
This means the time Jenkins spends checking triggers grows directly in proportion to the number of jobs with triggers.
[X] Wrong: "Trigger checks happen only once regardless of job count."
[OK] Correct: Each job with a trigger requires its own schedule check, so more jobs mean more checks.
Understanding how trigger checks scale helps you design Jenkins setups that stay responsive as your projects grow.
"What if we added multiple triggers per job? How would that affect the time complexity?"
Practice
triggers directive in a Jenkins pipeline?Solution
Step 1: Understand the role of triggers
The triggers directive is used to start Jenkins jobs automatically without manual intervention.Step 2: Compare with other pipeline directives
Other directives like steps define build actions, environment sets variables, and permissions control access, not job start timing.Final Answer:
To start jobs automatically based on time or events -> Option CQuick Check:
Triggers = automatic job start [OK]
- Confusing triggers with build steps
- Thinking triggers set environment variables
- Assuming triggers control user permissions
triggers directive?Solution
Step 1: Understand cron syntax in Jenkins
Jenkins cron uses five fields: minute hour day month weekday. '0 0 * * *' means midnight every day.Step 2: Identify correct trigger usage
The triggers directive uses cron('schedule') to schedule jobs. pollSCM is for source control polling, not time-based triggers.Final Answer:
triggers { cron('0 0 * * *') } -> Option AQuick Check:
Midnight cron = '0 0 * * *' [OK]
- Using invalid cron times like '60 24'
- Confusing pollSCM with cron
- Misordering cron fields
triggers {
upstream(upstreamProjects: 'BuildApp', threshold: hudson.model.Result.SUCCESS)
}Solution
Step 1: Understand the upstream trigger
The upstream trigger starts this job when the specified upstream job finishes with the given result.Step 2: Analyze the parameters
upstreamProjects: 'BuildApp' means the job named 'BuildApp' triggers this job. threshold SUCCESS means only on successful completion.Final Answer:
The job triggers when the 'BuildApp' job completes successfully -> Option DQuick Check:
Upstream job success triggers downstream [OK]
- Thinking it triggers every minute
- Confusing upstream with SCM polling
- Assuming manual trigger only
triggers {
cron('0 25 * * *')
}Solution
Step 1: Check cron field ranges
Cron fields are minute (0-59), hour (0-23), day, month, weekday. '25' in hour field is invalid.Step 2: Validate placement and field count
Cron is correctly placed in triggers and uses five fields, which is standard in Jenkins.Final Answer:
The hour field '25' is invalid in cron syntax -> Option AQuick Check:
Hour must be 0-23 in cron [OK]
- Assuming cron has six fields
- Placing cron inside steps block
- Thinking 25 means 25 minutes
triggers directive setup achieves this?Solution
Step 1: Understand pollSCM usage
pollSCM triggers the job every specified interval but only if SCM changes are detected.Step 2: Analyze the cron expressions
'H/15 * * * *' means every 15 minutes with hashing for load balancing. cron triggers regardless of SCM changes.Final Answer:
triggers { pollSCM('H/15 * * * *') } -> Option BQuick Check:
pollSCM runs on changes every 15 minutes [OK]
- Using cron which triggers regardless of changes
- Confusing upstream trigger with SCM polling
- Setting pollSCM to wrong cron expression
Ready to prove it?
Pass the test and this pattern moves unknown
