gpt4 book ai didi

jenkins - Jenkins 管道可以有一个可选的输入步骤吗?

转载 作者:行者123 更新时间:2023-12-04 11:33:47 25 4
gpt4 key购买 nike

是否可以创建具有可选输入阶段的 Jenkins 管道?

下面的代码片段没有达到这个目标。

预期行为

阶段(以及输入提示)应该只针对特定的分支运行。

实际行为

此阶段适用于所有分支。使用输入步骤时,将忽略 when 过滤器。

stage('Approve') {
when {
expression { BRANCH_NAME ==~ /^qa[\w-_]*$/ }
}
input {
message "Approve release?"
ok "y"
submitter "admin"
parameters {
string(name: 'IS_APPROVED', defaultValue: 'y', description: 'Deploy to master?')
}
}
steps {
script {
if (IS_APPROVED != 'y') {
currentBuild.result = "ABORTED"
error "User cancelled"
}
}
}
}

最佳答案

过滤器不会被忽略,它只是在输入步骤之后进行评估。在您的示例中,总是会询问您是否要部署,如果您不在 QA 分支上,则不会发生任何事情。

现在您可能会问为什么 Jenkins 不首先评估 'when' 指令。在这种情况下,您不能在 when 条件中使用输入参数。

并且拥有多个 when 指令就像在声明性管道中编写脚本一样。

但是,有一个表达式允许您控制何时计算 'when' 指令。这是 beforeAgent .它允许您在分配代理之前评估 when 语句。与此类似,您需要类似 beforeInput 之类的东西。您可以 create a feature request为了那个原因。

我不再使用 input 指令,而是现在在脚本块中使用 input,因为这提供了更大的灵活性,例如当有人必须批准某事时,我会发送 Slack 通知,这在声明式方法中是不可能的。为此,您需要一个通知指令。如果有,是在输入步骤之前还是之后进行评估?

你看,做所有声明性的事情并不总是最好的方式。所以我推荐的方法如下(免责声明:这是未经测试的!):

pipeline {
// We want to use agents per stage to avoid blocking our build agents
// while we are waiting for user input.
agent none
...
// The question mark naming convention is helpful to show you which
// approval stage belongs to which work stage.
stage('Release?') {
// Don't allocate an agent because we don't want to block our
// slaves while waiting for user input.
agent none
when {
// You forgot the 'env.' in your example above ;)
expression { env.BRANCH_NAME ==~ /^qa[\w-_]*$/ }
}
options {
// Optionally, let's add a timeout that we don't allow ancient
// builds to be released.
timeout time: 14, unit: 'DAYS'
}
steps {
// Optionally, send some notifications to the approver before
// asking for input. You can't do that with the input directive
// without using an extra stage.
slackSend ...

// The input statement has to go to a script block because we
// want to assign the result to an environment variable. As we
// want to stay as declarative as possible, we put noting but
// this into the script block.
script {
// Assign the 'DO_RELEASE' environment variable that is going
// to be used in the next stage.
env.DO_RELEASE = input ...
}
// In case you approved multiple pipeline runs in parallel, this
// milestone would kill the older runs and prevent deploying
// older releases over newer ones.
milestone 1
}
}
stage('Release') {
// We need a real agent, because we want to do some real work.
agent any
when {
// Evaluate the 'when' directive before allocating the agent.
beforeAgent true
// Only execute the step when the release has been approved.
environment name: 'DO_RELEASE', value: 'yes'
}
steps {
// Make sure that only one release can happen at a time.
lock('release') {
// As using the first milestone only would introduce a race
// condition (assume that the older build would enter the
// milestone first, but the lock second) and Jenkins does
// not support inter-stage locks yet, we need a second
// milestone to make sure that older builds don't overwrite
// newer ones.
milestone 2

// Now do the actual work here.
...
}
}
}

关于jenkins - Jenkins 管道可以有一个可选的输入步骤吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48821921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com