Skipping a stage in Jenkins pipeline without invoking agent?
Asked Answered
Z

2

5

We use the jenkins pipeline dsl for our job descriptions. Now we have something like that:

pipeline {
  agent none 

  options {
    timestamps() 
  }

  environment { 
    //SOME ENV VARS
  }

  stages {
    // more stages
    stage('stage1'){
    when {
         expression { env.BRANCH_NAME == 'master' }
         }
    agent { label 'slave1' }
    steps{
       //doing stuff
    }
  }
}

A stage in the build process that should only run when the master branch is build, you can think of a deploy job or something in that direction. The problem is, our resources of agents with that particular label are limited. When we build other branches the job still invoke a slave1 agent and than skips the stage after checking the that the branch is not master. This is bad, because when all slave1 agents are working on master branch jobs, the other jobs will have to wait for a slave1 agent becoming available just to check that it does need to run that stage.

Is there any way with the jenkins pipeline DSL to skip that stage without waiting for the slave1 agent to determine the branch?

Zincography answered 4/8, 2017 at 6:36 Comment(4)
Have you declared agent none at the top-level of your pipeline, i.e. before your stages?Joanjoana
Yes I have. See editZincography
Is this multibranch Pipeline ? If so, try using when { branch 'master' } conditionLinage
Thanks for the hint, I tried that, but unfortunatly it has the same behaviour. But it's much more readable so I'll stick with it.Zincography
S
16

The approach provided in the accepted answer works because you are not declaring an agent in the stage('stage1'). Instead you explicitly create a node within the steps and so the agent is not created when you check the condition.

This approach will work but it adds unnecessary complexity to your Jenkinsfile. As of Jenkins pipeline plugin version 1.2.6, the correct way to achieve this would be:

pipeline {
  agent none 
  stages {
    // more stages
    stage('stage1'){
      when {
        beforeAgent true
        branch 'master'
      }
      agent { label 'slave1' }
      steps { 
        // do stuff 
      }
    }
  }
}

Check the syntax and available options available in the when tag documentation.

Strata answered 21/4, 2018 at 21:6 Comment(2)
Thanks for the update. I will change that. But beforeAgent was introduced with Version 1.2.6 of the pipeline plugin on 12 Jan 2018 so it was not available when I asked the question or found the solution.Zincography
No problem, I am new to jenkinsfile as well and was not really aware about when this feature was introduced. I figured this one out recently while creating a jenkinsfile for one of my projects. :)Strata
Z
1

I found a solution that worked for me. Although I'm not quite sure why. Introducing a parallel section and using nodes however fixed the issue and the stages are skipped without invoking the agent first. See the modified pipeline:

pipeline {
  agent none 

  options {
   timestamps() 
  }

  environment { 
    //SOME ENV VARS
  }

  stages {
    // more stages
    stage('stage1'){
    when { branch 'master' }
    steps{
      parallel(
        'Job1': {
           node( 'slave1' ){
             //doing stuff
           }
        }
      )
    }
  }
}
Zincography answered 10/8, 2017 at 10:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.