5. Triggers¶
5.1. Recap¶
Crontab syntax
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
5.1.1. Webhook¶
Webhook trigger for GitScm polling
<jenkins-url>/git/notifyCommit?url=<clone-url>
5.1.2. Cron¶
cron()
Accepts a cron-style string to define a regular interval at which the Pipeline should be re-triggered
triggers {
cron('H */4 * * 1-5')
}
5.1.3. Poll SCM¶
pollSCM()
Build periodically
Accepts a cron-style string to define a regular interval at which Jenkins should check for new source changes
If new changes exist, the Pipeline will be re-triggered
Available since Jenkins 2.22
triggers {
pollSCM('H */4 * * 1-5')
}
5.1.4. Upstream¶
Build after other projects are built
upstream()
Accepts a comma separated string of jobs and a threshold
When any job in the string finishes with the minimum threshold, the Pipeline will be re-triggered.
triggers {
upstream(upstreamProjects: 'job1,job2',
threshold: hudson.model.Result.SUCCESS)
}
5.1.5. REST API¶
Trigger builds remotely (e.g., from scripts via REST API)
curl -X POST http://localhost:8080/job/JOB_NAME/build \
--user USER:TOKEN \
--data-urlencode json='{
"parameter": [
{"name":"id", "value":"123"},
{"name":"verbosity", "value":"high"}
]}'
5.1.6. Example¶
pipeline {
agent any
triggers {
cron('@daily')
}
stages {
stage("Test") {
steps {
sh '/bin/echo Testing...'
}
}
}
}
5.1.7. Assignments¶
5.2. API Trigger¶
Napisz skrypt
sh
wykorzystującycurl
Skrypt po odpaleniu ma triggerować build
Dodaj skrypt do
crontab
Skrypt ma się uruchamiać
@daily
Zwróć uwagę, że
cron
ma mniejszą ilość zmiennych środowiskowych (skrypt, który u Ciebie działa, może nie być odpalany przezcron
)