Reading file from Workspace in Jenkins with Groovy script

前端 未结 7 1551
广开言路
广开言路 2020-11-30 04:12

I want to add a Build step with the Groovy plugin to read a file and trigger a build fail depending on the content of the file.

How can I inject the workspace file p

相关标签:
7条回答
  • 2020-11-30 04:41

    May this help to someone if they have the same requirement.

    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.vert}"),
                            string(name: 'environment', value: "${params.env}"),
                            string(name: 'branch', value: "${params.branch}"),
                            string(name: 'project', value: "${params.project}")
                            ]
                        )
                            }  
                                           }
                        
             }
             }
          }
       }

    0 讨论(0)
提交回复
热议问题