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

[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"

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理