Rya*_*nes 18 jenkins jenkins-pipeline
我试图使用groovy DSL编写一个脚本化的Jenkins文件,它将在一组阶段中具有并行步骤.
这是我的jenkinsfile:
node {
stage('Build') {
sh 'echo "Build stage"'
}
stage('API Integration Tests') {
parallel Database1APIIntegrationTest: {
try {
sh 'echo "Build Database1APIIntegrationTest parallel stage"'
}
finally {
sh 'echo "Finished this stage"'
}
}, Database2APIIntegrationTest: {
try {
sh 'echo "Build Database2APIIntegrationTest parallel stage"'
}
finally {
sh 'echo "Finished this stage"'
}
}, Database3APIIntegrationTest: {
try {
sh 'echo "Build Database3APIIntegrationTest parallel stage"'
}
finally {
sh 'echo "Finished this stage"'
}
}
}
stage('System Tests') {
parallel Database1APIIntegrationTest: {
try {
sh 'echo "Build Database1APIIntegrationTest parallel stage"'
}
finally {
sh 'echo "Finished this stage"'
}
}, Database2APIIntegrationTest: {
try {
sh 'echo "Build Database2APIIntegrationTest parallel stage"'
}
finally {
sh 'echo "Finished this stage"'
}
}, Database3APIIntegrationTest: {
try {
sh 'echo "Build Database3APIIntegrationTest parallel stage"'
}
finally {
sh 'echo "Finished this stage"'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想要有3个阶段:Build; 集成测试和系统测试.在两个测试阶段中,我想要并行执行3组测试,每组测试针对不同的数据库.
我有3个可用的执行者.一个在master和2个代理上,我希望每个并行步骤在任何可用的执行程序上运行.
我注意到的是,在运行我的管道之后,我只看到了3个阶段,每个阶段都标记为绿色.我不想查看该阶段的日志,以确定该阶段中的任何并行步骤是否成功/不稳定/失败.
我希望看到我的测试阶段中的3个步骤 - 标记为绿色,黄色或红色(成功,不稳定或失败).
我已经考虑将测试扩展到他们自己的阶段,但已经意识到并不支持并行阶段(有谁知道这是否会得到支持?),所以我不能这样做,因为管道需要太长时间才能完成.
非常感谢任何见解,谢谢
Ed *_*all 38
在Jenkins脚本化管道中,parallel(...)采用Map来描述要构建的每个阶段。因此,您可以以编程方式预先构建构建阶段,该模式可实现灵活的串行/并行切换。
我使用了与此类似的代码,其中prepareBuildStages返回一个Map列表,每个List元素是按顺序执行的,而Map在那一点描述了并行阶段。
// main script block
// could use eg. params.parallel build parameter to choose parallel/serial
def runParallel = true
def buildStages
node('master') {
stage('Initialise') {
// Set up List<Map<String,Closure>> describing the builds
buildStages = prepareBuildStages()
println("Initialised pipeline.")
}
for (builds in buildStages) {
if (runParallel) {
parallel(builds)
} else {
// run serially (nb. Map is unordered! )
for (build in builds.values()) {
build.call()
}
}
}
stage('Finish') {
println('Build complete.')
}
}
// Create List of build stages to suit
def prepareBuildStages() {
def buildList = []
for (i=1; i<5; i++) {
def buildStages = [:]
for (name in [ 'one', 'two', 'three' ] ) {
def n = "${name} ${i}"
buildStages.put(n, prepareOneBuildStage(n))
}
buildList.add(buildStages)
}
return buildList
}
def prepareOneBuildStage(String name) {
return {
stage("Build stage:${name}") {
println("Building ${name}")
sh(script:'sleep 5', returnStatus:true)
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于可嵌套在并行块中的内容有某些限制,有关确切详细信息,请参阅管道文档。不幸的是,尽管它的灵活性不如脚本化(IMHO),但很多参考似乎偏向于声明式管道。该管道的例子页面是最有帮助的。
ada*_*lev 12
这是一个基于@Ed Randall 帖子的没有循环或函数的简单示例:
node('docker') {
stage('unit test') {
parallel([
hello: {
echo "hello"
},
world: {
echo "world"
}
])
}
stage('build') {
def stages = [:]
stages["mac"] = {
echo "build for mac"
}
stages["linux"] = {
echo "build for linux"
}
parallel(stages)
}
}
Run Code Online (Sandbox Code Playgroud)
...产生这个:
请注意,Map 的值不需要是阶段。你可以直接给出步骤。
Lam*_*bda 11
这是他们的文档中的一个示例:
并行执行
上一节中的示例在线性系列的两个不同平台上运行测试。实际上,如果执行make检查需要30分钟才能完成,那么“测试”阶段现在将需要60分钟完成!
幸运的是,Pipeline具有内置功能,可并行执行脚本化管线的各个部分,并以适当的并行步骤实现。
重构上面的示例以使用并行步骤:
// Jenkinsfile (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 .. */
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
在这里简化@Ed Randall 的回答。记住这是 Jenkinsfile 脚本化的(不是声明性的)
stage("Some Stage") {
// Stuff ...
}
stage("Parallel Work Stage") {
// Prealocate dict/map of branchstages
def branchedStages = [:]
// Loop through all parallel branched stage names
for (STAGE_NAME in ["Branch_1", "Branch_2", "Branch_3"]) {
// Define and add to stages dict/map of parallel branch stages
branchedStages["${STAGE_NAME}"] = {
stage("Parallel Branch Stage: ${STAGE_NAME}") {
// Parallel stage work here
sh "sleep 10"
}
}
}
// Execute the stages in parallel
parallel branchedStages
}
stage("Some Other Stage") {
// Other stuff ...
}
Run Code Online (Sandbox Code Playgroud)
请注意花括号。这将导致以下结果(使用 BlueOcean Jenkins 插件):
Rob*_*les -1
我已经stage{}
在并行块中使用过几次。然后每个阶段都会显示在阶段视图中。包含的父阶段parallel
不包括所有并行阶段的计时,但每个并行阶段都显示在阶段视图中。
在蓝海中,并行的阶段是分开出现的,而不是阶段展示的。如果有父级,它会显示为并行级的父级。
如果您没有相同的体验,则可能需要进行插件升级。
归档时间: |
|
查看次数: |
27107 次 |
最近记录: |