如何在Jenkins中设置环境变量?

时间:2022-03-15 01:22:26

I would like to be able to do something like:

我希望能做一些类似的事情:

AOEU=$(echo aoeu)

and have Jenkins set AOEU=aoeu.

并让詹金斯设置AOEU= AOEU。

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

Jenkins的环境变量部分没有这样做。相反,它设置了AOEU='$(echo AOEU)'。

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

如何让Jenkins评估shell命令并将输出分配给环境变量?

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.

最后,我希望能够将作业的执行器分配给一个环境变量,该变量可以被其他脚本传入或使用。

10 个解决方案

#1


166  

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

这可以通过EnvInject插件来实现:

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

    创建一个“执行shell”构建步骤:

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

    创建一个注入环境变量构建步骤,并将“属性文件路径”设置为propsfile。

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

注意:这个插件(大部分)与管道插件不兼容。

#2


76  

The simplest way

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

您可以使用EnvInject插件在构建启动时注入环境变量。例如:

如何在Jenkins中设置环境变量?

How you know it's working

如何在Jenkins中设置环境变量?

#3


30  

In my case, I needed to add the JMETER_HOME environment variable to be available via my Ant build scripts across all projects on my Jenkins server (Linux), in a way that would not interfere with my local build environment (Windows and Mac) in the build.xml script. Setting the environment variable via Manage Jenkins - Configure System - Global properties was the easiest and least intrusive way to accomplish this. No plug-ins are necessary.

在我的例子中,我需要将JMETER_HOME环境变量添加到在我的Jenkins服务器(Linux)上的所有项目上的Ant构建脚本中,以一种不会干扰构建中本地构建环境(Windows和Mac)的方式。xml脚本。通过管理Jenkins -配置系统来设置环境变量—全局属性是实现这一目标的最简单、最不受干扰的方式。不需要插件。

如何在Jenkins中设置环境变量?


The environment variable is then available in Ant via:

环境变量在Ant通过:

<property environment="env" />
<property name="jmeter.home" value="${env.JMETER_HOME}" />

This can be verified to works by adding:

这可以通过添加:

<echo message="JMeter Home: ${jmeter.home}"/>

Which produces:

生产:

JMeter Home: ~/.jmeter

JMeter:~ / .jmeter

#4


10  

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

EnvInject Plugin aka(环境注入器插件)为您提供了从Jenkins配置中设置环境变量的几个选项。

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.

    最后对Groovy脚本进行了评估。


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

评估Groovy脚本可以根据执行命令的结果设置环境变量:

  • 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:
  • 或者使用显式的Groovy代码:
    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.

执行一个脚本文件,目标是设置一个环境,例如创建文件夹、复制文件等等。给脚本文件内容。您可以使用上述属性变量。但是,在脚本中添加或重写环境变量对构建作业没有任何影响。

#5


9  

There is Build Env Propagator Plugin which lets you add new build environment variables, e.g.

有构建Env传播器插件,可以添加新的构建环境变量。

如何在Jenkins中设置环境变量?

Any successive Propagate build environment variables step will override previously defined environment variable values.

任何连续的传播构建环境变量步骤将覆盖先前定义的环境变量值。

#6


5  

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


5  

You can use Environment Injector Plugin to set environment variables in Jenkins at job and node levels. Below I will show how to do it at job level.

您可以使用环境注入器插件来设置工作和节点级别的Jenkins中的环境变量。下面我将展示如何在工作级别进行。

  1. From the Jenkins web interface, go to Manage Jenkins > Manage Plugins and install the plugin.
  2. 从Jenkins的web界面,到管理Jenkins >管理插件和安装插件。

如何在Jenkins中设置环境变量?

  1. Go to your job Configure screen
  2. 到您的作业配置屏幕。
  3. Find Add build step in Build section and select Inject environment variables
  4. 在构建部分中找到添加构建步骤,并选择注入环境变量。
  5. Set the desired environment variable as VARIABLE_NAME=VALUE pattern. In my case, I changed value of USERPROFILE variable
  6. 将所需的环境变量设置为VARIABLE_NAME=值模式。在我的例子中,我更改了USERPROFILE变量的值。

如何在Jenkins中设置环境变量?

If you need to define a new environment variable depending on some conditions (e.g. job parameters) you can refer to this answer.

如果您需要根据某些条件(例如作业参数)定义一个新的环境变量,您可以参考这个答案。

#8


4  

Normally you can configure Environment variables in Global properties in Configure System.

通常,您可以在配置系统中的全局属性中配置环境变量。

However for dynamic variables with shell substitution, you may want to create a script file in Jenkins HOME dir and execute it during the build. The SSH access is required. For example.

然而,对于使用shell替换的动态变量,您可能想在Jenkins家中创建一个脚本文件,并在构建期间执行它。需要SSH访问。为例。

  1. Log-in as Jenkins: sudo su - jenkins
  2. 登录为Jenkins: sudo su - Jenkins。
  3. Create a shell script, e.g.:

    创建一个shell脚本,例如:

    echo 'export VM_NAME="$JOB_NAME"' > ~/load_env.sh
    chmod 750 ~/load_env.sh
    
  4. In Jenkins Build (Execute shell), invoke the script and its variables before anything else, e.g.

    在Jenkins构建(执行shell)中,在其他事情之前调用脚本和它的变量。

    source ~/load_env.sh
    

#9


1  

For some reason sudo su - jenkins does not log me to jenkins user, I ended up using different approach.

出于某种原因,sudo su - jenkins没有将我记录到jenkins的用户,我使用了不同的方法。

I was successful setting the global env variables using using jenkins config.xml at /var/lib/jenkins/config.xml (installed in Linux/ RHEL) - without using external plugins.

通过使用jenkins配置,我成功地设置了全局env变量。xml在/var/lib/jenkins/config.xml(安装在Linux/ RHEL中)-不使用外部插件。

I simply had to stop jenkins add then add globalNodeProperties, and then restart.

我只需要停止jenkins的添加,然后添加globalNodeProperties,然后重新启动。

Example, I'm defining variables APPLICATION_ENVIRONMENT and SPRING_PROFILES_ACTIVE to continious_integration below,

例如,我定义了变量APPLICATION_ENVIRONMENT和SPRING_PROFILES_ACTIVE,以在下面继续集成,

<?xml version='1.0' encoding='UTF-8'?>
<hudson>

  <globalNodeProperties>
    <hudson.slaves.EnvironmentVariablesNodeProperty>
      <envVars serialization="custom">
        <unserializable-parents/>
        <tree-map>
          <default>
            <comparator class="hudson.util.CaseInsensitiveComparator"/>
          </default>
          <int>2</int>
          <string>APPLICATION_ENVIRONMENT</string>
          <string>continious_integration</string>
          <string>SPRING_PROFILES_ACTIVE</string>
          <string>continious_integration</string>
        </tree-map>
      </envVars>
    </hudson.slaves.EnvironmentVariablesNodeProperty>
  </globalNodeProperties>
</hudson>

#10


0  

Try Environment Script Plugin (GitHub) which is very similar to EnvInject. It allows you to run a script before the build (after SCM checkout) that generates environment variables for it. E.g.

尝试环境脚本插件(GitHub),它与EnvInject非常相似。它允许您在生成环境变量的构建之前运行一个脚本。如。

如何在Jenkins中设置环境变量?

and in your script, you can print e.g. FOO=bar to the standard output to set that variable.

在您的脚本中,您可以打印例如FOO=bar到标准输出来设置该变量。

Example to append to an existing PATH-style variable:

示例添加到现有的路径样式变量:

echo PATH+unique_identifier=/usr/local/bin

So you're free to do whatever you need in the script - either cat a file, or run a script in some other language from your project's source tree, etc.

因此,您可以*地在脚本中执行任何您需要的操作——无论是cat文件,还是从项目的源代码树中运行其他语言的脚本等等。

#1


166  

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

这可以通过EnvInject插件来实现:

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

    创建一个“执行shell”构建步骤:

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

    创建一个注入环境变量构建步骤,并将“属性文件路径”设置为propsfile。

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

注意:这个插件(大部分)与管道插件不兼容。

#2


76  

The simplest way

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

您可以使用EnvInject插件在构建启动时注入环境变量。例如:

如何在Jenkins中设置环境变量?

How you know it's working

如何在Jenkins中设置环境变量?

#3


30  

In my case, I needed to add the JMETER_HOME environment variable to be available via my Ant build scripts across all projects on my Jenkins server (Linux), in a way that would not interfere with my local build environment (Windows and Mac) in the build.xml script. Setting the environment variable via Manage Jenkins - Configure System - Global properties was the easiest and least intrusive way to accomplish this. No plug-ins are necessary.

在我的例子中,我需要将JMETER_HOME环境变量添加到在我的Jenkins服务器(Linux)上的所有项目上的Ant构建脚本中,以一种不会干扰构建中本地构建环境(Windows和Mac)的方式。xml脚本。通过管理Jenkins -配置系统来设置环境变量—全局属性是实现这一目标的最简单、最不受干扰的方式。不需要插件。

如何在Jenkins中设置环境变量?


The environment variable is then available in Ant via:

环境变量在Ant通过:

<property environment="env" />
<property name="jmeter.home" value="${env.JMETER_HOME}" />

This can be verified to works by adding:

这可以通过添加:

<echo message="JMeter Home: ${jmeter.home}"/>

Which produces:

生产:

JMeter Home: ~/.jmeter

JMeter:~ / .jmeter

#4


10  

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

EnvInject Plugin aka(环境注入器插件)为您提供了从Jenkins配置中设置环境变量的几个选项。

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.

    最后对Groovy脚本进行了评估。


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

评估Groovy脚本可以根据执行命令的结果设置环境变量:

  • 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:
  • 或者使用显式的Groovy代码:
    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.

执行一个脚本文件,目标是设置一个环境,例如创建文件夹、复制文件等等。给脚本文件内容。您可以使用上述属性变量。但是,在脚本中添加或重写环境变量对构建作业没有任何影响。

#5


9  

There is Build Env Propagator Plugin which lets you add new build environment variables, e.g.

有构建Env传播器插件,可以添加新的构建环境变量。

如何在Jenkins中设置环境变量?

Any successive Propagate build environment variables step will override previously defined environment variable values.

任何连续的传播构建环境变量步骤将覆盖先前定义的环境变量值。

#6


5  

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


5  

You can use Environment Injector Plugin to set environment variables in Jenkins at job and node levels. Below I will show how to do it at job level.

您可以使用环境注入器插件来设置工作和节点级别的Jenkins中的环境变量。下面我将展示如何在工作级别进行。

  1. From the Jenkins web interface, go to Manage Jenkins > Manage Plugins and install the plugin.
  2. 从Jenkins的web界面,到管理Jenkins >管理插件和安装插件。

如何在Jenkins中设置环境变量?

  1. Go to your job Configure screen
  2. 到您的作业配置屏幕。
  3. Find Add build step in Build section and select Inject environment variables
  4. 在构建部分中找到添加构建步骤,并选择注入环境变量。
  5. Set the desired environment variable as VARIABLE_NAME=VALUE pattern. In my case, I changed value of USERPROFILE variable
  6. 将所需的环境变量设置为VARIABLE_NAME=值模式。在我的例子中,我更改了USERPROFILE变量的值。

如何在Jenkins中设置环境变量?

If you need to define a new environment variable depending on some conditions (e.g. job parameters) you can refer to this answer.

如果您需要根据某些条件(例如作业参数)定义一个新的环境变量,您可以参考这个答案。

#8


4  

Normally you can configure Environment variables in Global properties in Configure System.

通常,您可以在配置系统中的全局属性中配置环境变量。

However for dynamic variables with shell substitution, you may want to create a script file in Jenkins HOME dir and execute it during the build. The SSH access is required. For example.

然而,对于使用shell替换的动态变量,您可能想在Jenkins家中创建一个脚本文件,并在构建期间执行它。需要SSH访问。为例。

  1. Log-in as Jenkins: sudo su - jenkins
  2. 登录为Jenkins: sudo su - Jenkins。
  3. Create a shell script, e.g.:

    创建一个shell脚本,例如:

    echo 'export VM_NAME="$JOB_NAME"' > ~/load_env.sh
    chmod 750 ~/load_env.sh
    
  4. In Jenkins Build (Execute shell), invoke the script and its variables before anything else, e.g.

    在Jenkins构建(执行shell)中,在其他事情之前调用脚本和它的变量。

    source ~/load_env.sh
    

#9


1  

For some reason sudo su - jenkins does not log me to jenkins user, I ended up using different approach.

出于某种原因,sudo su - jenkins没有将我记录到jenkins的用户,我使用了不同的方法。

I was successful setting the global env variables using using jenkins config.xml at /var/lib/jenkins/config.xml (installed in Linux/ RHEL) - without using external plugins.

通过使用jenkins配置,我成功地设置了全局env变量。xml在/var/lib/jenkins/config.xml(安装在Linux/ RHEL中)-不使用外部插件。

I simply had to stop jenkins add then add globalNodeProperties, and then restart.

我只需要停止jenkins的添加,然后添加globalNodeProperties,然后重新启动。

Example, I'm defining variables APPLICATION_ENVIRONMENT and SPRING_PROFILES_ACTIVE to continious_integration below,

例如,我定义了变量APPLICATION_ENVIRONMENT和SPRING_PROFILES_ACTIVE,以在下面继续集成,

<?xml version='1.0' encoding='UTF-8'?>
<hudson>

  <globalNodeProperties>
    <hudson.slaves.EnvironmentVariablesNodeProperty>
      <envVars serialization="custom">
        <unserializable-parents/>
        <tree-map>
          <default>
            <comparator class="hudson.util.CaseInsensitiveComparator"/>
          </default>
          <int>2</int>
          <string>APPLICATION_ENVIRONMENT</string>
          <string>continious_integration</string>
          <string>SPRING_PROFILES_ACTIVE</string>
          <string>continious_integration</string>
        </tree-map>
      </envVars>
    </hudson.slaves.EnvironmentVariablesNodeProperty>
  </globalNodeProperties>
</hudson>

#10


0  

Try Environment Script Plugin (GitHub) which is very similar to EnvInject. It allows you to run a script before the build (after SCM checkout) that generates environment variables for it. E.g.

尝试环境脚本插件(GitHub),它与EnvInject非常相似。它允许您在生成环境变量的构建之前运行一个脚本。如。

如何在Jenkins中设置环境变量?

and in your script, you can print e.g. FOO=bar to the standard output to set that variable.

在您的脚本中,您可以打印例如FOO=bar到标准输出来设置该变量。

Example to append to an existing PATH-style variable:

示例添加到现有的路径样式变量:

echo PATH+unique_identifier=/usr/local/bin

So you're free to do whatever you need in the script - either cat a file, or run a script in some other language from your project's source tree, etc.

因此,您可以*地在脚本中执行任何您需要的操作——无论是cat文件,还是从项目的源代码树中运行其他语言的脚本等等。