jenkinsfile 管道按代理分组阶段 [英] jenkinsfile pipeline grouping stages by agent

查看:20
本文介绍了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 月)Declarative Pipeline 1.3 中的顺序阶段:

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

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

Running Multiple Stages with the Same agent, or environment, or options

While the sequential stages feature was originally driven by users wanting to have multiple stages in parallel branches, we’ve found that being able to group multiple stages together with the same agent, environment, when, etc has a lot of other uses.

例如,如果您在管道中使用多个代理,但希望确保使用相同代理的阶段使用相同的工作区,您可以使用带有代理指令的父阶段,然后所有其阶段指令中的阶段将在同一工作区中的同一执行器上运行.

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 管道按代理分组阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆