在Jenkins声明式管道的参数中定义凭证参数? [英] Define credential parameter in parameters in Jenkins declarative pipeline?

查看:84
本文介绍了在Jenkins声明式管道的参数中定义凭证参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将Jenkins Delarative管道与参数化的构建一起使用

I Currently using Jenkins Delarative pipeline with a parameterised build

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\" "
                        }
            }
        }
    }
}

当我尝试使用参数进行构建时,它仅提示两个参数cleanDB,即宿主参数.我希望它也询问要采用哪个凭据参数.仅当通过参数化构建中的UI显式添加时才需要它.

when i try to build with parameters it prompts only two param cleanDB,host params.i would like it to also ask which credential parameter to take.it takes only when explicitly added though UI in parameterised build.

所以我该如何在参数中添加凭据参数,任何人都可以共享一个使用以下语法定义它的示例.

so how can i add credential parameter in parameters can any one share an example of defining it in below syntax.

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

推荐答案

截至今天(2017-08-29) jenkins文档仅提及可能的参数的字符串和布尔类型,是否有一些

While as of today (2017-08-29) jenkins docs mention only string and boolean types of possible parameters, there is some ticket that answer this question. It says to do:

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

我刚刚尝试过,效果很好.第一次执行时,它什么都没问,它只是为作业创建参数.之后,它会要求提供凭据.

I just tried it and it works fine. When executed for the first time it doesn't ask anything, it just creates parameter for the job. After then it asks for credentials as it should.

自然,它适用于声明式管道语法,因此必须用管道"进行封装.

Naturally, it works for Declarative Pipeline syntax, so must be enveloped with 'pipeline'.

这篇关于在Jenkins声明式管道的参数中定义凭证参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆