Reputation: 761
I am defining my environment variables in Jenkins-File. I am using Pipeline Utility Steps Plugin to read a json file in the directory that has configuration. When I echo out the read json file, the output is correct, it reads and prints the json file correctly. When I try to access the value associated with a key in that json object, I am getting error: "No such property: internalCentralConsoleUrl for class: java.lang.String"
The json format config file looks life following:
{
"activeVersion": "19.01.303",
"internalCentralConsoleUrl": "https://11.111.111:8083/api/v1",
"dataType": "APPLICATION_JSON"
}
I am reading that file using readJSON in the pipeline. And in the following lines, trying to access the value inside the json object using the key. Which gives the error I mentioned above.
pipeline {
agent any
environment {
config = readJSON file: 'config.json'
baseUrl = "${config.internalCentralConsoleUrl}"
baseUrl2 = config['internalCentralConsoleUrl']
}
stages {}
}
Both the ways I tried above to read the json value are documented in the jenkins page linked here
I cannot wrap my head around what is causing an issue in this straight forward task.
Edit1: Just corrected a formatting mistake in pipeline.
Upvotes: 3
Views: 7124
Reputation: 761
After RNoB's comment above that it works fine in his Jenkins; I came to the conclusion that it has nothing to do with the pipeline and it might be: a. Jenkins Plugin issue. b. Jenkins itself. c. Host where Jenkins is running.
So, I took the following approach: a. I upgraded the plugins, and reinstalled them. This did not fix the problem. b. I uninstalled Jenkins and removed all Jenkins related files and reinstalled it, and installed all the plugins again. This fixed the problem.
I still don't know what exactly was wrong, it just might be some file that was corrupt. I am not Jenkins expert but this solved the issue for me. Hope this will be helpful for somebody who is having similar issue.
Upvotes: 1
Reputation: 353
I copied your example and added a stage to print the variable:
pipeline {
agent any
environment {
def config = readJSON file: 'config.json'
baseUrl = "${config.internalCentralConsoleUrl}"
}
stages {
stage('Test') {
steps {
echo baseUrl
}
}
}
}
And it prints the variable correctly without any exception:
[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
Upvotes: 5