Jenkins trigger wrapper around pipeline shared library script - jenkins

We have shared global scripts available for our Jenkins repos.
They work by importing the shared library and executing it.
Many people may use the same shared library.
Jenkinsfile (In my repo)
#Library('shared-stuff) _
runSharedTests()
runSharedTests (In a completely separate repo)
def call() {
def agent = getAgent()
def setVariable = setAVariable()
pipline {
agent {
label agent
}
stages {
stage('Do Something') {
steps {
executeSomething()
}
}
}
}
}
Is it possible to add a trigger to my Jenkinsfile that will trigger the runSharedTests pipeline periodically?
I cannot add the trigger directly to runSharedTests directly because then hundreds of repos will get that change and trigger.

In your case most suitable way to achieve your goal is making your jenkins job periodically. Your code does not need to be changed.
Configure → Build Triggers → Build periodically → Schedule:
To allow periodically scheduled tasks to produce even load on the system, the symbol H (for “hash”) should be used wherever possible. For example, using 0 0 * * * for a dozen daily jobs will cause a large spike at midnight. In contrast, using H H * * * would still execute each job once a day, but not all at the same time, better using limited resources.
The H symbol can be used with a range. For example, H H(0-7) * * * means some time between 12:00 AM (midnight) to 7:59 AM. You can also use step intervals with H, with or without ranges.
The H symbol can be thought of as a random value over a range, but it actually is a hash of the job name, not a random function, so that the value remains stable for any given project.
Beware that for the day of month field, short cycles such as */3 or H/3 will not work consistently near the end of most months, due to variable month lengths. For example, */3 will run on the 1st, 4th, …31st days of a long month, then again the next day of the next month. Hashes are always chosen in the 1-28 range, so H/3 will produce a gap between runs of between 3 and 6 days at the end of a month. (Longer cycles will also have inconsistent lengths but the effect may be relatively less noticeable.)
Empty lines and lines that start with # will be ignored as comments.
In addition, #yearly, #annually, #monthly, #weekly, #daily, #midnight, and #hourly are supported as convenient aliases. These use the hash system for automatic balancing. For example, #hourly is the same as H * * * * and could mean at any time during the hour. #midnight actually means some time between 12:00 AM and 2:59 AM.
Examples:
# every fifteen minutes (perhaps at :07, :22, :37, :52)
H/15 * * * *
# every ten minutes in the first half of every hour (three times, perhaps at :04, :14, :24)
H(0-29)/10 * * * *
# once every two hours every weekday (perhaps at 10:38 AM, 12:38 PM, 2:38 PM, 4:38 PM)
H 9-16/2 * * 1-5
# once a day on the 1st and 15th of every month except December
H H 1,15 1-11 *
Another way is using the triggers directive. It defines the automated ways in which the Pipeline should be re-triggered. For example:
pipeline {
agent any
triggers {
cron('H */4 * * 1-5')
}
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
}
But note that the declarative style is used here.

Related

"H 5 * * 7" cron in Jenkins groovy script

I am trying to understand when this trigger runs in Jenkins groovy script. Seems to me that Jenkins groovy script has some DSL for cron. Does anyone know when this cron job is triggered?
triggers {
cron('H 5 * * 7')
}
In H 5 * * 7 the H means hash. You can have digit in the first place, but H allows to distribute the load better.
H = minute when less load is expected, Jenkins will propose it the first time, after that it will stay more or less the same, if there are available agents to handle it.
5 = hour,
* = any value (every day),
* = any value (every month),
7 = 7th day of week (Sunday).
Reference:
Spread load evenly by using ‘H * * * *’ rather than ‘5 * * * *’
You can test it using the CRONTab tester.
This means to start at 5:00 on every sunday
https://crontab.guru/#0_5_*_*_7

grails quartz job never gets executed

I have this job:
class MyJob {
static triggers = {
cron name:"some job", cronExpression:"0 0 * * * ? *"
}
def execute() {
log.info "job runs"
}
}
The job should run every hour. I thought that the problem is the cronExpression. That's why I changed it to the expression above. Before I had this expression:
0 0 0/1 1/1 * ? *
But none of the expressions work.
My setup:
I have 10 jobs that run between every 5 minutes to once per month.
Every job seems to run except this one...
My questions:
Why is the job not executed?
Is there a max_jobs parameter that is responsible for this?
Try without year:
static triggers = {
cron name: 'myTrigger', cronExpression: "0 0 0-23 * * ?"
}
You can control it f.e. by predicting time of execution and preparing proper expression (it triggers only twice a month; on first day of month at 1.00 and 2.00 am):
static triggers = {
cron name: 'myTrigger', cronExpression: "0 0 1-2 1 * ?"
}
Read more about different types of triggers:
Currently plugin supports three types of triggers:
simple — executes once per defined interval (ex. “every 10 seconds”);
cron — executes job with cron expression (ex. “at 8:00am every Monday through Friday”);
custom — your implementation of Trigger interface.
Remember also about:
The triggers name property must be unique across all triggers in the
application.
By default, jobs will not be executed when running under the test
environment.
More examples you can find here.

I want Jenkins job to build every two weeks

Will this expression run the build every other Friday at noon? Assume i set this up on a Friday?
0 12 * * */14
I tried 0 12 * * FRI/14 but Jenkins returned an error.
I ma trying to run a code report job every two weeks to match scrum.
You'll have to add some logic to the build script to determine if it ran last week, and then run it every week.
I looked around similar questions for cron jobs, and you have to do some shell magic to make it work.
You could try what was suggested here:
H H 8-14,22-28 * 5
Which would qualify on Fridays that are in the second or fourth week of the month.
it will run at noon every other friday
00 12 */2 * 5
I had the same issue, the easy work around I found was to create another job that run weekly.
This job was a simple groovy script that does the following:
import jenkins.model.*;
def job = Jenkins.instance.getJob('JobNameToRunEveryTwoWeek')
job.setDisabled(!job.isDisabled())
Since Jenkins does not offer the functionnality its the best easy solution I could find. If you have better solution feel free to let me know.
One ridiculous-looking-but-it-works answer: schedule your job to run every week, and then at the top of the job add the following:
// Suppressing even number builds, so this job only runs
// every other week.
def build_number = env.BUILD_NUMBER as int
if ((build_number % 2) == 0) {
echo "Suppressing even number builds!"
echo """THIS IS A HACK TO MAKE THIS JOB RUN BIWEEKLY.
Jenkins cron scheduling currently doesn't support scheduling a
bi-weekly job. We could resort to shell or other tricks to
calculate if the job should be run (e.g., comparing to the date
of the last run job), it's annoying, and this works just as well.
Schedule this job to run weekly. It will exit early every other week.
refs:
* https://stackoverflow.com/questions/33785196/i-want-jenkins-job-to-build-every-two-weeks
* https://issues.jenkins-ci.org/browse/JENKINS-19756
"""
currentBuild.result = 'SUCCESS'
return
}
For Jenkins, you can try this approach as well.
1 1 8-14,21-28 * 5

Jenkins job scheduler

How can I set Jenkins to run a job at a particular time?
Like if I'd like to set it to 8:30am every weekday and this is what I could do
H 7 * * 1-5
this randomly picks up 7:35am as running time.
H is a pseudo-random number, based on the hash of the jobname.
When you configured:
H 7
you are telling it:
At 7 o'clock, at random minute, but that same minute very time
Here is the help directly from Jenkins (just click the ? icon)
To allow periodically scheduled tasks to produce even load on the system, the symbol H (for “hash”) should be used wherever possible. For example, using 0 0 * * * for a dozen daily jobs will cause a large spike at midnight. In contrast, using H H * * * would still execute each job once a day, but not all at the same time, better using limited resources.
The H symbol can be used with a range. For example, H H(0-7) * * * means some time between 12:00 AM (midnight) to 7:59 AM. You can also use step intervals with H, with or without ranges.
The H symbol can be thought of as a random value over a range, but it actually is a hash of the job name, not a random function, so that the value remains stable for any given project
If you want it at 8:30 every weekday, then you must specify just that:
30 8 * * 1-5
Take a look at http://www.cronmaker.com/
0 30 8 ? * MON,TUE,WED,THU,FRI *
30 8 * * 1-5
This would start at 8:30am Mon-Fri.
0 and 7 are Sundays.
Not sure what the H does but I am assuming it takes the lower case hex of h and applies 68 which is 35 in decimal... lol. Don't do that.
Following this format:
Minute Hour DayOfMonth DayOfWeek Day
It picks that time because you told it that it can, as imagine you already know:
minute, hour, day of month, month, day of week.
Now you have user H which allows Jenkins to pick at random. So you have told it to run between 7-8 every week day.
Change this to:
30 8 * * 1-5
Hope this helps!

Schedule nightly 22-03 build using Jenkins and H, the "hash symbol"

A build that takes about three hours to complete needs to be scheduled for nightly building outside office hours: not sooner than 22:00 and not later than 3:59 next day.
I'd also like to use the "H symbol" to avoid collision with future nightly builds. From in-line help in Jenkins:
To allow periodically scheduled tasks to produce even load on the system, the symbol H (for “hash”) should be used wherever possible. For example, using 0 0 * * * for a dozen daily jobs will cause a large spike at midnight. In contrast, using H H * * * would still execute each job once a day, but not all at the same time, better using limited resources.
(How) can I schedule this using Jenkins? What I've tried was all considered invalid by Jenkins:
H H(22,23,0,1,2,3) * * *
Invalid input: "H H(22,23,0,1,2,3) * * *": line 1:7: expecting "-", found ','
H H22,23,0,1,2,3 * * *
Invalid input: "H H22,23,0,1,2,3 * * *": line 1:4: unexpected token: 22
H H(22-3) * * *
Invalid input: "H H(22-3) * * *": line 1:9: 1 is an invalid value. Must be within 1
and -18
Is it possible to achieve this without using plug-ins?
I think the closest you will get is to use:
H H(0-3) * * * This will run at some point between 0:00 and 3:59
#midnight This will run at some point between 0:00 and 2:59
The H(4-8) construct only works if the second items is larger then the first.
But you might as well fill in the hour yourself. Jenkins actually never changes the hour the jobs runs once it is set. It will basically create some random hour once you save the job and always run the job at that particular time.
Of course, you can also file a bug report or feature request that you should be able to specify this as H(22-3) or better, fix the code and submit a patch ;)
There is no direct support to write the expression like this, but since there is timezone support (now), you can work around this.
# DONT COPY PASTE - THIS DOESNT WORK!
# This is what we would like to write, but is not supported
H H(22-3) * * *
Above expression means we want to build somewhen between 22 PM and 3 AM, this is a 5 hour period, so we could write:
# Assuming we're in GMT+2 we can just shift the timezone
# so 22-03 becomes 10-15 wich is 12 hours earlier so the
# timezone is GMT-10
TZ=Etc/GMT-10
H H(10-15) * * *
I found this workaround in the comments of JENKINS-18313
UPDATE:
There is currently a bug JENKINS-57702 and the timezone GMT-XX is not evaluated correctly. A workaround is to use a equivalent timezone, in this example the one for Hawaii:
TZ=US/Hawaii
H H(10-15) * * *

Resources