7

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!

2 Answers 2

6

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

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.

1
3

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
                )

            }
        }
    }
}
2
  • Yes I have tried that and it worked (Using parallel as an additional stage and use one failure block).. But the issue i have is:
    – cad.sneha
    Jul 6, 2017 at 14:35
  • 1
    @cad.sneha The issue is missing in your comment.
    – Philip
    Jul 7, 2017 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.