How to list all `env` properties within jenkins pipeline job?

前端 未结 15 1762
情深已故
情深已故 2020-11-30 22:20

Given a jenkins 2.1 build pipeline, jenkins injects a env variable into the node{}. For example, BRANCH_NAME can be accessed with

相关标签:
15条回答
  • 2020-11-30 22:55

    I use Blue Ocean plugin and did not like each environment entry getting its own block. I want one block with all the lines.

    Prints poorly:

    sh 'echo `env`'
    

    Prints poorly:

    sh 'env > env.txt'
    for (String i : readFile('env.txt').split("\r?\n")) {
        println i
    }
    

    Prints well:

    sh 'env > env.txt'
    sh 'cat env.txt'
    

    Prints well: (as mentioned by @mjfroehlich)

    echo sh(script: 'env', returnStdout: true)
    
    0 讨论(0)
  • 2020-11-30 22:56

    You can get all variables from your jenkins instance. Just visit:

    • ${jenkins_host}/env-vars.html
    • ${jenkins_host}/pipeline-syntax/globals
    0 讨论(0)
  • 2020-11-30 22:57

    You can accomplish the result using sh/bat step and readFile:

    node {
        sh 'env > env.txt'
        readFile('env.txt').split("\r?\n").each {
            println it
        }
    }
    

    Unfortunately env.getEnvironment() returns very limited map of environment variables.

    0 讨论(0)
提交回复
热议问题