繁体   English   中英

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

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

我想在工作区中的 Prepare Artifacts 阶段读取 .json 文件。

如何读取暂存 groovy 中的工作区文件路径并运行代码?

我使用的以下代码:

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

当我在上面运行时,我收到如下控制台输出的错误消息

如果要在阶段之间使用变量,则必须将它们定义为全局变量。 因此尝试在管道之外定义releasePackages 下面是一个例子。

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