Search code examples
jenkinsjenkins-pipelinehudsonparallel-builds

Better parallelization of jenkins build nodes


I have a Jenkins jobs that runs as so (very simplified but the structure is there):

#!/usr/bin/env groovy
node('my_label'){
  timestamps{
    build()
    postBuild()
  }
}

def build(){
  parallel{
    flavorABuild: {
      if(condtionA){
        node(my_label){
          stage("build flavor a"){
            sh buildcommand
          }
        }
      }
    }
    flavorBBuild: {
      if(condtionB){
        node(my_label){
          stage("build flavor B"){
            sh buildcommand
          }
        }
      }
    }
    flavorCBuild: {
      if(condtionC){
        node(my_label){
          stage("build flavor C"){
            sh buildcommand
          }
        }
      }
    }
  }
}

This works fine for my purposes as far as functionality goes, but when any of those conditions (build parameter check boxes) are not checked for building a specific flavor, that build still shows up in my BlueOcean view as a parallel build step just with no actions in it (automatically succeeded).

Is there a better/cleaner builtin way to generate conditional parallel builds? All suggestions welcome, however I am trying to avoid adding more plugins.


Solution

  • You can simply omit adding the stages to the parallel execution map. Refer to the following.

    #!/usr/bin/env groovy
    node('my_label'){
      timestamps{
        parallel getParallelStages() 
      }
    }
    
    def getParallelStages() {
       def stageMap = [:]
       if(ConditionA) {
          stageMap['flavorABuild'] = { node(my_label){
            stage("build flavor a"){
              sh buildcommand
              }
            }
          }
       }
    
       if(ConditionB) {
          stageMap['flavorBBuild'] = { node(my_label){
            stage("build flavor B"){
              sh buildcommand
              }
            }
          }
       }
    return stageMap
    }