首页 > 解决方案 > 如何在詹金斯的单个管道作业中触发多次运行?

问题描述

我有一个管道作业,它使用下面的管道 groovy 脚本运行,

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

           }
         }

我只是使用不同的唯一 ID 号作为输入参数多次触发此作业。因此,我将在不同阶段为这项工作进行多次运行/构建。

有了这个,我需要在这个管道作业中触发多次运行/构建以提升到下一个阶段(即,从构建到部署或从部署到测试)作为一个单一构建,而不是触发每个单独的运行/构建到下一阶段。有没有可能?

标签: jenkins-pipeline

解决方案


我也试图做同样的事情,但没有找到相关的答案。愿这对某人有所帮助。

这将读取一个包含 Jenkins 作业名称的文件,并从一个作业中迭代地运行它们。

请在您的 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}")
                        ]
                    )
                        }  
                                       }
                    
         }
         }
      }
   }


推荐阅读