如何使用 groovy 存储秘密文本或文件

chr*_*ish 2 groovy jenkins

我已经找到了如何使用 Jenkins 的基于 groovy 的 API 来存储用户名/密码或 SSH 用户名/私钥。

https://gist.github.com/iocanel/9de5c976cc0bd5011653

domain = Domain.global()
store = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()

priveteKey = new BasicSSHUserPrivateKey(
CredentialsScope.GLOBAL,
"jenkins-slave-key",
"root",
new BasicSSHUserPrivateKey.UsersPrivateKeySource(),
"",
""
)

usernameAndPassword = new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL,
"jenkins-slave-password", "Jenkis Slave with Password Configuration",
"root",
"jenkins"
)

store.addCredentials(domain, priveteKey)
store.addCredentials(domain, usernameAndPassword)
Run Code Online (Sandbox Code Playgroud)

可以存储更多种类的凭据。我该怎么办:

  • 秘密档案
  • 密文

chr*_*ish 5

经过一番研究,我发现plain-credentials 插件实现了Secret Text 和Secret File 凭据。我分叉了上面的要点并为这两种类型添加了代码(请参阅 reqwuired 导入的要点)。

https://gist.github.com/chrisvire/383a2c7b7cfb3f55df6a

secretText = new StringCredentialsImpl(
CredentialsScope.GLOBAL,
"secret-text",
"Secret Text Description",
Secret.fromString("some secret text goes here"))

file = new File("/path/to/some/file")
noFileItem = [ getName: { return "" } ] as FileItem
FileCredentailsImpl can take a file from a do
secretFile = new FileCredentialsImpl(
CredentialsScope.GLOBAL,
"secret-file",
"Secret File Description"
noFileItem, // Don't use FileItem
file.getName(),
file.text)

store.addCredentials(domain, secretText)
store.addCredentials(domain, secretFile)
Run Code Online (Sandbox Code Playgroud)

  • @Dinesh 导入或完全限定 org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl (2认同)