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

查看:95
本文介绍了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月)

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.

例如,如果您在管道中使用多个代理,但是要确保使用同一代理的阶段使用相同的工作空间,则可以在其上使用带有代理指令的父阶段,然后将所有它的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管道按代理分组的阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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