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

前端 未结 15 1691
情深已故
情深已故 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:42

    if you really want to loop over the env list just do:

    def envs = sh(returnStdout: true, script: 'env').split('\n')
    envs.each { name  ->
        println "Name: $name"
    }
    
    0 讨论(0)
  • 2020-11-30 22:44

    I found this is the most easiest way:

    pipeline {
        agent {
            node {
                label 'master'
            }
        }   
        stages {
            stage('hello world') {
                steps {
                    sh 'env'
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 22:48

    Why all this complicatedness?

    sh 'env'
    

    does what you need (under *nix)

    0 讨论(0)
  • 2020-11-30 22:49

    Cross-platform way of listing all environment variables:

    if (isUnix()) {
        sh env
    }
    else {
        bat set
    }
    
    0 讨论(0)
  • 2020-11-30 22:54

    The pure Groovy solutions that read the global env variable don't print all environment variables (e. g. they are missing variables from the environment block, from withEnv context and most of the machine-specific variables from the OS). Using shell steps it is possible to get a more complete set, but that requires a node context, which is not always wanted.

    Here is a solution that uses the getContext step to retrieve and print the complete set of environment variables, including pipeline parameters, for the current context.

    Caveat: Doesn't work in Groovy sandbox. You can use it from a trusted shared library though.

    def envAll = getContext( hudson.EnvVars )
    echo envAll.collect{ k, v -> "$k = $v" }.join('\n')
    
    0 讨论(0)
  • 2020-11-30 22:54

    I suppose that you needed that in form of a script, but if someone else just want to have a look through the Jenkins GUI, that list can be found by selecting the "Environment Variables" section in contextual left menu of every build Select project => Select build => Environment Variables

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