如何从Jenkins Build Flow插件导出环境变量?
在所谓的构建流作业中,我尝试了以下两种方法:如何从Jenkins Build Flow插件导出环境变量?,jenkins,Jenkins,在所谓的构建流作业中,我尝试了以下两种方法: build.environment['AOEU'] = 'aoeu' // callee would `println called.environment['AOEU']` 以及: 没有运气。我也为此奋斗了很多,但没有找到干净的方法。我最终以某种丑陋的方式使用EnvInjectPlugin来实现这一点 def buildEnv = build.getEnvVars(); varsToAdd = [:] // add here your c
build.environment['AOEU'] = 'aoeu' // callee would `println called.environment['AOEU']`
以及:
没有运气。我也为此奋斗了很多,但没有找到干净的方法。我最终以某种丑陋的方式使用EnvInjectPlugin来实现这一点
def buildEnv = build.getEnvVars();
varsToAdd = [:]
// add here your custom properties
buildEnv.putAll(varsToAdd)
import org.jenkinsci.plugins.envinject.EnvInjectPluginAction
def envInjectAction = build.getAction(EnvInjectPluginAction.class);
envInjectAction.overrideAll(buildEnv)
。。。EnvInject插件发挥了神奇的作用
我首先尝试实现
并将其添加为build.addAction。。。但不知什么原因没有为我工作
请确保在调用的作业中设置“Flow run需要一个工作区”。我也为此做了很多努力,但没有找到干净的方法。我最终以某种丑陋的方式使用EnvInjectPlugin来实现这一点
def buildEnv = build.getEnvVars();
varsToAdd = [:]
// add here your custom properties
buildEnv.putAll(varsToAdd)
import org.jenkinsci.plugins.envinject.EnvInjectPluginAction
def envInjectAction = build.getAction(EnvInjectPluginAction.class);
envInjectAction.overrideAll(buildEnv)
。。。EnvInject插件发挥了神奇的作用
我首先尝试实现
并将其添加为build.addAction。。。但不知什么原因没有为我工作
确保在被调用的作业中设置“Flow run需要一个工作区”。@NoelYap:我不能对Hristo的答案发表评论,但这是正确的答案。缺少的细节是,仅当您的作业选择了“流程运行需要工作区”选项时,导入EnvInject才有效。@NoelYap:我无法对Hristo的答案发表评论,但这是正确的答案。缺少的细节是,仅当您的作业选择了“流程运行需要一个工作区”选项时,导入EnvInject才有效。我知道这是一个老问题,但我就是这样解决的。最美妙的是,这段代码在Jenkins中运行良好,既可以在系统评估groovy中运行,也可以在构建流作业DSL中运行 从系统Groovy作业/控制台运行import语句时,只需删除它们,因为它们已经可用 作为奖励,该类还帮助获取环境变量,而不管Groovy上下文如何。仍然在构建流程中,您应该更喜欢build.environment.get,因为它更自然 注意:应选中“流程运行需要工作区”
//Import the required bindings for a Build Flow DSL
import hudson.model.AbstractBuild
import hudson.model.EnvironmentContributingAction
import hudson.EnvVars
//Drop the above if running with a System Groovy Console/Job
class Job {
static def getVariable(String key) {
def config = new HashMap()
def thr = Thread.currentThread()
def build = thr?.getCurrentExecutable()
def envVarsMap = build.parent.builds[0].properties.get("envVars")
config.putAll(envVarsMap)
return config.get(key)
}
static def setVariable(String key, String value) {
def build = Thread.currentThread().getCurrentExecutable()
def action = new VariableInjectionAction(key, value)
build.addAction(action)
build.getEnvironment()
}
static def setVariable(String key, Integer value) {
setVariable(key, value.toString())
}
}
class VariableInjectionAction implements EnvironmentContributingAction {
private String key
private String value
public VariableInjectionAction(String key, String value) {
this.key = key
this.value = value
}
public void buildEnvVars(AbstractBuild build, EnvVars envVars) {
if (envVars != null && key != null && value != null) {
envVars.put(key, value);
}
}
public String getDisplayName() { return "VariableInjectionAction"; }
public String getIconFileName() { return null; }
public String getUrlName() { return null; }
}
Job.setVariable("THIS_ENV_VAR", "Hello World")
将环境变量THIS_ENV_VAR设置为Hello World这是一个我知道的老问题,但我就是这样解决的。最美妙的是,这段代码在Jenkins中运行良好,既可以在系统评估groovy中运行,也可以在构建流作业DSL中运行 从系统Groovy作业/控制台运行import语句时,只需删除它们,因为它们已经可用 作为奖励,该类还帮助获取环境变量,而不管Groovy上下文如何。仍然在构建流程中,您应该更喜欢build.environment.get,因为它更自然 注意:应选中“流程运行需要工作区”
//Import the required bindings for a Build Flow DSL
import hudson.model.AbstractBuild
import hudson.model.EnvironmentContributingAction
import hudson.EnvVars
//Drop the above if running with a System Groovy Console/Job
class Job {
static def getVariable(String key) {
def config = new HashMap()
def thr = Thread.currentThread()
def build = thr?.getCurrentExecutable()
def envVarsMap = build.parent.builds[0].properties.get("envVars")
config.putAll(envVarsMap)
return config.get(key)
}
static def setVariable(String key, String value) {
def build = Thread.currentThread().getCurrentExecutable()
def action = new VariableInjectionAction(key, value)
build.addAction(action)
build.getEnvironment()
}
static def setVariable(String key, Integer value) {
setVariable(key, value.toString())
}
}
class VariableInjectionAction implements EnvironmentContributingAction {
private String key
private String value
public VariableInjectionAction(String key, String value) {
this.key = key
this.value = value
}
public void buildEnvVars(AbstractBuild build, EnvVars envVars) {
if (envVars != null && key != null && value != null) {
envVars.put(key, value);
}
}
public String getDisplayName() { return "VariableInjectionAction"; }
public String getIconFileName() { return null; }
public String getUrlName() { return null; }
}
Job.setVariable("THIS_ENV_VAR", "Hello World")
将环境变量THIS_ENV_VAR设置为Hello World一个基于Michael解决方案的更简单版本
import hudson.model.*
def setEnvVariable(final String key, final String value) {
Thread.currentThread().getCurrentExecutable()
.addAction(new ParametersAction(new StringParameterValue(key, value))
);
}
setEnvVariable("THIS_ENV_VAR", "Hello World")
该变量随后可在构建后操作中使用。基于Michael解决方案的更简单版本
import hudson.model.*
def setEnvVariable(final String key, final String value) {
Thread.currentThread().getCurrentExecutable()
.addAction(new ParametersAction(new StringParameterValue(key, value))
);
}
setEnvVariable("THIS_ENV_VAR", "Hello World")
该变量随后可在构建后操作中使用。我认为在构建流脚本中,可以使用build.environment.get'AOEU'。这就是你想要的吗?@DaveBacher,有两个构建流作业。我想将环境变量从一个导出到另一个。我突然想到,由于作业在master上运行,这是不可能的。我将尝试使用文件系统将信息从一个作业传递到另一个作业。我认为在构建流脚本中,可以使用Build.environment.get'AOEU'。这就是你想要的吗?@DaveBacher,有两个构建流作业。我想将环境变量从一个导出到另一个。我突然想到,由于作业在master上运行,这是不可能的。我将尝试使用文件系统将信息从一个作业传递到另一个作业。当我尝试这样做时,它会发出错误:Script1.groovy:1:无法解析类org.jenkinsci.plugins.envinject.EnvInjectPluginActionDo您已经安装了吗?我很确定我们已经安装了。我们确实安装了一个注入环境变量的插件。当我尝试这样做时,它会发出错误:Script1.groovy:1:无法解析类org.jenkinsci.plugins.envinject.EnvInjectPluginActionDo您安装了什么?我很确定我们安装了。我们肯定安装了一个插件来注入环境变量。我不能,我的声誉不够高。我不能,我的声誉不够高。