本文介绍了如何从工作中设置环境变量并在詹金斯的下一个工作中使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一份工作来设置环境变量并在接下来的所有工作中使用这些环境变量.如何通过 Jenkins 设置环境变量?

I want to have a job to set the environment variable and use these environment variables in all the next jobs. How can I set environment variable through Jenkins ?

推荐答案

从技术上讲,你不能将环境变量从一个工作传递到另一个工作,而且我不知道有一个插件可以开箱即用地做到这一点.

Technically, you can't pass env variables from one job to the next, and I'm not aware of a plugin to do that out of the box.

但是有一种技术.这个想法是在第一个作业中创建一个属性文件(例如exported.properties),将该文件添加到作业工件中,然后通过 EnvInject 插件在第二个工作中.

There is a technique however. The idea is to create a properties file in the first job (e.g. exported.properties), add that file to the job artifacts, and then import this file via the EnvInject plugin in the second job.

这假设您在第一份工作和第二份工作之间有一些联系,这通常通过 Copy Artifact 插件,但许多类似工作流的插件也可以为您提供帮助.

This pre-supposes that you have some link between the first and second job, which is typically achieved with the Copy Artifact plugin, but a number of workflow-like plugins can help you as well.

例如,为了创建属性文件,添加一个步骤Execute shell",例如

For example, for creating the properties file, add a step "Execute shell", with e.g.

echo "# Saving some version properties
BUILD_VERSION=${BuildVersion}
BUILD_NODE_NAME=${NODE_NAME}
SOURCE_JOB=${JOB_NAME}
" > ${WORKSPACE}/BuildVersion.properties

当然,您可以使用其他构建步骤,例如Windows shell、groovy 脚本等...当然每个都有其特定的语法.

Of course, you can use other build steps, e.g. Windows shell, groovy script, etc... each with their specific syntax of course.

这篇关于如何从工作中设置环境变量并在詹金斯的下一个工作中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-20 01:26