我想在作業區中的 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"
}
}
}
}
}
}
}
當我在上面運行時,我收到如下 控制臺輸出的錯誤訊息
uj5u.com熱心網友回復:
如果要在階段之間使用變數,則必須將它們定義為全域變數。因此嘗試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"
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/497280.html
