如何使用Declarative语法在Jenkinsfile中声明许多代理?

时间:2018-03-27 08:48:52

标签: jenkins jenkins-pipeline

在Pipeline脚本语法中,我们使用此语法来声明特定节点的特定步骤:

steps{
    node('node1'){
        // sh
    }
    node('node2'){
        // sh
    }
}

但我想使用Pipeline Declarative Syntax,我可以放多个代理吗?

1 个答案:

答案 0 :(得分:4)

当然可以。请看一下示例(来自docs):

pipeline {
    agent none
    stages {
        stage('Build') {
            agent any
            steps {
                checkout scm
                sh 'make'
                stash includes: '**/target/*.jar', name: 'app' 
            }
        }
        stage('Test on Linux') {
            agent { 
                label 'linux'
            }
            steps {
                unstash 'app' 
                sh 'make check'
            }
            post {
                always {
                    junit '**/target/*.xml'
                }
            }
        }
        stage('Test on Windows') {
            agent {
                label 'windows'
            }
            steps {
                unstash 'app'
                bat 'make check' 
            }
            post {
                always {
                    junit '**/target/*.xml'
                }
            }
        }
    }
}