Home > other >  skip other pipeline stages when executing a specific stage
skip other pipeline stages when executing a specific stage

Time:10-08

I have a declarative pipeline jenkinsfile, in which I have 4 stages. I would like to make so that whenever a specific stage executes(this stage is conditional therefor it will not always execute), all stages after this one are skipped. For example purposes let's say that this stage is the first stage. Does anyone knows a way to do it in a declarative pipeline?

Thanks in advance, Alon

CodePudding user response:

Define a boolean variable e.g. skip = false in the pipeline. When you enter your conditional stage set the variable to true. Make other stages (2-4) dependent on skip variable.

stage("1 - Your conditional stage") {
  when {
    ...
  }
  steps {
    script {
      skip = true
    }
}

stage("2") {
  when {
    expression {
      return skip == false
    }
  }
}
...
stage("4") {
  when {
    expression {
      return skip == false
    }
  }
}
  • Related