How to run multiple stages on the same node with declarative Jenkins pipeline?

后端 未结 4 631
失恋的感觉
失恋的感觉 2021-01-31 02:54

Goal
Run multiple stages of a declarative Jenkins pipeline on the same node.

Setup
This is just a minimal example to show the p

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 02:59

    You could define stages inside a script block. Those stages are kind of sub-stages of a parent stage running in a given agent. That was the approach that I had to use in a similar use case than yours.

    #!groovy
    
    windowsNode = 'windows'
    
    pipeline {
      agent none
      stages {
        stage('Stage A') {
          agent {
            label windowsNode
          }
          steps {
            script {
    
              stage('Stage 1') {        
                windowsNode = NODE_NAME
                echo "windowsNode: $windowsNode, NODE_NAME: $NODE_NAME"
              }
    
              stage('Stage 2') {
                echo "windowsNode: $windowsNode, NODE_NAME: $NODE_NAME"
              }
            }
          }
        }
      }
    }
    

提交回复
热议问题