Home > Back-end >  Reading .json file from Workspace in Jenkins with Groovy script
Reading .json file from Workspace in Jenkins with Groovy script

Time:06-28

I want to read a .json file in the stage Prepare Artifacts, which is there in workspace.

How can I read the workspace file path in the staging groovy and run the code?

The below code I used:

                    checkout scm: [
                        $class: 'GitSCM',
                        branches: [[name: "FETCH_HEAD"]],
                        extensions: [
                            [$class: 'RelativeTargetDirectory',
                                relativeTargetDir: repo2],
                            [$class: 'WipeWorkspace'],
                            [$class: 'CloneOption',
                                depth: 1,
                                noTags: true,
                                reference: '',
                                shallow: true,
                                honorRefspec: true],
                            [$class: 'CheckoutOption',
                                timeout: 30]],
                        gitTool: 'Default',
                        submoduleCfg: [],
                        userRemoteConfigs: [
                            [credentialsId: 'JENKINS_LOGIN',
                           ]
                        ]
                    ]
                    def releasePackages = readJSON file: "./BuildAutomation/Jenkins/Pipeline//files/release_package.json"
                    println releasePackages
                }
            }
        }


        stage('Prepare Variable') {
            steps {
                script{
                    
                        for(file in releasePackages[buildMod]['India']) {
                            bat("xcopy ..\\CALReleaseOutput\\${file} ..\\${IndiaReleaseOutputFolder}\\${file} /I /E /Y")
                        }

                        

for(file in releasePackages[buildMod]['Russia']) {
                                bat("xcopy ..\\CALReleaseOutput\\${file} ..\\${RussiaReleaseOutputFolder}\\${file} /I /E /Y")
                            }
                            
                            zip archive: false, dir: "..\\${b2bReleaseOutputFolder}", glob: '', zipFile: "..\\CALReleaseOutput_${tagFoldername}_B2B.zip"
                        }
                    }
                }
            }
            
        }
    }
}

When I run above one, I got error message as below Console Output

CodePudding user response:

If you are going to use variables between stages you have to define them as Global variables. Hence try defining releasePackages outside the pipeline. Following is an example.

def releasePackages

pipeline {
    agent any

    stages {
        stage('SetVariable') {
            steps {
                script {
                    releasePackages = readJSON file: "./BuildAutomation/Jenkins/Pipeline//files/release_package.json"
                    echo "$releasePackages"
                }
            }
        }
        stage('UseVariable') {
            steps {
                   echo "$releasePackages"           
            }
        }
    }
}
  • Related