简体   繁体   English

使用 Groovy 脚本从 Jenkins 中的 Workspace 读取 .json 文件

[英]Reading .json file from Workspace in Jenkins with Groovy script

I want to read a .json file in the stage Prepare Artifacts, which is there in workspace.我想在工作区中的 Prepare Artifacts 阶段读取 .json 文件。

How can I read the workspace file path in the staging groovy and run the code?如何读取暂存 groovy 中的工作区文件路径并运行代码?

The below code I used:我使用的以下代码:

                    checkout scm: [
                        $class: 'GitSCM',
                        branches: [[name: "FETCH_HEAD"]],
                        extensions: [
                            [$class: 'RelativeTargetDirectory',
                                relativeTargetDir: repo2],
                            [$class: 'WipeWorkspace'],
                            [$class: 'CloneOption',
                                depth: 1,
                                noTags: true,
                                reference: '',
                                shallow: true,
                                honorRefspec: true],
                            [$class: 'CheckoutOption',
                                timeout: 30]],
                        gitTool: 'Default',
                        submoduleCfg: [],
                        userRemoteConfigs: [
                            [credentialsId: 'JENKINS_LOGIN',
                           ]
                        ]
                    ]
                    def releasePackages = readJSON file: "./BuildAutomation/Jenkins/Pipeline//files/release_package.json"
                    println releasePackages
                }
            }
        }


        stage('Prepare Variable') {
            steps {
                script{
                    
                        for(file in releasePackages[buildMod]['India']) {
                            bat("xcopy ..\\CALReleaseOutput\\${file} ..\\${IndiaReleaseOutputFolder}\\${file} /I /E /Y")
                        }

                        

for(file in releasePackages[buildMod]['Russia']) {
                                bat("xcopy ..\\CALReleaseOutput\\${file} ..\\${RussiaReleaseOutputFolder}\\${file} /I /E /Y")
                            }
                            
                            zip archive: false, dir: "..\\${b2bReleaseOutputFolder}", glob: '', zipFile: "..\\CALReleaseOutput_${tagFoldername}_B2B.zip"
                        }
                    }
                }
            }
            
        }
    }
}

When I run above one, I got error message as below Console Output当我在上面运行时,我收到如下控制台输出的错误消息

If you are going to use variables between stages you have to define them as Global variables.如果要在阶段之间使用变量,则必须将它们定义为全局变量。 Hence try defining releasePackages outside the pipeline.因此尝试在管道之外定义releasePackages Following is an example.下面是一个例子。

def releasePackages

pipeline {
    agent any

    stages {
        stage('SetVariable') {
            steps {
                script {
                    releasePackages = readJSON file: "./BuildAutomation/Jenkins/Pipeline//files/release_package.json"
                    echo "$releasePackages"
                }
            }
        }
        stage('UseVariable') {
            steps {
                   echo "$releasePackages"           
            }
        }
    }
}

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

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