Search code examples
jenkinsjenkins-pluginsjenkins-pipeline

How to run two Jenkins multi phase Jobs at the same time?


I have two groups of multi-phase jobs, parallel test 1 and parallel test 2; where I need to execute both the groups together at the same time.

Does multi job jenkins plugin has a hack for it? or any alternatives...

Note: I don't want all the 3 jobs in the same MultiJob Phase

enter image description here


Solution

  • Since you can't run those jobs in one multijob phase, as an alternative You could use Jenkins pipeline job (Pipeline docs). Parallel stages execution can be achieved by using declarative pipeline parallel block. A dummy example of how your MultiJob could be achieved with pipeline:

    pipeline {
        agent any
        stages {
            stage('MultiJob like stage') {
                parallel {
                    stage('Parallel Test') {
                        steps {
                            echo "Here trigger job: allure_behave. Triggered at time:"
                            sh(script: "date -u")
                            // build(job: "allure_behave")
                        }
                    }
                    stage('Parallel Test 2') {
                        steps {
                            echo "Here trigger job: allure_behave_new. Triggered at time:"
                            sh(script: "date -u")
                            // build(job: "allure_behave_new")
                            echo "Here trigger job: allure_behave_old. Triggered at time:"
                            sh(script: """date -u""")
                            // build(job: "allure_behave_old")
                        }
                    }
                }
            }
        }
    }
    

    In this case, You have a Stage called MultiJob like stage which has substages Parallel Test and Parallel Test 2 just like in your MultiJob. The difference is that both of those sub stages are being executed in parallel.

    To trigger other jobs from inside the pipeline job use build step:

    build(job: "job-name")
    

    Or if you need to run it with parameters then just add parameters build() option:

    build(job: "${JOB_NAME}", parameters: [string(name: 'ENVNAME', value: 'EXAMPLE_STR_PARAM')])
    

    Blue Ocean View:

    Pipeline Build Blue Ocean View

    Output:

    Running on Jenkins in /var/jenkins_home/workspace/Dummy_pipeline
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (MultiJob like stage)
    [Pipeline] parallel
    [Pipeline] { (Branch: Parallel Test)
    [Pipeline] { (Branch: Parallel Test 2)
    [Pipeline] stage
    [Pipeline] { (Parallel Test)
    [Pipeline] stage
    [Pipeline] { (Parallel Test 2)
    [Pipeline] echo
    Here trigger job: allure_behave. Triggered at time:
    [Pipeline] sh
    [Pipeline] echo
    Here trigger job: allure_behave_new. Triggered at time:
    [Pipeline] sh
    + date -u
    Thu Nov 22 18:48:56 UTC 2018
    + date -u
    Thu Nov 22 18:48:56 UTC 2018
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] echo
    Here trigger job: allure_behave_old. Triggered at time:
    [Pipeline] sh
    + date -u
    Thu Nov 22 18:48:56 UTC 2018
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // parallel
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: SUCCESS
    

    Is this alternative valid for your use case?

    Regards