本文介绍了jenkinsfile管道按代理分组的阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两个不同的代理程序来运行我的jenkins管道.我想在同一个代理上执行某些过程,但是到目前为止,我无法执行此操作,因为只有两个选项可以定义代理:我可以在管道顶部执行操作,也可以在每个阶段定义代理.我有这个:

I am trying to run my jenkins pipeline using two different agents. I want to execute some process on the same agent but so far I am unable to do this because there are only 2 options for agent definition: I can do at top of pipeline or I can define the agent into each stage. I have this:

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{
            }
        }
    }
}

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

In this case the main problem is that the code is pulled in every stage even when the agent is the same.

我想要这样的东西:

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{
            }
        }
    }
}

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

But the definition above is failing so I wonder if anyone knows a way to run several stages using same agent.

推荐答案

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

Check out the new (July 2018) Sequential Stages in Declarative Pipeline 1.3:

例如,如果您在管道中使用多个代理,但是要确保使用同一代理的阶段使用相同的工作空间,则可以在其上使用带有代理指令的父阶段,然后将所有它的stages指令内的stages将在同一工作区中的同一执行程序上运行.

For example, if you are using multiple agents in your Pipeline, but would like to be sure that stages using the same agent use the same workspace, you can use a parent stage with an agent directive on it, and then all the stages inside its stages directive will run on the same executor, in the same workspace.

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/**/*"
                }
            }
        }

这篇关于jenkinsfile管道按代理分组的阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 22:56