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

Credential 類型
參考:
Jenkins可以存儲以下類型的credentials:
- Secret text - API token之類的token (如GitHub個人訪問token)
- Username and password - 可以為獨立的字段,也可以為冒號分隔的字符串:username:password(更多信息請參照 處理 credentials)
- Secret file - 保存在文件中的加密內(nèi)容
- SSH Username with private key - SSH 公鑰/私鑰對
- Certificate - a PKCS#12 證書文件 和可選密碼
- Docker Host Certificate Authentication credentials.
Credential 安全
為了最大限度地提高安全性,在Jenins中配置的 credentials 以加密形式存儲在Jenkins 主節(jié)點上(用Jenkins ID加密),并且 只能通過 credentials ID 在Pipeline項目中獲取
這最大限度地減少了向Jenkins用戶公開credentials真實內(nèi)容的可能性,并且阻止了將credentials復(fù)制到另一臺Jenkins實例
Credential 創(chuàng)建
- 選擇適合的憑證類型
- 創(chuàng)建 “Username and password” 憑證
- 創(chuàng)建 “SSH Username with private key” 憑證
Credential ID 定義
- 在 ID 字段中,必須指定一個有意義的
Credential ID- 例如 jenkins-user-for-xyz-artifact-repository。注意: 該字段是可選的。 如果您沒有指定值, Jenkins 則Jenkins會分配一個全局唯一ID(GUID)值。 - 請記?。?/strong> 一旦設(shè)置了credential ID,就不能再進(jìn)行更改。
Credential 使用
參考:
存儲在Jenkins中的credentials可以被使用:
- 適用于Jenkins的任何地方 (即全局 credentials),
- 通過特定的Pipeline項目/項目 (在 處理 credentials 和 使用Jenkinsfile部分了解更多信息),
- 由特定的Jenkins用戶 (如 Pipeline 項目中創(chuàng)建 Blue Ocean的情況).
- Blue Ocean 自動生成一個 SSH 公共/私有密鑰對, 確保 SSH 公共/私有秘鑰對在繼續(xù)之前已經(jīng)被注冊到你的Git服務(wù)器
實際使用中,下面幾個場景會用到creential
- gitlab 訪問、API調(diào)用
- jenkins slave 創(chuàng)建
Credential 相關(guān)插件
注意: 上述 Credential 類型都依賴于 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:
最佳實踐
- 為了便于管理和使用, 強(qiáng)烈建議使用統(tǒng)一的約定來指定credential ID
- 建議使用類似下面的format做為credential ID, 便于jenkinsfile開發(fā)時直接使用,同時在”描述“里寫清楚credential的作用
gitlab-api-token、gitlab-private-key、gitlab-userpwd-pair、harbor-xxx-xxx
實踐:
- 如下所示,將憑證使用統(tǒng)一的ID命名之后,便于復(fù)用,憑證定義一次,可多次,多個地方統(tǒng)一使用,無論是后期維護(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"
本文僅代表作者觀點,版權(quán)歸原創(chuàng)者所有,如需轉(zhuǎn)載請在文中注明來源及作者名字。
免責(zé)聲明:本文系轉(zhuǎn)載編輯文章,僅作分享之用。如分享內(nèi)容、圖片侵犯到您的版權(quán)或非授權(quán)發(fā)布,請及時與我們聯(lián)系進(jìn)行審核處理或刪除,您可以發(fā)送材料至郵箱:service@tojoy.com






