I have a working Jenkins scripted pipeline, very simple.
I am also using triggers to enable pipeline to run on a schedule.
Something like this:
node{
do_something()
triggers{
cron(‘* * * * *’)
}
}
do_something() does various things, and takes into account some input params (and env variables)
I want to trigger pipeline multiple times - lets say once a minute with some parameters, and every hour with other parameters.
(Imagine that I would like the same pipeline to be reused for continuous and full builds)
Is it possible?
I did not found a way how to do this, I am not even sure that it can be triggered by more than one trigger.
The trigger will trigger the entire job. It doesn't trigger parts of a job, or trigger with different parameters. What you probably want to do is setup a trigger job. Just have a simple pipeline job that triggers every minute, 10 minutes, whatever you need. That job will use logic in groovy to decide what needs to be done at that time and then trigger another job or jobs with the parameters you need.
Why don't you use the Parametrized Scheduler as trigger. This should do the trick for you:
triggers {
parameterizedCron('''
* * * * * %PARAM1=x;PARAM2=y
0 * * * * %PARAM3=z
''')
}
Related
We have a Jenkins pipeline which has a parameter called PIPELINE_ACTIONS, set by two checkboxes. The two checkbox options are:
FlashFirmware
RunTests
So it can run with PIPELINE_ACTIONS=FlashFirmware, PIPELINE_ACTIONS=RunTests and PIPELINE_ACTIONS=FlashFirmware,RunTests.
We are now trying to use Build Triggers to trigger the pipeline with specific parameters (Build periodically with parameters option in Jenkins). We would like to run it with PIPELINE_ACTIONS=FlashFirmware,RunTests but for some reason it does not work. Sounds like a formatting issue and I can't find any documentation on how to pass more than one value to a parameter with a cron string.
The current cron string looks like this:
# Runs every 2 minutes
H/2 * * * * % FW_VERSION=4.0C3;PIPELINE_ACTIONS=FlashFirmware,RunTests;
When we print PIPELINE_ACTIONS, it is empty. We tried with brackes, whitespaces, etc. but no combination worked.
How can it be achieved?
EDIT
Here is how the PIPELINE_ACTIONS parameter is defined:
For information, you can't use the default 'Build periodically' option to schedule a Jenkins job with parameters. You need to install the parameterized-scheduler plugin.
And With the Active Choices plugin, you might want to configure your parameter as follows:
You can of course use the scripted or declarative style (if you're using Jenkinsfile) to define your Active Choice parameter.
Finally schedule your build like the following:
H/2 * * * * %PIPELINE_ACTIONS=FlashFirmware,RunTests;FW_VERSION=1.0.0
How to schedule the parameterized jenkins job?
I have a parameterized parent job which has 6 child jobs. when the parent job is triggered it should start the child jobs one after another.I should run the parent job for every 7 hours on weekend which I have given as H */7 * * 6-7.
My questions are:
How does the parent job takes the parameters when it is triggered?
Does it take the default choice or do i need a script to give the parameters?
How to configure if i want to skip any one of the child job?
Use the Parameterized Scheduler Plugin
1) Here's an example of passing parameters via the scheduler. If your job is parameterized, the plugin will add an option "Build periodically with parameters".
H */7 * * 6-7 % Environment=development; Version=2,4,3
2) I don't think it honors a default, I have not seen that in the docs. Let us know if you find something.
3) Via the parameters?
I have a full set of unit tests I'd like to run daily overnight in Jenkins, but only if my application has built correctly in another job. I DON'T want the unit tests to trigger throughout the day as commits are added to the application.
How do I configure this? To restate: there are two Jenkins jobs:
A and B:
A runs each checkin, unless B is running, in which case it waits for B.
B runs at midnight, IFF A is in a good state. If A is running, B waits for A.
I already have A set up as "A runs each checkin."
I assume you are using Jenkins pipeline. There might be many ways but I would address this by adding a new stage in JOB B that check the status of JOB A and a utility function to check status.
stage('check Job A status'){
// If A is running, B waits for A.
if(checkStatus() == "RUNNING" ){
timeout(time: 60, unit: 'MINUTES') {
waitUntil {
def status = checkStatus()
return (status == "SUCCESS" || status == "FAILURE" || status == "UNSTABLE" || status == "ABORTED")
}
}
}
// Proceed with B, only when A is in a good state
if( checkStatus() != "SUCCESS" ){
error('Stopping Job B becuase job A is not successful.')
}
}
def checkStatus() {
def statusUrl = httpRequest "https://jenkins.example.com/job/${job-A-Name}/lastBuild/api/json"
def statusJson = new JsonSlurper().parseText(statusUrl.getContent())
return statusJson['result']
}
My answer is a bit late to the party here (sorry :-7) but a useful question and not answered properly(sorry guys - not your fault - it took me/us a few years to find out the best different ways of doing this (originally I had some post-build groovy and other scripts doing funky things like triggering other jobs)). Actually jenkins has quite a flexible choice of methods for jobs that need to interact with one another.
There is a built in "Post-build Action: build other projects" and there are a couple of plugins which can be used. The "Post-build Action: build other projects" is probably most suitable. And the "Lockable Resources Plug-in" can be used to make the jobs mutually exclusive.
* SIMPLEST ANSWER: *
Install Lockable Resource plugin and add a lockable resource "build_or_test" and configure jobs A and B to lock on that resource.
Configure the build job A, Add Post-build Action: Build other projects
Build job B if job A is Stable.
* LIST of useful built-ins and plugins: *
It is also useful to use FSTrigger plugin, build jobs or other jobs may generate logs or image files or test reports. Jobs can be triggered to run when these files or directories appear or are updated. Jobs in remote jenkins or external to jenkins can be used to trigger jobs using this method.
Built in Post-build Action:
Build other projects
* Trigger only if build is stable
* Trigger even if the build is unstable
* Trigger even if the build fails
BuildResultTrigger Plug-in -
This plugin makes it possible to monitor the build results of other jobs.
Similar to "Post-build Action: build other projects" only at top of job config as a trigger with cron schedule.
Filesystem Trigger Plug-in -
The plug-in makes it possible to monitor changes of a file or a set of files in a folder.
Parameterized Trigger Plug-in (which adds Post-build Action:
Trigger parameterized build on other projects)
Similar to "Post-build Action: build other projects but convenient to pass build information e.g. in parameters.ini style file or boolean or other params from one job to another.
Lockable Resources Plug-in
This plugin allows to define external resources (such as printers, phones, computers) that can be locked by builds. If a build requires an external resource which is already locked, it will wait for the resource to be free.
Off the top of my head, I can't think of a way to do exactly what you want. But that might be because it is probably not the best way to handle it.
In job A, you should probably just not deploy/deliver the artifacts to the place where B will look unless the build is successful. Then B will always run against a successful build from A.
But without understanding your entire setup or environment, I can't really comment on what is "right". But maybe you need to rethink the problem?
You can publish a "state" on completion of Job A. Say a property file in your source code repo, or even in DB.
This value can be boolean. If Job A is running, value will be false till Job A build successfully.
Now, when Job B gets triggered, first check if the above value is true or not.
It seems there is no plugin to support this. Most of the plugins will trigger the Job B as soon as Job A is done (ie it will monitor status of Job A).
I'm using a Jenkinsfile to customize my build and figured out that the following sets polling:
triggers {
pollSCM '*/10 * * * *'
}
However, I need to add the option to ignore commits by certain users. I'm not seeing that option in the UI config. I'm configured to use Jenkins Pipeline. Anyone know how to do this in the Jenkinsfile?
If you dig into the currentBuild.changeSets variable, you can call getItems. This will return a collection of Entrys at which point you can look at calling getAuthor() on the Entry and getDisplayName() on the User object returned by getAuthor(). You can just add conditional logic around that getDisplayName() return value at that point.
I'm not 100% sure on how this would translate to the declarative pipeline syntax, and may require some method whitelisting.
The simple case where you just have one job depending on the completion of a set of other jobs is easy: either use a multijob or use the build flow plugin with parallel { ... }. The case I am trying to solve is more general, for example:
JobA depends on JobX and JobZ
JobB depends on JobY and JobZ
SuperJob depends on JobA and JobB
I want each of these jobs to trigger as soon as, and only when their prerequisites complete.
It would appear that neither the build flow plugin, nor the join plugin or the job DSL plugin have a good mechanism for this. I can, of course, just start all my jobs and have them poll Jenkins, but that would be quite ugly.
Another dead end is the "Upstream job trigger". I want to trigger off a specific build of a job, not just any run of an upstream job.
update
One answer mentions the multijob plugin. It can indeed be used to solve this problem, but the scheduling and total build time is almost always worst case. For example, assume this dependency graph, with the build times as indicated:
left1 (1m) right1 (55m)
| |
left2 (50m) right2 (2m)
|____________|
|
zip
With the multijob plugin, you get:
Phase 1:
left1, right1 // done in 55m
Phase 2:
left2, right2 // done in 50m
Phase 3:
zip // total time 105m
If I had a way to trigger the next job exactly when all prerequisites are done, then the total build time would be just 57m.
The answer here should explain how I can obtain that behavior, preferably without writing my own polling mechanism.
update 1 1/2
In the comments below, it was suggested I group the left tasks and the right tasks into a single subtask. Yes, this can be done in this example, but it is very hard to do this in general, and automatically. For example, assume there is an additional dependency: right2 depends on left1. With the build times given, the optimal build time should not change, since left1 is long done before right2 is launched, but without this knowledge, you can no longer lump left1 and left2 in the same group, without running the risk of not having right1 available.
update 2
It looks like there is no ready made answer here. It seems I am going to have to code up a system groovy script myself. See my own answer to the question.
update 3
We ended up forking the multijob plugin and writing new logic within. I hope we can publish it as a new plugin after some cleanup...
Since you added the jenkins-workflow tag, I guess that using Jenkins Workflow Plugin is ok to you, so perhaps this Workflow script fit your needs:
node {
parallel left: {
build 'left1'
build 'left2'
}, right: {
build 'right1'
build 'right2'
},
failFast: true
build 'zip'
}
This workflow will trigger zip as soon as both parallel branches finish.
As far as I can tell, there is no published solution to my problem, so I have to roll my own. The following system groovy script works, but can obviously use some enhancements. Specifically, I really miss a nice simple one page build status overview...
This gist implements my solution, including proper handling of job cancellations: https://gist.github.com/cg-soft/0ac60a9720662a417cfa
You can use Build other projects as Post Build Actions in the configuration of one of your parent job which would trigger second parent job on successful build of the job. When the second parent job also gets completed, trigger your child job by same method.
Multijob plugin could be used to make hierarchy of jobs.
First select Multijob Project in new item and then in configuration you can add as many jobs as you want. You need to also specify phase for each Job.