nozzleman
nozzleman

Reputation: 9669

Skip Jenkins Stage if its last excution was successfull

I am looking for way to speed up our Build-Pipeline. The biggest impact would be to do certain things only if they have not been done.

So basically, I have already parameterized some optional stages which works fine, but there are some things I'd like to skip if there was a execution before which was successfull.

I have searched the docs, especially the section ref. when but the was nothing that did the job.

So the question is: Is there something I can do in a declarative pipeline to always skip a stage, except when

  1. It has never been run successfully before
  2. The last time it ran was not successful (eg. if I allow for its execution to be forced by passing a parameter)

I though about using the Build Number (eg only run it on the first execution of the pipeline), but this does't cut it for 2 reasons

  1. I'm using milestones to prevent multiple executions of the pipe for a given branch/PR
  2. If the first run fails, it would never be tried again

Oh, and I also thought about putting all of the logic in post -> regression and forcing the stage to fail on the first pipeline run, but this doesn't seem to be a good idea either.

Upvotes: 0

Views: 319

Answers (2)

Noam Helmer
Noam Helmer

Reputation: 6859

One option ,although not ideal for large scale, can be to store the diffrent states as Global Environment Variables. (Manage Jenkins -> Configure System -> Global properties -> Environment variables).
These parameters are available for all jobs and can store parameters in a 'server wide' scope.
You can then update or set them via code using the following groovy method:

@NonCPS
def updateGlobalEnvVariable(String name, String value) {
    def globalNodeProperties = Jenkins.getInstance().getGlobalNodeProperties()
    def envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)

    if (envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0) {
        def envVarsNodePropertyClass = this.class.classLoader.loadClass('hudson.slaves.EnvironmentVariablesNodeProperty')
        globalNodeProperties.add(envVarsNodePropertyClass.newInstance())
    }
    envVarsNodePropertyList.get(0).getEnvVars().put(name, value)
}

This function will create the parameter if it does not exists or update its value in case it already exists. Also this function should better be placed in a Shared Library from which it will be available for all pipelines.

A nice advantage for this technique is that you can always 'reset' the different stages from the configuration page, However if you need to store multiple stages it can overflow the configuration page with a bit too much information.

Upvotes: 1

Colin Moreno Burgess
Colin Moreno Burgess

Reputation: 1602

Maybe you could use custom shared-workspace and then create/store some state file or something that could be used by your next executions like lastexecfailed.state and then try to locate the file on the shared workspace at the beginning of your execution.

Upvotes: 1

Related Questions