jenkinsfile 管道按代理分组阶段

Rob*_*ert 5 jenkins jenkins-pipeline

我有什么

我正在尝试使用两个不同的代理运行我的 jenkins 管道。我想在同一个代理上执行一些过程,但到目前为止我无法做到这一点,因为代理定义只有 2 个选项:我可以在管道顶部执行,或者我可以将代理定义到每个阶段。我有这个:

pipeline{
    agent none
    stages {
        stage("Unit Testing"){
            agent { label 'maven-build-slave' }
            steps{
            }
        }
        stage('Sonar Scanner - Quality Gates') {
            agent { label 'maven-build-slave' }
            steps{
            }
        }
        stage("Integration"){
            agent { label 'integration-slave' }
            steps{
            }
        }
        stage('SoapUI') {
            agent { label 'integration-slave' }
            steps{
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,主要问题是即使代理相同,代码也会在每个阶段被拉取。

我想要什么

我想要这样的东西:

pipeline{
    agent none
    stages {
        agent { label 'maven-build-slave' }
        stage("Unit Testing"){
            steps{
            }
        }
        stage('Sonar Scanner - Quality Gates') {
            steps{
            }
        }

        agent { label 'integration-slave' }
        stage("Integration"){
            steps{
            }
        }
        stage('SoapUI') {
            steps{
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是上面的定义失败了,所以我想知道是否有人知道使用同一个代理运行多个阶段的方法。

Von*_*onC 8

查看声明性管道 1.3 中的新(2018 年 7 月)顺序阶段

使用相同的代理、环境或选项运行多个阶段

虽然顺序阶段功能最初是由希望在并行分支中拥有多个阶段的用户驱动的,但我们发现能够将多个阶段与相同的代理、环境、时间等组合在一起还有很多其他用途。

例如,如果您在流水线中使用多个代理,但希望确保使用相同代理的阶段使用相同的工作空间,您可以使用带有代理指令的父阶段,然后在其内部的所有阶段stage 指令将在同一个工作区中的同一个执行器上运行。

pipeline {
    agent none

    stages {
        stage("build and test the project") {
            agent {
                docker "our-build-tools-image"
            }
            stages {
               stage("build") {
                   steps {
                       sh "./build.sh"
                   }
               }
               stage("test") {
                   steps {
                       sh "./test.sh"
                   }
               }
            }
            post {
                success {
                    stash name: "artifacts", includes: "artifacts/**/*"
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)