Jenkins 憑證管理 - 看這一篇就夠了~
[TOC]
許多三方網(wǎng)站和應(yīng)用可以與Jenkins交互,如Artifact倉(cāng)庫(kù),基于云的存儲(chǔ)系統(tǒng)和服務(wù)等. 在Jenkins中添加/配置credentials,Pipeline項(xiàng)目就可以使用 credentials 與三方應(yīng)用交互

Credential 類(lèi)型
參考:
Jenkins可以存儲(chǔ)以下類(lèi)型的credentials:
- Secret text - API token之類(lèi)的token (如GitHub個(gè)人訪問(wèn)token)
- Username and password - 可以為獨(dú)立的字段,也可以為冒號(hào)分隔的字符串:username:password(更多信息請(qǐng)參照 處理 credentials)
- Secret file - 保存在文件中的加密內(nèi)容
- SSH Username with private key - SSH 公鑰/私鑰對(duì)
- Certificate - a PKCS#12 證書(shū)文件 和可選密碼
- Docker Host Certificate Authentication credentials.
Credential 安全
為了最大限度地提高安全性,在Jenins中配置的 credentials 以加密形式存儲(chǔ)在Jenkins 主節(jié)點(diǎn)上(用Jenkins ID加密),并且 只能通過(guò) credentials ID 在Pipeline項(xiàng)目中獲取
這最大限度地減少了向Jenkins用戶(hù)公開(kāi)credentials真實(shí)內(nèi)容的可能性,并且阻止了將credentials復(fù)制到另一臺(tái)Jenkins實(shí)例
Credential 創(chuàng)建
- 選擇適合的憑證類(lèi)型
- 創(chuàng)建 “Username and password” 憑證
- 創(chuàng)建 “SSH Username with private key” 憑證
Credential ID 定義
- 在 ID 字段中,必須指定一個(gè)有意義的
Credential ID- 例如 jenkins-user-for-xyz-artifact-repository。注意: 該字段是可選的。 如果您沒(méi)有指定值, Jenkins 則Jenkins會(huì)分配一個(gè)全局唯一ID(GUID)值。 - 請(qǐng)記?。?/strong> 一旦設(shè)置了credential ID,就不能再進(jìn)行更改。
Credential 使用
參考:
存儲(chǔ)在Jenkins中的credentials可以被使用:
- 適用于Jenkins的任何地方 (即全局 credentials),
- 通過(guò)特定的Pipeline項(xiàng)目/項(xiàng)目 (在 處理 credentials 和 使用Jenkinsfile部分了解更多信息),
- 由特定的Jenkins用戶(hù) (如 Pipeline 項(xiàng)目中創(chuàng)建 Blue Ocean的情況).
- Blue Ocean 自動(dòng)生成一個(gè) SSH 公共/私有密鑰對(duì), 確保 SSH 公共/私有秘鑰對(duì)在繼續(xù)之前已經(jīng)被注冊(cè)到你的Git服務(wù)器
實(shí)際使用中,下面幾個(gè)場(chǎng)景會(huì)用到creential
- gitlab 訪問(wèn)、API調(diào)用
- jenkins slave 創(chuàng)建
Credential 相關(guān)插件
注意: 上述 Credential 類(lèi)型都依賴(lài)于 jenkins插件,同樣jenkins pipeline 也需要這些插件的安裝以支持代碼片段
- Credentials Binding:
- For secret text, usernames and passwords, and secret files
environment {
MAGE_REPO_CREDENTIALS = credentials('COMPOSER_REPO_MAGENTO')
COMPOSER_AUTH = """{
"http-basic": {
"repo.magento.com": {
"username": "${env.MAGE_REPO_CREDENTIALS_USR}",
"password": "${env.MAGE_REPO_CREDENTIALS_PSW}"
}
} }"""
}
- For other credential types
withCredentials([usernamePassword(credentialsId: 'amazon', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
// available as an env variable, but will be masked if you try to print it out any which way
// note: single quotes prevent Groovy interpolation; expansion is by Bourne Shell, which is what you want
sh 'echo $PASSWORD'
// also available as a Groovy variable
echo USERNAME
// or inside double quotes for string interpolation
echo "username is $USERNAME"
}
- Jenkins Plain Credentials Plugin:
- SSH Credentials:
最佳實(shí)踐
- 為了便于管理和使用, 強(qiáng)烈建議使用統(tǒng)一的約定來(lái)指定credential ID
- 建議使用類(lèi)似下面的format做為credential ID, 便于jenkinsfile開(kāi)發(fā)時(shí)直接使用,同時(shí)在”描述“里寫(xiě)清楚credential的作用
gitlab-api-token、gitlab-private-key、gitlab-userpwd-pair、harbor-xxx-xxx
實(shí)踐:
- 如下所示,將憑證使用統(tǒng)一的ID命名之后,便于復(fù)用,憑證定義一次,可多次,多個(gè)地方統(tǒng)一使用,無(wú)論是后期維護(hù),復(fù)用都非常方便!
environment {
// HARBOR="harbor.devopsing.site"
HARBOR_ACCESS_KEY = credentials('harbor-userpwd-pair')
SERVER_ACCESS_KEY = credentials('deploy-userpwd-pair')
}
.....
docker login --username=${HARBOR_ACCESS_KEY_USR} --password=${HARBOR_ACCESS_KEY_PSW} ${HARBOR}
sshpass -p "${SERVER_ACCESS_KEY_PSW}" ssh -o StrictHostKeyChecking=no ${SERVER_ACCESS_KEY_USR}@${DEPLOY_SERVER} "$runCmd"
本文僅代表作者觀點(diǎn),版權(quán)歸原創(chuàng)者所有,如需轉(zhuǎn)載請(qǐng)?jiān)谖闹凶⒚鱽?lái)源及作者名字。
免責(zé)聲明:本文系轉(zhuǎn)載編輯文章,僅作分享之用。如分享內(nèi)容、圖片侵犯到您的版權(quán)或非授權(quán)發(fā)布,請(qǐng)及時(shí)與我們聯(lián)系進(jìn)行審核處理或刪除,您可以發(fā)送材料至郵箱:service@tojoy.com





