How can i use 'parallel' option in jenkins pipeline in the 'post' section?

岁酱吖の 提交于 2020-01-14 14:01:22

问题


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
         '''
    }
}
}

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?

Thanks in advance!


回答1:


The error message is quiet clear about this:

Invalid step "parallel" used - not allowed in this context - The parallel step can only be used as the only top-level step in a stages step

The more restrictive declarative syntax does not allow the usage of parallel in thw post section at the moment.

If you don't want to switch to the scripted syntax, another option that should work: Build the jobs B,C,D in parallel in a second stage and move the the failure condition in the post section of your first stage. As a result job B,C,D will run if A is successful. If A is not successful only job B will run.

pipeline {
    agent any

    stages {
        stage('one') {
            steps {
                // run job A
            }   
            post {
                failure {
                    // run job B
                }
            }
        }
        stage('two') {
            steps {
                parallel(
                     // run job B, C, D
                )

            }
        }
    }
}


来源:https://stackoverflow.com/questions/44806231/how-can-i-use-parallel-option-in-jenkins-pipeline-in-the-post-section

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!