【问题标题】:Set Jenkins Environment variables via Groovy script通过 Groovy 脚本设置 Jenkins 环境变量
【发布时间】:2017-06-19 14:47:09
【问题描述】:

我想通过在 Ansible 中执行的 Groovy 脚本(YAML 文件)设置 Jenkins 全局属性 - 环境变量。
我试过关注

import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
instance = Jenkins.getInstance()
envVars.put("FOO1", "bar1")
envVars.put("FOO2", "bar2")
instance.save()

为什么这不起作用?

【问题讨论】:

标签: java jenkins groovy ansible


【解决方案1】:

以这种方式完成:

import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
instance = Jenkins.getInstance()
globalNodeProperties = instance.getGlobalNodeProperties()
envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)

envVars = null
if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
  newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
  globalNodeProperties.add(newEnvVarsNodeProperty)
  envVars = newEnvVarsNodeProperty.getEnvVars()
} else {
  envVars = envVarsNodePropertyList.get(0).getEnvVars()  
}
envVars.put("name", "value")
instance.save()

【讨论】:

  • 我不想在全局范围内设置变量,只为那个时候的构建而不是 globalNodeProperties = instance.getGlobalNodeProperties() 可以使用什么以及我需要进行哪些其他更改?
猜你喜欢
  • 2020-05-03
  • 1970-01-01
  • 2019-05-17
  • 2013-02-06
  • 2016-12-23
  • 1970-01-01
  • 2014-01-26
  • 2013-10-12
  • 2019-07-21
相关资源
最近更新 更多