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

前端 未结 15 1706
情深已故
情深已故 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: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')
    

提交回复
热议问题