如何在“帖子"部分的詹金斯管道中使用“并行"选项? [英] How can i use 'parallel' option in jenkins pipeline in the 'post' section?

查看:10
本文介绍了如何在“帖子"部分的詹金斯管道中使用“并行"选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了许多管道示例以及如何在管道脚本中编写后期构建部分.但从来没有得到我正在寻找的答案.我有 4 个作业 - 比如说作业 A、B、C 和 D.我希望作业 A 先运行,如果成功,它应该并行触发作业 B、C、D.如果作业 A 失败,它应该只触发作业 B.如下所示:

I looked at many pipeline examples and how to write the post build section in a pipeline script. But never got my answer i was looking for. I have 4 jobs - say Job A,B,C and D. I want job A to run first, and if successful it should trigger Job B,C,D in parallel. If Job A fails, it should trigger only Job B. Something like below:

pipeline {
    agent any

stages {
    stage('Build_1') {
        steps {
            sh '''
               Build Job A
            '''
        }
    }

post {
    failure {
        sh '''
            Build Job B
        '''
    }
    success {
         sh '''
             Build Job B,C,D in parallel
         '''
    }
}
}

我尝试在帖子部分使用并行"选项,但它给了我错误.有没有办法在帖子成功"部分并行构建作业 B、C、D?

I tried using 'parallel' option in post section but it gave me errors. Is there a way to build Job B,C,D in parallel, in the post 'success' section?

提前致谢!

推荐答案

parallel 关键字实际上可以在后置条件中工作,只要它被封装在脚本块中,因为脚本块只是脚本化管道的后备这将允许您在任何地方运行并行执行步骤.
以下应该可以正常工作:

The parallel keyword actually can work inside a post condition as long as it is encapsulated inside a script block, as the script blocks is just a fallback to the scripted pipeline which will allow you to run parallel execution step wherever you want.
The following should work fine:

pipeline {
    agent any
    stages {
        stage('Build_1') {
            steps {
                //  Build Job A
            }
        }
    }

    post {
        failure {
            // run job B
            build job: 'Job-B'
        }
        success {
            script {
                // run jobs B, C, D in parallel 
                def jobs = ['Job-B', 'Job-C', 'Job-D']
                parallel jobs.collectEntries { job ->
                    ["Building ${job}" : {
                        build job: job
                    }]
                }
            }
        }
    }
}

这只是一个示例,具体的参数或配置(对于 build 关键字)可以根据您的需要添加到每个作业执行中.

This is just an example and specific parameters or configuration (for the build keyword) can be added to each job execution according to your needs.

这篇关于如何在“帖子"部分的詹金斯管道中使用“并行"选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆