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
Related
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.
I have 5 jobs in Jenkins:
Image example
"E" is executed only after both parents(C & D) have completed their builds.
How do I trigger the child job only after both parents jobs?
Note: I want to make sure that "E" executes only once.
Use a pipeline for it.
Run jobs C and D in parallel , once both complete run job E.
Is there any case where C or D would be ran independently of the other and then E being ran? If not, then you could just add a post-build downstream for D from C and downstream E from D therefore the pipeline would get built C->D->E in that order.
You can have Up & Down stream project setup with your project order. Use General => Advanced => Block build when upstream project is building option to your Project E so that it will wait until Project C & D builds are done. You can have the same for Project C & D also. Hope this helps.
We have one jenkins job (A) that triggers 3 other jobs (B1,B2,B3).
These 3 jobs all trigger the same job (C).
When triggering job A, the job C is executed twice (I expected 3 times).
Question: Can someone please explain why C is triggered twice rather than 3 times?
Dependencies overview:
-> B1 ->
A -> B2 -> C
-> B3 ->
The downstream build view shows that 2 of the jobs (for example B2,B3) trigger only one execution of C. Please note that these are not always the same 2 jobs.
Execution overview (Downstream build view of A)
-> B1 -> C (build number 1)
A -> B2 -> C (build number 2)
-> B3 -> C (build number 2) <<< same as for B2
More details about the job configs:
Job A has Post-build Actions/Build other projects: B1, B2, B3
Job C has Build trigger/Build after other projects are built/Projects to watch: B1, B2, B3
Jenkins version: 1.583
It's the way how Jenkins triggers jobs. If concurrent builds are not allowed in C (I suppose they aren't), then the following happens:
A finishes and triggers B1,B2,B3
B1 (for example, could be B2 or B3 as well) finishes and triggers C, build #1 (C#1).
B2 finishes and triggers C. The build is stacked since C#1 is still running.
B3 finishes and triggers C. As long as C#1 build is running, other builds are stacked and if they are triggered the same way (i.e. C is not parameterized build or parameters are the same), the stacked builds are merged into one. Thus, only one build of C (C#2) keeps stacked.
C#1 finishes and the next build in the queue (C#2) is started. As C#2 was merged (from triggers B2,B3), the build queue is now empty.
C finishes C#2.
As you can see, C was only run twice.
There is a workaround, though. Make C parameterized and supply different values (for example job name of the trigger). Or allow concurrent builds of C - but you must ensure it won't access the same shared resource (e.g. by critical section exclusion).
I am in need of little help here. appreciate any pointer on this issue.
One of my projects (MultiJob) has few phases (p1, p2, p3). I have set the conditions to proceed to each phase.
Condition from P1 to P2 is only if P1 successful
Condition from P2 to P3 is only if P2 is Failed
Everything would be fine except in a case where P2 succeeds. Whenever P2 succeeds, P3 does not run (this is what I want) but it marks Jenkins job as UNSTABLE.
Am I missing something here? Is this is the right approach to handle the cases I have? Please suggest
Use conditional build step plugin : https://wiki.jenkins-ci.org/display/JENKINS/Conditional+BuildStep+Plugin
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