Magesh
Magesh

Reputation: 308

How to conditionally trigger a build in Jenkins

I have a couple of jobs both would trigger on Monday but 1 st project on 1st Monday and 2nd project on 2nd Monday. I couldn't use the Jenkins cron to trigger the project based on condition because cron can be used to trigger a project on a specific time of the day or week of the day alone (eg 0 6 ** 1 to trigger project on Monday morning 6 AM) and not based on condition.

I am looking for a solution where I could use the cron to trigger the project but it would have some condition only if it passes the project would be triggered.

Upvotes: 0

Views: 4497

Answers (2)

praveen kumar
praveen kumar

Reputation: 26

Currently jenkins cron doesn't support triggering project based on condition. You can do this in two ways

  1. i)The simplest way is to create two projects, one for running in even week and other for odd week.

ii)Once the project is triggered you write a condition either using windows batch command or shell script to check if the current week is as expected (Odd/Even) if it meet the condition you proceed the next step or fail it.

But the issue with this solution is you will have a unnecessary project.

  1. i)A bit hard but stable way is have a Parent Project and two dependent(child) project - i.e one for even and other for odd

ii)In the parent project execute a shell/windows batch command to check if the current week is odd or even refer - Date convert to week number based on it you create a variable and set value as true or false and then write the values into a property file

iii)Then convert the value as a environment variable using Jenkins Environment Injector Plugin (url - https://plugins.jenkins.io/envinject/) for details on how to convert refer - How to set environment variables in Jenkins?, Now you can access the value across the project

iv)Create a new conditional build step (Plugin url - https://plugins.jenkins.io/conditional-buildstep/ ) In that trigger your child project based on comparison of environment variable with a expected condition (i.e if "TRUE" then trigger Odd week project)

Upvotes: 1

MaratC
MaratC

Reputation: 6859

You can trigger the build, check the condition, and bail out of the build if the condition does not hold. You can optionally fail the build in this case.

Upvotes: 1

Related Questions