如何使用在构建中注入的密码作为活动选择参数中的环境变量
How can I use passwords injected in the build as environment variables in Active Choices Parameter
我在名为 "Inject passwords to the build as environment variables":
的构建配置部分中定义了密码
我想在我的 Active Choices 中使用 MYVAR(未加密值)。不幸的是,它不起作用。对 MYVAR 的引用失败。
在下面的例子中,为了测试,我只是试图将 MYVAR 的值显示为一个选项。你可以看到它失败了,取而代之的是回退主动选择脚本。
最终,我想使用此变量对服务进行身份验证以构建选择列表,但甚至无法在我的脚本中引用它。我卡住了。
当我尝试 "build with parameters":
提前感谢您的帮助!
我认为这行不通。为什么?那么,EnvInject Plugin 的 Build Environment 选项读取:
Inject environment variables to the build process
和
Inject passwords to the build as environment variables
在 Active Choices Plugin 开始运行时,构建尚未开始,因此尚未进行注入。
受 @Bruno's 评论的启发,我开发了以下内容:
- ☑ 此构建已参数化
- 活动选择参数
- 脚本
- ◉ Groovy 脚本
- 脚本
// From: How can i use passwords injected in the build as environment variables in Active Choices Parameter Groovy Script
//
import static java.lang.System.out
import static java.lang.System.err
import hudson.model.Project
import org.w3c.dom.*;
import javax.xml.parsers.*
import javax.xml.xpath.*
// -----------------------------------------------------------
// Adapt these according to your environment
final String JENKINS_HOME = '< your Jenkins home >'
final String THIS_JOB_NAME = '< your job name >'
// -----------------------------------------------------------
//try (final PrintStream LOG = new PrintStream("${JENKINS_HOME}/jobs/${THIS_JOB_NAME}/activechoices.log")) { // doesn't work
final PrintStream LOG = new PrintStream("${JENKINS_HOME}/jobs/${THIS_JOB_NAME}/activechoices.log")
try {
System.setOut(LOG)
System.setErr(LOG)
out.println("${JENKINS_HOME}/jobs/${THIS_JOB_NAME}/job.log")
// groovy.lang.MissingPropertyException: No such property: Jenkins for class: Script1
//final Project THIS_JOB = Jenkins.instance.getItem(THIS_JOB_NAME)
//final String THIS_JOB_CONFIG = THIS_JOB.getRootDir().getPath() + '/config.xml'
// static path to job config since the above doesn't work
final String THIS_JOB_CONFIG = "${JENKINS_HOME}/jobs/${THIS_JOB_NAME}/config.xml"
out.println(THIS_JOB_CONFIG)
final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(THIS_JOB_CONFIG)
final XPathExpression stringExpr = XPathFactory.newInstance().newXPath()
.compile("//hudson.model.StringParameterDefinition/defaultValue/text()")
final String STRING_PARAMETER = stringExpr.evaluate(doc, XPathConstants.NODE).getNodeValue()
final XPathExpression pwdExpr = XPathFactory.newInstance().newXPath()
.compile("//hudson.model.PasswordParameterDefinition/defaultValue/text()")
final String PASSWORD_PARAMETER = pwdExpr.evaluate(doc, XPathConstants.NODE).getNodeValue()
final List parameters = new ArrayList()
parameters.add('static')
parameters.add(THIS_JOB_NAME)
//parameters.add(THIS_JOB)
parameters.add(STRING_PARAMETER)
parameters.add(PASSWORD_PARAMETER)
return parameters
}
catch (Exception e) {
e.printStackTrace()
}
finally {
LOG.close()
}
问题:
- 为什么
try-with-resources
不起作用?
- 如何获取Jenkins实例?
职位config.xml
<properties>
...
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
...
<hudson.model.PasswordParameterDefinition>
<name>Password Parameter</name>
<description>This is a Password Parameter.</description>
<defaultValue>q2sZWfVMgQNyIi/pjY6yaE7DT9zRvnPv1mBcbydjlMQ=</defaultValue>
</hudson.model.PasswordParameterDefinition>
<hudson.model.StringParameterDefinition>
<name>String Parameter</name>
<description>This is a String Parameter.</description>
<defaultValue>string value</defaultValue>
</hudson.model.StringParameterDefinition>
<hudson.model.StringParameterDefinition>
<name>Another String Parameter</name>
<description>This is another String Parameter.</description>
<defaultValue>another string value</defaultValue>
</hudson.model.StringParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
使用参数构建
我把它作为一个挑战留给 reader 迭代 Nodes when using XPathExpression.evaluate
(...,
XPathConstants.NODESET
)
以防有多个相同类型的参数。
我在名为 "Inject passwords to the build as environment variables":
的构建配置部分中定义了密码我想在我的 Active Choices 中使用 MYVAR(未加密值)。不幸的是,它不起作用。对 MYVAR 的引用失败。
在下面的例子中,为了测试,我只是试图将 MYVAR 的值显示为一个选项。你可以看到它失败了,取而代之的是回退主动选择脚本。
最终,我想使用此变量对服务进行身份验证以构建选择列表,但甚至无法在我的脚本中引用它。我卡住了。
当我尝试 "build with parameters":
提前感谢您的帮助!
我认为这行不通。为什么?那么,EnvInject Plugin 的 Build Environment 选项读取:
Inject environment variables to the build process
和
Inject passwords to the build as environment variables
在 Active Choices Plugin 开始运行时,构建尚未开始,因此尚未进行注入。
受 @Bruno's 评论的启发,我开发了以下内容:
- ☑ 此构建已参数化
- 活动选择参数
- 脚本
- ◉ Groovy 脚本
- 脚本
- ◉ Groovy 脚本
- 脚本
- 活动选择参数
// From: How can i use passwords injected in the build as environment variables in Active Choices Parameter Groovy Script
//
import static java.lang.System.out
import static java.lang.System.err
import hudson.model.Project
import org.w3c.dom.*;
import javax.xml.parsers.*
import javax.xml.xpath.*
// -----------------------------------------------------------
// Adapt these according to your environment
final String JENKINS_HOME = '< your Jenkins home >'
final String THIS_JOB_NAME = '< your job name >'
// -----------------------------------------------------------
//try (final PrintStream LOG = new PrintStream("${JENKINS_HOME}/jobs/${THIS_JOB_NAME}/activechoices.log")) { // doesn't work
final PrintStream LOG = new PrintStream("${JENKINS_HOME}/jobs/${THIS_JOB_NAME}/activechoices.log")
try {
System.setOut(LOG)
System.setErr(LOG)
out.println("${JENKINS_HOME}/jobs/${THIS_JOB_NAME}/job.log")
// groovy.lang.MissingPropertyException: No such property: Jenkins for class: Script1
//final Project THIS_JOB = Jenkins.instance.getItem(THIS_JOB_NAME)
//final String THIS_JOB_CONFIG = THIS_JOB.getRootDir().getPath() + '/config.xml'
// static path to job config since the above doesn't work
final String THIS_JOB_CONFIG = "${JENKINS_HOME}/jobs/${THIS_JOB_NAME}/config.xml"
out.println(THIS_JOB_CONFIG)
final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(THIS_JOB_CONFIG)
final XPathExpression stringExpr = XPathFactory.newInstance().newXPath()
.compile("//hudson.model.StringParameterDefinition/defaultValue/text()")
final String STRING_PARAMETER = stringExpr.evaluate(doc, XPathConstants.NODE).getNodeValue()
final XPathExpression pwdExpr = XPathFactory.newInstance().newXPath()
.compile("//hudson.model.PasswordParameterDefinition/defaultValue/text()")
final String PASSWORD_PARAMETER = pwdExpr.evaluate(doc, XPathConstants.NODE).getNodeValue()
final List parameters = new ArrayList()
parameters.add('static')
parameters.add(THIS_JOB_NAME)
//parameters.add(THIS_JOB)
parameters.add(STRING_PARAMETER)
parameters.add(PASSWORD_PARAMETER)
return parameters
}
catch (Exception e) {
e.printStackTrace()
}
finally {
LOG.close()
}
问题:
- 为什么
try-with-resources
不起作用? - 如何获取Jenkins实例?
职位config.xml
<properties>
...
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
...
<hudson.model.PasswordParameterDefinition>
<name>Password Parameter</name>
<description>This is a Password Parameter.</description>
<defaultValue>q2sZWfVMgQNyIi/pjY6yaE7DT9zRvnPv1mBcbydjlMQ=</defaultValue>
</hudson.model.PasswordParameterDefinition>
<hudson.model.StringParameterDefinition>
<name>String Parameter</name>
<description>This is a String Parameter.</description>
<defaultValue>string value</defaultValue>
</hudson.model.StringParameterDefinition>
<hudson.model.StringParameterDefinition>
<name>Another String Parameter</name>
<description>This is another String Parameter.</description>
<defaultValue>another string value</defaultValue>
</hudson.model.StringParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
使用参数构建
我把它作为一个挑战留给 reader 迭代 Nodes when using XPathExpression.evaluate
(...,
XPathConstants.NODESET
)
以防有多个相同类型的参数。