安裝插件
創建憑證
忽略已經存在的憑證
這裏以創建一個gitllab的賬號密碼爲例
注意這個ID可以不填保存後可以隨機生成,此ID唯一
添加後會到憑據頁面即可看到
測試
創建一個流水線項目
node ('haimaxy-jnlp'){
stage ("Clone code") {
withCredentials([usernameColonPassword(credentialsId: 'gitlab', variable: 'USERPASS')]){
sh """
git clone http://$USERPASS@"git地址" /ccreate-parent
"""
}
}
stage ("Compile ccreate-parent") {
sh """
ls -l /ccreate-parent
"""
}
}
這裏的用戶名和密碼變成了"*"
pipeline調用不同憑證的方法
使用SSH私鑰文件
withCredentials([file(credentialsId: 'secret', variable: 'FILE')]) {
sh 'use $FILE'
}
使用以冒號分隔的帳號密碼
withCredentials([usernameColonPassword(credentialsId: 'gitlab', variable: 'USERPASS')]) {
sh '''
git clone https://$USERPASS@********
'''
}
使用字符串類型密鑰
withCredentials([string(credentialsId: “CRET-ID”, variable: "varName")]){
sh "echo $varName"
}
獲取用戶名密碼
withCredentials([usernamePassword(credentialsId: “CRET-ID”, usernameVariable: "username", passwordVariable: "password")]){
sh "echo $username $password"
}