简体   繁体   中英

Create JSON listing all Jenkins specific environment variables within Groovy Script

I'm trying (in a Groovy script executed via pipeline) to get all Jenkins environment variable values output in a json format.

So, for example:

{
   "branchName":"test/branchA",
   "changeID":null
} 

Ultimately I would like to have this json written to a file in the workspace of the running job.

Any suggestions?

Thanks

A simple stage using declarative pipeline can achieve this easily.

pipeline {
   agent any
   stages {
        stage('Write-Jenkins-Env-To-File') {
            steps {
              sh """
                jq -n env >> jenkinsEnvironmentFile
                cat jenkinsEnvironmentFile
              """
            }
        } 
    }     
}

Just ensure, that you've the jq linux utility installed in your jenkins agent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM