将轻量级执行器用于声明性管道阶段(代理无) [英] Use a lightweight executor for a declarative pipeline stage (agent none)

查看:20
本文介绍了将轻量级执行器用于声明性管道阶段(代理无)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有声明式语法的 Jenkins Pipeline,目前有以下几个阶段:

I'm using Jenkins Pipeline with the declarative syntax, currently with the following stages:

  1. 准备
  2. 构建(两组平行的步骤)
  3. 测试(也是两组平行的步骤)
  4. 询问是否/在哪里部署
  5. 部署

对于第 1、2、3 和 5 步,我需要代理(执行者),因为他们在工作区中进行实际工作.对于第 4 步,我不需要一个,并且我不想在等待用户输入时阻止我可用的执行程序.对于经典的脚本化语法,这似乎被称为轻量级"或轻量级"执行器,但我找不到有关如何使用声明性语法实现此目的的任何信息.

For steps 1, 2, 3, and 5 I need and agent (an executor) because they do actual work on the workspace. For step 4, I don't need one, and I would like to not block my available executors while waiting for user input. This seem to be referred to as either a "flyweight" or "lightweight" executor for the classic, scripted syntax, but I cannot find any information on how to achieve this with the declarative syntax.

到目前为止我已经尝试过:

So far I've tried:

  1. 直接在管道选项中设置代理,然后在舞台上设置agent none.这不起作用,管道正常运行,在等待输入时阻塞执行程序.文档中还提到它不会有任何影响,但我想无论如何我都会试一试.
  2. 在管道选项中设置agent none,然后为除#4 之外的每个阶段设置一个代理.不幸的是,但在意料之中,这为每个阶段分配了一个新的工作空间,这反过来又需要我存储和取消存储.这既混乱又给我在并行阶段(2 和 3)中带来更多问题,因为我不能在 parallel 结构之外编写代码.我假设并行步骤在同一个工作区中运行,因此在两者中隐藏/取消隐藏会产生不幸的结果.
  1. Setting an agent directly in the pipeline options, and then setting agent none on the stage. This has no effect, and the pipeline runs as normalt, blocking the executor while waiting for input. It is also mentioned in the documentation that it will have no effect, but I thought I'd give it a shot anyway.
  2. Setting agent none in the pipeline options, and then setting an agent for each stage except #4. Unfortunately, but expectedly, this allocates a new workspace for every stage, which in turn requires me to stash and unstash. This is both messy and gives me further problems in the parallel stages (2 and 3) because I cannot have code outside the parallel construct. I assume the parallel steps run in the same workspace, so stashing/unstashing in both would have unfortunate results.

这是我的 Jenkinsfile 的大纲:

Here is an outline of my Jenkinsfile:

pipeline {
    agent {
        label 'build-slave'
    }
    stages {
        stage("Prepare build") {
            steps {
                // ...
            }
        }
        stage("Build") {
            steps {
                parallel(
                    frontend: {
                        // ...
                    },
                    backend: {
                        // ...
                    }
                )
            }
        }
        stage("Test") {
            steps {
                parallel(
                    jslint: {
                        // ...
                    },
                    phpcs: {
                        // ...
                    },
                )
            }
            post {
                // ...
            }
        }
        stage("Select deploy target") {
            steps {
                script {
                    // ... code that determines choiceParameterDefinition based on branch name ...
                    try {
                        timeout(time: 5, unit: 'MINUTES') {
                            deployEnvironment = input message: 'Deploy target', parameters: [choiceParameterDefinition]
                        }
                    } catch(ex) {
                        deployEnvironment = null
                    }
                }
            }
        }
        stage("Deploy") {
            when {
                expression {
                    return binding.variables.get("deployEnvironment")
                }
            }
            steps {
                // ...
            }
        }
    }
    post {
        // ...
    }
}

我在这里遗漏了什么,还是在当前版本中不可能?

Am I missing something here, or is it just not possible in the current version?

推荐答案

在顶层设置 agent none,然后在每个阶段设置 agent { label 'foo' },在 input 阶段再次使用 agent none 似乎对我来说按预期工作.

Setting agent none at the top level, then agent { label 'foo' } on every stage, with agent none again on the input stage seems to work as expected for me.

即执行某些工作的每个阶段都在同一个代理上运行,而 input 阶段不使用任何代理上的执行程序.

i.e. Every stage that does some work runs on the same agent, while the input stage does not consume an executor on any agent.

pipeline {
    agent none
    stages {
        stage("Prepare build") {
            agent { label 'some-agent' }
            steps {
                echo "prepare: ${pwd()}"
            }
        }
        stage("Build") {
            agent { label 'some-agent' }
            steps {
                parallel(
                    frontend: {
                        echo "frontend: ${pwd()}"
                    },
                    backend: {
                        echo "backend: ${pwd()}"
                    }
                )
            }
        }
        stage("Test") {
            agent { label 'some-agent' }
            steps {
                parallel(
                    jslint: {
                        echo "jslint: ${pwd()}"
                    },
                    phpcs: {
                        echo "phpcs: ${pwd()}"
                    },
                )
            }
        }
        stage("Select deploy target") {
            agent none
            steps {
                input message: 'Deploy?'
            }
        }
        stage("Deploy") {
            agent { label 'some-agent' }
            steps {
                echo "deploy: ${pwd()}"
            }
        }
    }
}

但是,不能保证在流水线中使用相同的代理标签最终会使用相同的工作空间,例如作为同一作业的另一个构建,而第一个构建正在等待 input.

However, there are no guarantee that using the same agent label within a Pipeline will always end up using the same workspace, e.g. as another build of the same job while the first build is waiting on the input.

您必须在构建步骤之后使用 stash.如您所见,目前使用 parallel 无法正常完成此操作,因此您必须另外使用 script 块,以便编写脚本管道的片段用于并行步骤之后/之前的存储/取消存储.

You would have to use stash after the build steps. As you note, this cannot be done normally with parallel at the moment, so you'd have to additionally use a script block, in order to write a snippet of Scripted Pipeline for the stashing/unstashing after/before the parallel steps.

这篇关于将轻量级执行器用于声明性管道阶段(代理无)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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