0

We have "stage (build)" on all our branches. Temporarily how can we skip this stage to run on all our branches in multibranch pipeline. I know one solution is use when condition on stage and ask all developers to pull that branch into their branch. But thats lot of work and coordination. Instead I am looking for a global configuration where we can simply skip the stage by name on any branch.

suresh
  • 254
  • 5
  • 9

3 Answers3

1

It sounds like you keep the Jenkinsfile alongside the code, but want to change how the Jenkinsfile runs from an administrative point of view.

You could store this stage in a separate (version control flavor of preference) repository. Before you execute the stage, load in the repository then load in the script file and within the stage execute a method defined in that file.

Alternatively, add a parameter to the build job (this assumes you aren't setting parameters via options in the Jenkinsfile) that is a boolean to use or skip the stage and you can modify the project configuration when you want to turn it on or off

jeubank12
  • 891
  • 9
  • 17
0

What about this approach ?

  stage('build') { 

    if(buildFlag=="disable"){
      //jump to next stage
      return;
    }
    //do something
  } 

buildFlag could be

  • a simple var in the same pipeline
  • a global var configured in jenkins. You could set disabled temporarily to skip build stage and change to enabled to restore to normalcy

Also you could set the status to failure, instead of return :

currentBuild.result = 'FAILURE'

Or throw an exception and exit the entire job or pipeline.

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
  • It is not possible to do this. WorkflowScript: 79: Not a valid stage section definition: "if(buildFlag){ – David Marciel May 07 '20 at 15:46
  • Did you create the variable? This snippet works in a pipeline script or declarative pipeline. Don't blame others (downvote) for your incompetence. – JRichardsz May 07 '20 at 19:51
  • Idont blame you, bu It doesn´t work, you must enclose it in stage('stage name') { steps { script { if(condition) { } } } } – David Marciel May 08 '20 at 07:40
0

One option is to skip the body of the stage if the condition is met. For example:

  stage('Unit Tests') {
    steps {
      script{
        if (env.branch.startsWith(releaseBranch)} {
           echo 'Running unit tests with coverage...'
        } else {
           // run actual unit tests
        }
      }
    }
  }

The only downside is that the UI will show the "green box" for this step - even though it effectively did nothing.

If you want to remove the stage completely for the branch, use a when directive.

stage('deploy') {
        when {
            branch 'production'
        }
        steps {
            echo 'Deploying'
        }
 }

Bonus: You can also specify a "when not" directive as well. See How to specify when branch NOT (branch name) in jenkinsfile?

Charlie Dalsass
  • 1,986
  • 18
  • 23