Jenkinsfile JSON对象返回null



我有一个使用repo自动重新设置货叉底座的管道。我做了一个JSON,其中包含repos列表,它就像这个

[
{
"name": "fork-sync-test",
"fork": "git@<host>:<org>//fork-sync-test.git",
"main": "git@<host>:<org>//fork-sync-test.git"
}
]

从Jenkinsfile读取类似

node {
stage ('repo sync') {
def repos = readJSON file: 'repo-list.json'
echo "${repos[0].name}..."
echo "${repos[0].fork}..."
}
}

输出为

fork-sync-test...
null...

如何读取"name"键,而不能读取"fork"键。这没有任何意义。它不应该需要任何像JsonSlurper这样的库。

也尝试了带转义符和不带转义符,但结果是一样的。

最后,我发现了问题。Jenkins无法同步SCM的master,因此无法获取json更改。为了解决这个问题,我将下面的代码添加到我的Jenkinsfile 中

sh 'rm -rf <jenkins repo>'
sh 'git clone git@<host>:<org>/<jenkins repo>.git 

最新更新