如何使用初始化后脚本添加机密

时间:2018-11-07 15:38:41

标签: jenkins jenkins-pipeline

我需要在詹金斯的post-initialization script中配置一个秘密文本。到目前为止,我只发现了code to configure username/password credentials,但我只是不知道用于秘密文本的正确类。我目前的方法是:

import com.cloudbees.plugins.credentials.impl.*;
import com.cloudbees.plugins.credentials.*;
import com.cloudbees.plugins.credentials.domains.*;

Credentials c = (Credentials) new WhateverClassSecretText(
    CredentialsScope.GLOBAL,
    "my-credential-id",
    "Secret Text for something", 
    "S3cr3t") // TODO find right class here!

SystemCredentialsProvider.getInstance().getStore().addCredentials(Domain.global(), c)

1 个答案:

答案 0 :(得分:0)

我找到了一种方法,但是我仍然好奇是否有人有更好的解决方案。

我正在寻找的类是org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl,它实际上以hudson.util.Secret作为秘密字符串。这是代码(可在Jenkins脚本控制台中测试):

import static com.cloudbees.plugins.credentials.CredentialsScope.GLOBAL

import com.cloudbees.plugins.credentials.domains.Domain
import com.cloudbees.plugins.credentials.SystemCredentialsProvider
import hudson.util.Secret
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl

StringCredentialsImpl credentials = new StringCredentialsImpl(
    GLOBAL,
    "my-credential-id",
    "Secret Text for something",
    Secret.fromString("S3cr3t"))

SystemCredentialsProvider.getInstance().getStore().addCredentials(Domain.global(), credentials)