詹金斯证书与种子工作

时间:2020-03-30 18:14:02

标签: jenkins groovy jenkins-pipeline jenkins-job-dsl

我有一个来自私有云团队的小型Kubernetes集群,但他们希望我们使一切自动化。有人告诉我使用Jenkins种子作业来创建所需的所有管道。 我想出了如何做,但是有一个小问题。 我需要在jenkins中手动存储docker注册表的凭据。有什么方法可以使凭证创建自动化?还是除了使用

以外,还有其他方法可以针对私有注册表进行身份验证
container('docker') {
                    withDockerRegistry([credentialsId: 'dockerRegistry', url: "https://registrurl/"]) {

我发现了使用诸如此类的普通代码创建詹金斯凭证的示例:

import jenkins.model.*
import hudson.util.*
import org.jenkinsci.plugins.plaincredentials.impl.*
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.domains.*
import com.cloudbees.plugins.credentials.impl.*

// parameters
def jenkinsKeyUsernameWithPasswordParameters = [
        description:  'Description here',
        id:           'key-id-here',
        secret:       '12345678901234567890',
        userName:     'your-username-here'
]

// get Jenkins instance
Jenkins jenkins = Jenkins.getInstance()

// get credentials domain
def domain = Domain.global()

// get credentials store
def store = jenkins.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()

// define secret
def jenkinsKeyUsernameWithPassword = new UsernamePasswordCredentialsImpl(
        CredentialsScope.GLOBAL,
        jenkinsKeyUsernameWithPasswordParameters.id,
        jenkinsKeyUsernameWithPasswordParameters.description,
        jenkinsKeyUsernameWithPasswordParameters.userName,
        jenkinsKeyUsernameWithPasswordParameters.secret
)

// add credential to store
store.addCredentials(domain, jenkinsKeyUsernameWithPassword)

// save to disk
jenkins.save()

它在Jenkins脚本控制台中运行良好,但在我创建的任何作业中均无法运行。

我总是会收到这样的错误:

jenkins_credentials.groovy: 28: unable to resolve class UsernamePasswordCredentialsImpl 
 @ line 28, column 38.
   def jenkinsKeyUsernameWithPassword = new UsernamePasswordCredentialsImpl(

1 个答案:

答案 0 :(得分:0)

您可以使用jenkins配置作为代码插件https://github.com/jenkinsci/configuration-as-code-plugin/

自动创建凭据