Home > OS >  Run multiple Jobs in parallel via Jenkins Declarative pipeline syntax
Run multiple Jobs in parallel via Jenkins Declarative pipeline syntax

Time:10-31

I want to execute multiple jobs from a single pipeline using declarative syntax in parallel. Can this be possible!! I know we can make a declarative parallel pipeline using "parallel" parameter.

pipeline {

    agent any
    parallel{
    stages {
        stage('Test1') {
            steps {
                sh 'pip install -r requirements.txt'
            }
        }
        
        stage('Test2') {
            steps {
                echo 'Stage 2'
                sh 'behave -f allure_behave.formatter:AllureFormatter -o allure-results features/scenarios/**/*.feature'
            }
        }
        
        stage('Test3') {
           steps {
                script {
                    allure([
                        includeProperties: false,
                        jdk: '',
                        properties: [],
                        reportBuildPolicy: 'ALWAYS',
                        results: [[path: 'allure-results']]
                    ])
                }
            }
        }
    }
    }
}

Below image will show you the proper flow that I want. Any approach how to do it?

CodePudding user response:

// Pipeline project: SO-69680107-1-parallel-downstream-jobs-matrix
pipeline {
    agent any
    stages {
        stage('Clean Workspace') {
            steps {
                cleanWs()
            }
        }

        stage('Job matrix') {
            matrix {
                axes {
                    axis {
                        name 'job'
                        values 'SO-69680107-2', 'SO-69680107-3', 'SO-69680107-k' // , ...
                    }
                }
                stages {
                    stage('Run job') {
                        steps {
                            build "$job"
                            copyFiles( "$WORKSPACE\\..\\$job", "$WORKSPACE")
                        }
                    } // stage 'Run job'
                }
            } // matrix
        } // stage 'Job matrix'

        stage('List upstream workspace') {
            steps {
                bat "@dir /b \"$WORKSPACE\""
            }
        }
    } // stages
}

def copyFiles( downstreamWorkspace, upstreamWorkspace ) {
    dir("$downstreamWorkspace") {
        bat """
            @set prompt=\$g\$s
            @echo Begin: %time%
            dir /b
            xcopy /f *.* \"$upstreamWorkspace\\\"
            @echo End: %time%
        """
    }
}

Template for downstream projects SO-69680107-2, SO-69680107-3, SO-69680107-k:

// Pipeline project: SO-69680107-X
pipeline {
    agent any
    stages {
        stage('Stage X') {
            steps {
                sh 'set  x; echo "Step X" | tee SO-69680107-X.log; date; sleep 3; date'
            }
        }
    }
}
  • Related