在Jenkins声明式管道的参数中定义凭证参数?

aru*_*mar 2 jenkins jenkins-plugins jenkins-pipeline

我目前使用带有参数化构建的Jenkins Delarative管道

pipeline {
    agent any
    parameters {
        booleanParam(name: 'cleanDB',defaultValue: false,description: 'should clean db ?' )
        string(name: 'host',defaultValue: 'xyx',description: 'DB Host')
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn verify'
            }
        }
        stage('Execute') {
            steps {
                withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'CREDENTIALS', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']])
                        {
                            sh "ant " +"-Ddb.clean=${params.cleanDB} -Ddb.host=${params.host} -Ddb.userid=$USERNAME \"-Ddb.password=$PASSWORD\" "
                        }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用参数进行构建时,它仅提示两个参数cleanDB,host params.i还要询问采用哪个凭据参数。仅当通过参数化构建中的UI显式添加时,它才需要。

因此,如何在参数中添加凭据参数,谁能共享一个使用以下语法定义它的示例。

parameters {
        booleanParam(name: 'cleanDB',defaultValue: false,description: 'should clean db ?' )
        string(name: 'host',defaultValue: 'xyx',description: 'DB Host')
credentialParam(name: 'host',description: 'Credentials')
    }
Run Code Online (Sandbox Code Playgroud)

Bar*_*oży 7

截至今天(2017-08-29),jenkins文档仅提及可能的参数的字符串和布尔类型,但有些可以回答这个问题。它说要做:

parameters {
    credentials(name: 'CredsToUse', description: 'A user to build with', defaultValue: '', credentialType: "Username with password", required: true )
} 
Run Code Online (Sandbox Code Playgroud)

我刚试过,它工作正常。第一次执行时,它不会询问任何内容,只会为作业创建参数。之后,它会要求提供凭据。

自然,它适用于声明性管道语法,因此必须用“管道”进行封装。


has*_*chi 1

请尝试以下操作:

withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'CREDENTIALS', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']])
                        {
                            sh 'ant -Ddb.clean=${params.cleanDB} -Ddb.host=${params.host} -Ddb.userid=$USERNAME -Ddb.password=$PASSWORD'
                        }
Run Code Online (Sandbox Code Playgroud)

根据cloudbees上的文档https://support.cloudbees.com/hc/en-us/articles/204897020-Fetch-a-userid-and-password-from-a-Credential-object-in-a-Pipeline-工作-