I want Jenkins job to build every two weeks - jenkins

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

Related

Jenkins trigger wrapper around pipeline shared library script

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.

Jenkins plugin - Merge build queue?

I'm looking for the jenkins plugins.
Here is my scenario;
1) Job B's quiet period is set to 10 minutes.
2) Job B will have 10 queued builds.
3) After 10 minutes, job B-1 starts running.
4) After B-1 finished, then B-2 starts running.
5) ...
==> Instead of running a single B-1 build in step 3), I want to gather all the 10 queued build's parameters and run a merged build B-x, and discard all the 10 build queues.
Is it possible??
if I got your question you have a parameter job with 10 jobs in queue , and you want to run only the last one ?
If yes you should use some groovy script to check the queue before you trigger the job or inside the job as build step, and clean all previous jobs that exist in the queue.
here is an example to clean jobs for a specific branch , you can modify it for your needs. let me know if you need any help
Thanks , Mor
import jenkins.model.*
def branchName = build.environment.get("GIT_BRANCH_NAME")
def buildNo = build.environment.get("BUILD_NUMBER")
println "checking if need to clean the queue for" + branchName + " build number : " + buildNo
def q = Jenkins.instance.queue
q.items.each {
println("${it.task.name}:")
}
q.items.findAll { it.task.name.startsWith(branchName) }.each {
q.cancel(it.task)
}
You sound to be describing a matrix project which enables a matrix of different build parameter combinations.
If you had 3 different parameters with three different options, this would give you 9 builds, each in its own workspace. There are options to remove some combinations
This is a good explanation of matrix builds

Schedule a Jenkins Job to run hourly everyday but skip midnight (00:00) and resume again from 01:00 till 23:00

I have a Jenkins job which I would like to run from (01:00) 1 AM till (23:00) 11PM but skip midnight (00:00) and resume again from (01:00) on a daily basis. The thread How to schedule Jenkins job every Hour for the next 12 hours had this example H 9-21 * * * which i have changed to H 1-23 * * * to cater for my example.
Am I on the right track perhaps with using H 1-23 * * * ??
Yes, that should work. There is a cron tester you can look that will show you the cron version of that, and for Jenkins you just replace the 0 with the H.
http://cron.schlitt.info/index.php?cron=0+1-23+*+*+*+&iterations=50&test=Test

Jenkins Cron Expression Not Scheduled at Right time

All,
Tried to configure jenkins job to trigger at EVERYDAY 10AM and used below cron H 10 * * * but the jenkins console is not running at 10AM rather its running at 10.09AM. Please help me to run at 10AM everyday around the year.
update: After adding the expression with '0 10 * * *', got below warning and no next run time is displayed. is that normal?
30 5 * * *
will run every day at 5:30 AM
#daily
will run the job once a day at some time, chosen by Jenkins
0 10 * * *
will run every day at 10:00 AM
When not using 'H' in the beginning you will get the warning, you will not get a tip when the job will or would have run, but it will still be active, i.e. it will run as per the statement.
You will always see a syntax error in red color when making any syntax errors.
Also, a good idea might be to create a dummy job to experiment with cron trigger, if you don't feel comfortable with it yet. Or use crontab on Linux:
crontab -e
man crontab
If you really want it at 10AM, please use 0 10 * * *
or 5:30 AM as you asked in comment
30 5 * * *
Jenkins will warn you in this case. If every job schedules like this, load suddenly goes up. Jenkins advice you to differ job time a bit. The H indicate once in every hour, not particularly at 0th minute.

How do I write this Cron Expression?

I am trying to write a cron expression for a job which should run every 3 days at 9AM GMT. This is what I am writing
"0 0 9/72 ? * ?"
But I get it's as an invalid cron expression because I suppose there can not be two question marks in that. So how should I create a cron job for my use case?
I can not put a * as that would mean the cron must run every day, which it shouldn't.
With grails & quartz-scheduler, the right syntax would be :
"0 0 9 */3 * ?"
0 9 */3 * * <Your Command>
This will run the job every 3 days at 9:00 AM.
Cron Syntax.

Resources