jenkins pipeline避免使用明文密碼

安裝插件

Credentials Binding

創建憑證

在這裏插入圖片描述在這裏插入圖片描述忽略已經存在的憑證
在這裏插入圖片描述在這裏插入圖片描述這裏以創建一個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"
}

參考博客:https://k4nz.com/Software_Engineering/Continuous_Delivery/Jenkins_Pipeline/Plugins_and_Programming/Credentials.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章