How to trigger a multiple run in a single pipeline job of jenkins?

回眸只為那壹抹淺笑 提交于 2019-12-13 04:06:19

问题


I have a pipeline job which run with below pipeline groovy script,

pipeline {
     parameters{
    string(name: 'Unique_Number', defaultValue: '', description: 'Enter Unique Number')
        }  
    stages {
            stage('Build') {
            agent { node {  label 'Build'  } }
            steps {
               script {
               sh build.sh
                    }
                }

            stage('Deploy') {
            agent { node {  label 'Deploy'  } }
            steps {
               script {
               sh deploy.sh
                    }
                }

            stage('Test') {
            agent { node {  label 'Test'  } }
            steps {
               script {
               sh test.sh
                    }
                }

           }
         }

I just trigger this job multiple times with different unique ID number as input parameter. So as a result i will have multiple run/build for this job at different stages.

With this, i need to trigger a multiple run/build to be promote to next stage (i.e., from build to deploy or from deploy to test) in this pipeline job as a one single build instead of triggering each and every single run/build to next stage. Is there any possibility?


回答1:


I was also trying to do the same thing and found no relevant answers. May this help to someone.

This will read a file that contains the Jenkins Job name and run them iteratively from one single job.

Please change below code accordingly in your Jenkins.

pipeline {
   agent any

   stages {
      stage('Hello') {
         steps {
             script{
            git branch: 'Your Branch name', credentialsId: 'Your crendiatails', url: ' Your BitBucket Repo URL '

##To read file from workspace which will contain the Jenkins Job Name ###
           
     def filePath = readFile "${WORKSPACE}/ Your File Location"                   

##To read file line by line ###
 
     def lines = filePath.readLines() 
      
##To iterate and run Jenkins Jobs one by one ####

                    for (line in lines) {                                            
                      build(job: "$line/branchName",
                        parameters:
                        [string(name: 'vertical', value: "${params.vertical}"),
                        string(name: 'environment', value: "${params.environment}"),
                        string(name: 'branch', value: "${params.aerdevops_branch}"),
                        string(name: 'project', value: "${params.host_project}")
                        ]
                    )
                        }  
                                       }
                    
         }
         }
      }
   }



回答2:


You can start multiple jobs from one pipeline if you run something as:

build job:"One", wait: false
build job:"Two", wait: false

Your main job starts children pipelines and children pipelines will run in parallel.

You can read PipeLine Build Step documentation for more information.

Also, you can read about the parallel run in declarative pipeline

Here you can find a lot of examples for parallel running



来源:https://stackoverflow.com/questions/54864904/how-to-trigger-a-multiple-run-in-a-single-pipeline-job-of-jenkins

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