我正在尝试编写一个并行执行一系列步骤的Jenkinsfile。目标是拥有两个agents (也就是。nodes)。一个应该是windows版本,另一个应该是linux版本。然而,我不希望这是顺序发生的,而是并行发生的。我正在尝试查找Pipeline - Parallel execution of tasks中描述的parallel指令的文档。
我在Jenkins上发现了一个parallel one实例,但文档似乎被破坏了:https://jenkins.io/doc/pipeline/steps/workflow-cps/
parallel: Execute in parallel
org.kohsuke.stapler.NoStaplerConstructorException:
There’s no @DataBoundConstructor on any constructor of class
org.jenkinsci.plugins.workflow.cps.steps.ParallelStep我应该如何设置Jenkinsfile,以便在两个不同的代理(一个linux,一个windows)上并行执行一系列构建步骤?
特别是,我应该使用声明式还是基于脚本的管道DSL?
发布于 2017-05-11 21:36:49
您可以使用声明式或基于脚本的方式进行并行工作。可以在以下位置找到基于脚本的并行文档:https://jenkins.io/doc/book/pipeline/jenkinsfile/#advanced-scripted-pipeline
他们给出了下面的例子。
stage('Build') {
/* .. snip .. */
}
stage('Test') {
parallel linux: {
node('linux') {
checkout scm
try {
unstash 'app'
sh 'make check'
}
finally {
junit '**/target/*.xml'
}
}
},
windows: {
node('windows') {
/* .. snip .. */
}
}
}对于声明式,我相信你会这样做:
stage('Build') {
steps {
parallel (
"Windows" : {
echo 'done'
},
"Linux" : {
echo 'done'
}
)
}
}由于声明性管道1.2,首选的声明性语法是:
pipeline {
agent none
stages {
stage('Run Tests') {
parallel {
stage('Test On Windows') {
agent {
label "windows"
}
steps {
bat "run-tests.bat"
}
post {
always {
junit "**/TEST-*.xml"
}
}
}
stage('Test On Linux') {
agent {
label "linux"
}
steps {
sh "run-tests.sh"
}
post {
always {
junit "**/TEST-*.xml"
}
}
}
}
}
}
}发布于 2020-02-05 18:48:06
Declarative Matrix是运行并行任务的一个很好的特性。它允许您执行定义的阶段(包括构建后操作),用于矩阵指令中定义的每个配置,而无需代码重复。
示例:
pipeline {
agent none
stages {
stage('Test') {
matrix {
agent {
label "${PLATFORM}-agent"
}
axes {
axis {
name 'PLATFORM'
values 'linux', 'windows'
}
}
stages {
stage('Test') {
steps {
echo "Do Test for ${PLATFORM}"
}
}
}
post {
always {
junit "**/TEST-*.xml"
}
}
}
}
}
}引用詹金斯blog post的话:
没有矩阵创建的等效管道很容易就会大几倍,并且更难理解和维护。
https://stackoverflow.com/questions/43913698
复制相似问题