1

I'm using Jenkins, we want to read a json file to provide necessary details within the jenkins scripts such as IP as an example.

Jenkinsfile

def getSecrets(json_file_path, env, var){
  def inputFile = new File(json_file_path)
  def InputJSON = new JsonSlurper().parse(inputFile)
  def secret = InputJSON."${env}"."${var}"
return secret
}
pipeline {
  stages {
    // Below are dev stages
    stage('Dev deployment') {
      environment{
          deployment_env = 'dev'
      }
      def devClusterIP = getSecrets(pwd() + "/values.json", "dev", "IP")
      when {
        expression { GIT_BRANCH_NAME == params.PUBLISH_BRANCH ||  GIT_BRANCH_NAME == params.DEVELOP_BRANCH }
        expression { params.ENV_CHOICE == 'buildOnly' || params.ENV_CHOICE == 'devDeployOnly' }
        beforeInput true
      }

      steps {
       withKubeConfig([credentialsId: 'kubernetes-preproduction-1-cluster', serverUrl: "https://${devClusterIP}"]) {         
         .....
     } 
    }
  }
}

values.json

{
    "dev": {
        "IP": "0.0.0.0"
    }
}

Error facing is

WorkflowScript: 208: Not a valid stage section definition: "def devIP = getSecrets(pwd() + "/values.json", "dev", "IP")". Some extra configuration is required. @ line 208, column 5.

       stage('Dev deployment') {

       ^



1 error



    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)

    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)

    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
user1595858
  • 3,700
  • 15
  • 66
  • 109

1 Answers1

0

Declaring the variable outside the pipeline and initializing it in a script section within the stage where it is needed should work. The pipeline might look something like below:

import groovy.json.JsonSlurper

def getSecrets(jsonFilePath, env, var) {
  return new JsonSlurper().parseText(readFile(jsonFilePath))[env][var]
}

def devClusterIP

pipeline {
  stages {
    // Below are dev stages
    stage('Dev deployment') {
      environment{
        deployment_env = 'dev'
      }
      when {
        expression { GIT_BRANCH_NAME == params.PUBLISH_BRANCH ||  GIT_BRANCH_NAME == params.DEVELOP_BRANCH }
        expression { params.ENV_CHOICE == 'buildOnly' || params.ENV_CHOICE == 'devDeployOnly' }
        beforeInput true
      }

      steps {
        script {
          devClusterIP = getSecrets("values.json", "dev", "IP")
        }
        withKubeConfig([credentialsId: 'kubernetes-preproduction-1-cluster', serverUrl: "https://${devClusterIP}"]) {         
       
        } 
      }
  }
}
devatherock
  • 2,423
  • 1
  • 8
  • 23
  • i got this error when i use your way `Scripts not permitted to use new java.io.File java.lang.String` – user1595858 May 14 '22 at 21:40
  • Hmm, that's interesting. I didn't get that error on Jenkins 2.319.1. I did get a `FileNotFoundException`, which I solved using Jenkins' `readFile` method. Will update answer with the code that I used to read the JSON file – devatherock May 15 '22 at 03:01
  • Let me know if you able to test it? I granted all permission jenkins was prompted but still I can't make it work. – user1595858 May 20 '22 at 22:52
  • I did test it, by printing the value of `devClusterIP` in a next step - the IP address printed correctly. What error are you getting now? And what version of Jenkins do you have? – devatherock May 21 '22 at 00:29
  • Looks like the error message you got is from the script-security-plugin. It is possible in the environment I tested, the plugin was disabled or the method was already allowed. Take a look at these questions - https://stackoverflow.com/questions/61053073, https://stackoverflow.com/questions/49028088. According to them, the job will ask for an approval when it throws the error and once you approve, it should work without approval from the next time onwards – devatherock May 21 '22 at 00:38