使用轻量级执行程序进行声明式管道阶段(代理无) [英] Use a lightweight executor for a declarative pipeline stage (agent none)

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

问题描述

我正在使用具有声明性语法的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天全站免登陆