許多三方網站和應用可以與 Jenkins 交互,如 Artifact 倉庫,基于云的存儲系統和服務等. 在 Jenkins 中添加/配置 credentials,Pipeline 項目就可以使用 credentials 與三方應用交互
Credential 類型
參考: https://jenkins.io/zh/doc/book/using/using-credentials/
Jenkins 可以存儲以下類型的 credentials:
Secret text - API token 之類的 token (如 GitHub 個人訪問 token)
Username and password - 可以為獨立的字段,也可以為冒號分隔的字符串:username:password(更多信息請參照 處理 credentials)
Secret file - 保存在文件中的加密內容
SSH Username with private key - SSH 公鑰/私鑰對
Certificate - a PKCS#12 證書文件 和可選密碼
Docker Host Certificate Authentication credentials.
Credential 安全
為了最大限度地提高安全性,在 Jenins 中配置的 credentials 以加密形式存儲在 Jenkins 主節點上(用 Jenkins ID 加密),并且 只能通過 credentials ID
在 Pipeline 項目中獲取
這最大限度地減少了向 Jenkins 用戶公開 credentials 真實內容的可能性,并且阻止了將 credentials 復制到另一臺 Jenkins 實例
Credential 創建
-
選擇適合的憑證類型
image -
創建 “Username and password” 憑證
image -
創建 “SSH Username with private key” 憑證
image
Credential ID 定義
在 ID 字段中,必須指定一個有意義的
Credential ID
- 例如 jenkins-user-for-xyz-artifact-repository。注意: 該字段是可選的。 如果您沒有指定值, Jenkins 則 Jenkins 會分配一個全局唯一 ID(GUID)值。請記住: 一旦設置了 credential ID,就不能再進行更改。
Credential 使用
參考: https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#handling-credentials
存儲在 Jenkins 中的 credentials 可以被使用:
適用于 Jenkins 的任何地方 (即全局 credentials),
通過特定的 Pipeline 項目/項目 (在 處理 credentials 和 使用 Jenkinsfile 部分了解更多信息),
-
由特定的 Jenkins 用戶 (如 Pipeline 項目中創建 Blue Ocean的情況).
- Blue Ocean 自動生成一個 SSH 公共/私有密鑰對, 確保 SSH 公共/私有秘鑰對在繼續之前已經被注冊到你的 Git 服務器
實際使用中,下面幾個場景會用到 creential
- gitlab 訪問、API 調用
- jenkins slave 創建
Credential 相關插件
注意: 上述 Credential 類型都依賴于 jenkins 插件,同樣 jenkins pipeline 也需要這些插件的安裝以支持代碼片段
-
Credentials Binding: https://plugins.jenkins.io/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: https://plugins.jenkins.io/plain-credentials/
image SSH Credentials: https://plugins.jenkins.io/ssh-credentials/
最佳實踐
為了便于管理和使用, 強烈建議使用統一的約定來指定 credential ID
-
建議使用類似下面的 format 做為 credential ID, 便于 jenkinsfile 開發時直接使用,同時在”描述“里寫清楚 credential 的作用
gitlab-api-token、gitlab-private-key、gitlab-userpwd-pair、harbor-xxx-xxx
image
實踐:
-
如下所示,將憑證使用統一的 ID 命名之后,便于復用,憑證定義一次,可多次,多個地方統一使用,無論是后期維護,復用都非常方便!
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"