关于jenkins:Jenkins-凭证管理-看这一篇就够了

6次阅读

共计 2757 个字符,预计需要花费 7 分钟才能阅读完成。

[TOC]

许多三方网站和利用能够与 Jenkins 交互,如 Artifact 仓库,基于云的存储系统和服务等. 在 Jenkins 中增加 / 配置 credentials,Pipeline 我的项目就能够应用 credentials 与三方利用交互

Credential 类型

参考:https://jenkins.io/zh/doc/boo…

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/bo…

存储在 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/cr…

    • 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/pl…

  • SSH Credentials: https://plugins.jenkins.io/ss…

最佳实际

  • 为了便于管理和应用,强烈建议应用对立的约定来指定 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"
正文完
 0