簡體   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