在管道文件中将jenkins变量传递给Job dsl脚本

时间:2020-05-26 13:50:07

标签: jenkins jenkins-pipeline jenkins-job-dsl

问题 如何将变量传递给管道文件中嵌入的作业dsl脚本。我有一个设置一些变量的Jenkins管道,我想在管道的pipelineJob模板中使用这些变量。尝试了不同的组合,但似乎无法正确处理。例如,下面是我的管道,该管道从用户那里获取输入,即git repo url。

    pipeline {
   agent any

           parameters {
            string(name: 'repo_url', defaultValue: '')
        }

   stages {
      stage('Input gathering') {
         steps {
             script {
                            env.repo_url = input message: 'Enter github url', parameters: [string(defaultValue: '', description: '', name: 'repo_url', trim: false)]

             }
             echo "====${env.repo_url}======"
         }
      }
              stage('stage'){
            steps {
                // some other steps
                echo "====${env.repo_url}======"

                jobDsl scriptText: '''pipelineJob(\'new-job\') {


                    triggers {
                        scm(\'H/5 * * * *\')
                    }

                    definition {
                        cpsScm {
                            scm {
                                git {
                                    remote { 
                                        url(repo_url) 
                                        credentials('bitbucket-jenkins-access')
                                    }
                                    branches(\'master\')
                                    scriptPath(\'Jenkinsfile\')
                                    extensions { } 
                                }
                            }
                        }
                    }
                }'''                    
            }           
        }
    }
   }

0 个答案:

没有答案