gpt4 book ai didi

jenkins - 在调用并行阶段之前运行脚本(存储)

转载 作者:行者123 更新时间:2023-12-05 03:36:29 27 4
gpt4 key购买 nike

我有一个并行阶段设置,想知道是否可以在嵌套阶段之前运行脚本,所以像这样:

stage('E2E-PR-CYPRESS') {
when {
allOf {
expression {
return fileExists("cypress.json")
}
branch "PR-*"
}
}
steps {
script {
stash name: 'cypress-dir', includes: 'cypress/**/*'
}
}
parallel {
stage('Cypress Tests 1') {
agent { label 'aws_micro_slave_e2e' }
options { skipDefaultCheckout() }
steps {
runE2eTests()
}
}
stage('Cypress Tests 2') {
agent { label 'aws_micro_slave_e2e' }
options { skipDefaultCheckout() }
steps {
runE2eTests()
}
}
}
post {
always {
e2eAfterCypressRun(this, true)
}
}
}

我知道上面是错误的,我收到错误 Only one of "matrix", "parallel", "stages", or "steps"allowed for stage "E2E-PR-CYPRESS"

我在管道开始时的设置阶段已经有了存储脚本,但我希望能够在 Jenkins 上从上面的这个阶段重新启动,因此需要这个阶段的存储部分作为并行阶段需要解开内容。

最佳答案

更新的答案:

在玩了一下 Restart from a Stage 之后选项似乎有一个很好的功能,它是专门为您的需求而设计的,称为 Preserving stashes for Use with Restarted Stages:

Normally, when you run the stash step in your Pipeline, the resultingstash of artifacts is cleared when the Pipeline completes, regardlessof the result of the Pipeline. Since stash artifacts aren’t accessibleoutside of the Pipeline run that created them, this has not createdany limitations on usage. But with Declarative stage restarting, youmay want to be able to unstash artifacts from a stage which ran beforethe stage you’re restarting from.

To enable this, there is a job property that allows you to configure amaximum number of completed runs whose stash artifacts should bepreserved for reuse in a restarted run. You can specify anywhere from1 to 50 as the number of runs to preserve.

This job property can be configured in your Declarative Pipeline’s options section, as below:

options {
preserveStashes()
// or
preserveStashes(buildCount: 5)
}

这个内置功能正是您解决问题所需的功能,无需对代码进行任何特殊修改,因为它允许您从任何阶段重新运行管道并仍然使用之前隐藏的现有文件。

原答案:

实际上,您可以使用 parallel 命令的脚本语法非常简单地实现这一点,它还可以让您避免并行阶段的重复代码。

parallel: Execute in parallel
Takes a map from branch names to closures and an optional argument failFast which will terminate all branches upon a failure in any other branch:

parallel firstBranch: {
// do something
}, secondBranch: {
// do something else
},
failFast: true|false

在您的情况下,它可能看起来像:

stage('E2E-PR-CYPRESS') {
when {
allOf {
expression {
return fileExists("cypress.json")
}
branch "PR-*"
}
}
steps {
script {
stash name: 'cypress-dir', includes: 'cypress/**/*'

// Define the parallel execution stages
def stages = ['Cypress Tests 1', 'Cypress Tests 2']

// Create the parallel executions and run them
parallel stages.collectEntries {
["Running ${it}": {
node('aws_micro_slave_e2e') {
skipDefaultCheckout()
runE2eTests()
}
}]
}
}
}
post {
always {
e2eAfterCypressRun(this, true)
}
}
}

通过这种方式,您可以通过更新 stages 列表轻松添加更多并行步骤,甚至可以将其作为输入参数接收。此外,您可以通过不同的标签或测试套件创建并行执行,而不是阶段名称。

关于jenkins - 在调用并行阶段之前运行脚本(存储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69573538/

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