How to set environment variables in Jenkins?

2019-01-01 14:38发布

I would like to be able to do something like:

AOEU=$(echo aoeu)

and have Jenkins set AOEU=aoeu.

The Environment Variables section in Jenkins doesn't do that. Instead, it sets AOEU='$(echo aoeu)'.

How can I get Jenkins to evaluate a shell command and assign the output to an environment variable?

Eventually, I want to be able to assign the executor of a job to an environment variable that can be passed into or used by other scripts.

12条回答
浅入江南
2楼-- · 2019-01-01 15:28

The simplest way

You can use EnvInject plugin to injects environment variables at build startup. For example:

Add key=value (bash OK!) under 'Build Environment'->'Inject environment variables to the build process' -> 'Properties Content'

How you know it's working

EnvInject - Variables injected successfully

查看更多
梦该遗忘
3楼-- · 2019-01-01 15:32

We use groovy job file:

description('')
steps {
    environmentVariables {
        envs(PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true)
    }
}
查看更多
看淡一切
4楼-- · 2019-01-01 15:32

Jenkin is having inbuilt support for this, using which your can access value of local variables as global environment variables. You can achieve this in following 4 simple steps.

Step-1

enter image description here

............................
rm -f <some_name>.properties
touch <Some_name>.properties
............................
#pass the variable name you want to access as an env variable
echo variable_name1=$some_value1 >> <some_name>.properties
echo variable_name2=$some_value2 >> <some_name>.properties
............................
echo variable_name3=$some_value3 >> <some_name>.properties

Step-2

Under "Add Build Step" drop-down, select "Inject environment variable"

enter image description here

enter image description here

Step:3

Enter the fully qualified file name that you created previously(<some_name>.properties) in Properties File Path field.

enter image description here

Step-4

Now it's available as a Jenkins environment variable and you can use as needed in Post-build-Action . $variable_name1 like any other environment variable.

Here is a nice post regarding this

查看更多
千与千寻千般痛.
5楼-- · 2019-01-01 15:34

EnvInject Plugin aka (Environment Injector Plugin) gives you several options to set environment variables from Jenkins configuration.

By selecting Inject environment variables to the build process you will get:

  • Properties File Path
  • Properties Content
  • Script File Path

  • Script Content

  • and finally Evaluated Groovy script.


Evaluated Groovy script gives you possibility to set environment variable based on result of executed command:

  • with execute method:
    return [HOSTNAME_SHELL: 'hostname'.execute().text, 
        DATE_SHELL: 'date'.execute().text,
        ECHO_SHELL: 'echo hello world!'.execute().text
    ]
  • or with explicit Groovy code:
    return [HOSTNAME_GROOVY: java.net.InetAddress.getLocalHost().getHostName(),
        DATE_GROOVY: new Date()
    ] 

(More details about each method could be found in build-in help (?))


Unfortunately you can't do the same from Script Content as it states:

Execute a script file aimed at setting an environment such as creating folders, copying files, and so on. Give the script file content. You can use the above properties variables. However, adding or overriding environment variables in the script doesn't have any impacts in the build job.

查看更多
情到深处是孤独
6楼-- · 2019-01-01 15:34

You can try something like this

stages {
        stage('Build') {
            environment { 
                    AOEU= sh (returnStdout: true, script: 'echo aoeu').trim()
                }
            steps {
                sh 'env'
                sh 'echo $AOEU'
            }
        }
    }
查看更多
高级女魔头
7楼-- · 2019-01-01 15:35

This can be done via EnvInject plugin in the following way:

  1. Create an "Execute shell" build step that runs:

    echo AOEU=$(echo aoeu) > propsfile
    
  2. Create an Inject environment variables build step and set "Properties File Path" to propsfile.

Note: This plugin is (mostly) not compatible with the Pipeline plugin.

查看更多
登录 后发表回答