Jenkinsfile-JsonSlurper返回字符串而不是映射
因此,我正在尝试一个嵌入config.json文件的CI构建Jenkinsfile-JsonSlurper返回字符串而不是映射,jenkins,groovy,jenkins-pipeline,Jenkins,Groovy,Jenkins Pipeline,因此,我正在尝试一个嵌入config.json文件的CI构建 config.json { "some_collection": [ { "foo": "bar" } ] } 我的文件: import groovy.json.JsonSlurper node { bootstrap() test() } def bootstrap() { stage('bootstrap') { git([url: "git@github.com:my-user/my-je
config.json
{
"some_collection": [
{ "foo": "bar" }
]
}
我的文件:
import groovy.json.JsonSlurper
node {
bootstrap()
test()
}
def bootstrap() {
stage('bootstrap') {
git([url: "git@github.com:my-user/my-jenkinsfile-repo.git"])
}
}
def test() {
def config = getConfig()
echo "${config}"
echo "${config.class}"
}
@NonCPS
def getConfig() {
new JsonSlurper().parseText(readFile("./config.json")))
}
config对象的echo显示文件中的json,config.class表示是一个普通的旧字符串。我希望代码会返回一个映射
我试过JsonSlurper和JsonSluperClassic,我也试过了所有我能想到的方法来重新构造代码,使之更加明确,我已经没有想法了
编辑:我尝试添加一些强类型:
def getConfig() {
JsonSlurper parser = new groovy.json.JsonSlurper()
def json = readFile("./config.json")
Map parsedJson = parser.parseText(json)
return parsedJson
}
这仍然会导致config.class以字符串形式返回y您可以使用
新文件(文件名)
获取config.json
的内容并将其传递给parse()
方法
您只需使用:
def getConfig() {
def pjson = new groovy.json.JsonSlurper().parse(new File('./config.json'))
assert pjson instanceof Map
pjson
}
在Jenkins中使用默认库处理JSON是一件麻烦事。只需使用管道实用程序步骤插件中的
readJSON
步骤 这个方法是什么,它返回什么?能否尝试将其替换为新文件(“./config.json”).getText()
?通过将def
替换为Map
,您可以指定在getConfig()
声明中期望的确切类。新的文件变量引发了一个错误,关于在Jenkins的上下文中如何不允许文件类,我将尝试映射声明。您是否可以尝试使您的config
对象也显式Map
?脚本不允许使用新的java.io.File java.lang.String
不幸的是,您需要提供权限?请看我不太清楚你为什么会投反对票,但我会给你+1,因为我认为这是完全有效和相关的。readJSON
也因为