无法通过 JenkinsFile 中的键读取 Json 值

Vir*_*odh 3 json jenkins jenkins-plugins jenkins-pipeline

我正在 Jenkins 文件中定义环境变量。我正在使用 Pipeline Utility Steps Plugin 读取具有配置的目录中的 json 文件。当我回显读取的 json 文件时,输出是正确的,它正确读取并打印 json 文件。当我尝试访问与该 json 对象中的键关联的值时,出现错误:“没有这样的属性:class: java.lang.String 的internalCentralConsoleUrl

json 格式的配置文件看起来如下:

{
    "activeVersion": "19.01.303",
    "internalCentralConsoleUrl": "https://11.111.111:8083/api/v1",
    "dataType": "APPLICATION_JSON"
}
Run Code Online (Sandbox Code Playgroud)

我正在管道中使用 readJSON 读取该文件。在下面的行中,尝试使用键访问 json 对象内的值。这给出了我上面提到的错误。

pipeline {
agent any
environment { 
        config = readJSON file: 'config.json'
        baseUrl = "${config.internalCentralConsoleUrl}"
        baseUrl2 = config['internalCentralConsoleUrl']
        }
stages {}
}
Run Code Online (Sandbox Code Playgroud)

我上面尝试读取 json 值的两种方法都记录在此处链接的詹金斯页面中

我无法理解在这个简单的任务中是什么导致了问题。

Edit1:刚刚纠正了管道中的格式错误。

小智 5

我复制了您的示例并添加了一个阶段来打印变量:

pipeline {
    agent any
    environment {
        def config = readJSON file: 'config.json'
        baseUrl = "${config.internalCentralConsoleUrl}"
    }
    stages {
        stage('Test') {
            steps {
                echo baseUrl
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它正确打印变量,没有任何异常:

[Pipeline] {
[Pipeline] readJSON
[Pipeline] readJSON
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
https://11.111.111:8083/api/v1
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Run Code Online (Sandbox Code Playgroud)