Allow timer to start jenkins pipeline only if a global environment variable is set

69 Views Asked by At

I have set a global environment varaiable in jenkins - flag which takes in values on and off

I have multiple declarative pipelines in jenkins, some of which get triggered based on timers.

Now I don't want the timers to trigger the scheduled pipelines when flag is set to on.

One way is to use the when {expression {env.flag == 'off' }} in all the stages in all the pipelines triggered by timer. I can't even apply it to stages at once but instead have to apply it individually to all stage inside stages. Is there a better way?

How can I accomplish this, preferably without installing any new plugins?

1

There are 1 best solutions below

0
fakolours On

One solution could be to throw an error if flag = on in the beginning of your pipeline, but that would mean having a failing pipeline as soon the flag is on. But you can avoid the FAILURE with some post function treatment.

Else you can try using scripted pipeline instead of declarative one :

node('your node'){
 if(!flag){
  stage(){...}
 }
}