Jenkins 敏感信息实战指南

在 Jenkins 中,安全地管理敏感信息对于构建和部署过程至关重要。本实战指南将详细介绍如何添加凭据、使用 HashiCorp Vault 插件,并通过创建 Pipeline 脚本、在 shell 脚本中使用,以及在 Python 脚本中使用来管理敏感信息。

步骤 1: 添加凭据

  1. 登录 Jenkins 控制台。
  2. 在左侧导航栏选择 "Manage Jenkins"。
  3. 选择 "Manage Credentials"。
  4. 在 "Stores scoped to Jenkins" 下选择 "Jenkins"。
  5. 在 "System" 中,点击 "Global credentials (unrestricted)"。
  6. 点击 "Add Credentials"。
  7. 选择凭据类型,例如 "Secret text" 或 "Username with password"。
  8. 输入凭据信息,点击 "OK" 保存凭据。

步骤 2: 使用 HashiCorp Vault 插件

如果使用 HashiCorp Vault 来管理敏感信息,可以添加 HashiCorp Vault 插件:

  1. 在 Jenkins 控制台中选择 "Manage Jenkins"。
  2. 选择 "Manage Plugins"。
  3. 切换到 "Available" 选项卡,搜索并选择 "HashiCorp Vault" 插件进行安装。
  4. 安装完成后,重启 Jenkins。

步骤 3: 配置 HashiCorp Vault 插件

  1. 在 Jenkins 控制台中选择 "Manage Jenkins"。
  2. 选择 "Configure System"。
  3. 找到 "HashiCorp Vault" 部分,配置 Vault 服务器信息。

步骤 4: 创建 Pipeline 脚本

在 Jenkins Pipeline 中使用刚刚创建的凭据或从 HashiCorp Vault 中获取敏感信息。

复制代码
pipeline {
    agent any

    environment {
        MY_SECRET = credentials('my-secret-id')
    }

    stages {
        stage('Example') {
            steps {
                script {
                    withCredentials([usernamePassword(credentialsId: 'my-secret-id', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
                        // 在这里使用凭据
                        echo "Username: $USERNAME"
                        echo "Password: $PASSWORD"
                    }
                }
            }
        }
    }
}

步骤 5: 在 shell 脚本中使用凭据

在 Jenkins Pipeline 中的 shell 脚本中安全地使用凭据。

复制代码
pipeline {
    agent any

    environment {
        MY_SECRET = credentials('my-secret-id')
    }

    stages {
        stage('Example') {
            steps {
                script {
                    withCredentials([usernamePassword(credentialsId: 'my-secret-id', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
                        // 在 shell 脚本中使用凭据
                        sh 'echo $USERNAME'
                        sh 'echo $PASSWORD'
                    }
                }
            }
        }
    }
}

步骤 6: 在 Python 脚本中使用凭据

在 Jenkins Pipeline 中的 Python 脚本中安全地使用凭据。

复制代码
pipeline {
    agent any

    environment {
        MY_SECRET = credentials('my-secret-id')
    }

    stages {
        stage('Example') {
            steps {
                script {
                    withCredentials([usernamePassword(credentialsId: 'my-secret-id', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
                        // 在 Python 脚本中使用凭据
                        sh 'python my_script.py $USERNAME $PASSWORD'
                    }
                }
            }
        }
    }
}

通过这个实战指南,你将学到如何在 Jenkins 中添加凭据、使用 HashiCorp Vault 插件,并通过 Pipeline 脚本、在 shell 脚本中使用,以及在 Python 脚本中使用来管理敏感信息。这些步骤将确保你的 Jenkins 构建过程中的信息安全性。

相关推荐
陈敬雷-充电了么-CEO兼CTO5 分钟前
主流大模型Agent框架 AutoGPT详解
人工智能·python·gpt·ai·chatgpt·nlp·aigc
KENYCHEN奉孝14 分钟前
Flask3.1打造极简CMS系统
后端·python·flask
花海如潮淹29 分钟前
开源项目管理工具:从协作混乱到高效交付的实战指南
python
秋难降34 分钟前
Python 知识点详解(二)
数据库·python·正则表达式
拓端研究室43 分钟前
专题:2025母婴行业洞察报告|附60+份报告PDF汇总下载
开发语言·人工智能·python
Gq.xxu1 小时前
RAG实战之dify源码文件解析-pdf文件解析流程
开发语言·python·pdf
Kyln.Wu1 小时前
【python实用小脚本-131】Python 实现 HTML 到 PDF 转换:解决文档处理痛点的高效工具
python·pdf·html
JavaEdge在掘金1 小时前
告别“作坊式”开发,CodeBuddy能否成为企业级AI编程的“银弹”?
python
sohoAPI1 小时前
Flask快速入门
后端·python·flask
Deng9452013146 小时前
基于Python的职位画像系统设计与实现
开发语言·python·文本分析·自然语言处理nlp·scrapy框架·gensim应用