使用init.groovy将全局密码添加到Jenkins [英] Adding Global Password to Jenkins with init.groovy
问题描述
如何通过启动时运行的init.groovy向Jenkins添加全局密码?
How can I add a global passwords to Jenkins through the init.groovy that runs at startup?
为清楚起见,在管理Jenkins->配置Jenkins"页面中,有一个标题为全局密码"的部分.我想在Jenkins启动期间通过Groovy代码在该部分中添加条目.
To be clear, in the Manage Jenkins -> Configure Jenkins page, there is a section titled "Global Passwords". I would like to add entries in that section via Groovy code during the startup of Jenkins.
我正在尝试使用 init.groovy .我需要通过 EnvInject插件添加全局密码.我可以使用以下代码为同一插件成功添加文件路径:
I am trying to provision my jenkins environment through groovy code by using the init.groovy. I need to add global passwords through the EnvInject plugin. I can successfully add path to a file for the same plugin using this code:
def instance = Jenkins.getInstance()
DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties =
instance.getGlobalNodeProperties();
globalNodeProperties.add(
new EnvInjectNodeProperty(false, "/var/lib/jenkins/secret.properties")
);
但是,我无法理解以编程方式添加全局密码所需的机制.
However, I am failing to understand the mechanics needed to programmatically add global passwords.
推荐答案
这是应该起作用的代码示例.似乎save()方法也将其添加到GlobalNodeProperties中,因此您不必手动将其添加到该集合中.
Here is the code example that should work. It seems that save() method also adds it to GlobalNodeProperties, so you don't have to add to that collection manually.
import jenkins.model.*
import hudson.util.*
import hudson.slaves.NodeProperty
import hudson.slaves.NodePropertyDescriptor
import org.jenkinsci.plugins.envinject.*
def instance = Jenkins.getInstance()
DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties
= instance.getGlobalNodeProperties();
envInjectNodeProperty= new EnvInjectNodeProperty(false, "/var/lib/jenkins/secret.properties"
propDescriptor = envInjectNodeProperty.getDescriptor()
//password entry
def passEntry = new EnvInjectGlobalPasswordEntry("some_username", "password")
//password entries list, add you global password here
List<EnvInjectGlobalPasswordEntry> envInjectGlobalPasswordEntriesList= [passEntry];
propDescriptor.envInjectGlobalPasswordEntries =
envInjectGlobalPasswordEntriesList.toArray(
new EnvInjectGlobalPasswordEntry[envInjectGlobalPasswordEntriesList.size()]
);
propDescriptor.save();
这篇关于使用init.groovy将全局密码添加到Jenkins的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!