Jenkins 凭证管理 - 看这一篇就够了

Jenkins 凭证管理 - 看这一篇就够了

编码文章call10242025-02-01 3:16:5412A+A-

Jenkins 凭证管理

许多三方网站和应用可以与 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 创建

  • 选择适合的凭证类型
  • 创建 “Username and password” 凭证
  • 创建 “SSH Username with private key” 凭证

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 可以被使用:

  1. 适用于 Jenkins 的任何地方 (即全局 credentials),
  2. 通过特定的 Pipeline 项目/项目 (在 处理 credentials 和 使用 Jenkinsfile 部分了解更多信息),
  3. 由特定的 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/
  • 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

实践:

  • 如下所示,将凭证使用统一的 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"
点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4