简体   繁体   English

当这两个文件在不同的作业中时,如何将参数从 Jenkins 文件传递​​到 Groovy 脚本文件

[英]How to pass parameters from Jenkins file to Groovy script file, when both these file are in different jobs

I have two Jenkins job say Job1 and Job2.我有两个 Jenkins 工作,分别是 Job1 和 Job2。 Job1 uses Jenkinsfile to execute and Job2 uses Groovy script to execute. Job1 使用 Jenkinsfile 执行,Job2 使用 Groovy 脚本执行。

Parameters being used are of type key-value pair, and these parameters will be passed from Jenkinsfile to Groovy Script so as to run Job2 parallel to Job1.使用的参数是键值对类型,这些参数将从 Jenkinsfile 传递到 Groovy Script,以便与 Job1 并行运行 Job2。

Please suggest a way to do the above scenario.请提出一种方法来完成上述场景。

Below is my code:下面是我的代码:

Jenkins(caller)file used in Job1: Job1 中使用的 Jenkins(caller) 文件:

#!groovy    
library <internal library I am using>


pipeline {              
agent any       
stages {
    stage ('callParallelJob') {     
        steps {
               script { 
                       ------- Some code------                          
                       
                       def gitCommitterEmail = sh (
                       script: 'git --no-pager show -s --format=\'%ae\'',
                       returnStdout: true
                       ).trim()
                       echo "git committer: ${gitCommitterEmail}"
                       appConfig.gitCommitterEmail = gitCommitterEmail
                       
                       def gitBranch = "${env.GIT_BRANCH}".trim()
                       appConfig.gitBranch = gitBranch
                     
                       def jenkinsBuildUrl = "${env.BUILD_URL}".trim()
                       appConfig.jenkinsBuildUrl = jenkinsBuildUrl
                       
                      def ciData = [
                                   'git': [
                                   'git_repo': "${git_repo}",
                                   'git_branch': "${env.BRANCH_NAME}",
                                   'git_committer': "${gitCommitterEmail}",
                                   'git_commit': "${GIT_COMMIT}"
                                          ],
                               'jenkins': [
                               'build_url': "${env.BUILD_URL}",
                               'job_name': "${JOB_NAME}",
                               'timestamp': "${BUILD_TIMESTAMP}"
                                          ]
                                      ]
                            writeYaml file: 'ci.yml', data: ciData 
        
                            echo 'calling another job'              
                       build job: '<job2Namehere>, parameters: [
                       string(name: 'Testparam', value: "123"),
                       ], propagate: true, wait: false      
                       echo 'called another job'  

                    }
                }
            }

I want to pass appConfig and ci.yml data to below groovy script我想将 appConfig 和 ci.yml 数据传递给下面的 groovy 脚本

Groovy file used in Job2 : Job2 中使用的 Groovy 文件:

pipeline {
agent any
stages {
    stage('Experiment'){
        steps{
              script{
                  sh 'echo ${Testparam}'               
                    

       -------Some code here-------

}}}}}

您需要将被调用的作业配置为接受参数,然后您的代码应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM