Jenkins Pipeline能否使用Promoted Builds并通过邮件审批实现生产构建?
实现Jenkins Pipeline + Promoted Builds的手动审批&生产构建流程
Absolutely! You can totally pull off this workflow by combining Jenkins Pipeline with the Promoted Builds plugin. Let me walk you through exactly how to set this up, step by step.
前提准备
First things first: make sure you have these plugins installed in your Jenkins instance:
- Promoted Builds: Core plugin for managing build promotions and approvals
- Email Extension: For sending customized notification emails with approval links
1. 配置审批(晋升)规则
Head over to your Pipeline project's configuration page and follow these steps:
- 滚动到Promotions区域,点击Add Promotion
- 给你的审批流程起个清晰的名字,比如
Manual Approval for Production Deployment - 在Conditions标签下,选择Manual Approval
- 指定审批人(可以是单个用户、用户组,或者开发团队的邮箱地址)
- 勾选Only allow promotion if the build is successful,确保只有通过自动化测试的构建才能进入审批环节
- 切换到Actions标签(这里配置审批通过后触发生产构建的逻辑)
- 点击Add Action → Trigger Job
- 从下拉菜单中选择你的生产环境流水线/任务
- 可选:添加参数(比如传递当前审批通过的构建编号),把审批环节和生产构建关联起来
2. 编写Pipeline脚本触发审批&发送通知
下面是一个实用的Pipeline脚本示例,我添加了注释来解释每个部分:
pipeline { agent any stages { stage('自动化构建&测试') { steps { // 替换成你实际的构建、测试命令 sh 'mvn clean package -DskipTests=false' junit 'target/surefire-reports/*.xml' } } } post { success { script { // 触发我们之前在项目配置里设置的审批流程 promote( promotionName: 'Manual Approval for Production Deployment', targetBuild: currentBuild.number ) // 给开发团队发送带审批链接的定制化邮件 emailext( subject: "🚀 构建#${currentBuild.number}已完成自动化测试,等待手动验证&审批", to: 'dev-team@yourcompany.com', body: """ 各位好, 构建#${currentBuild.number}已通过所有自动化测试,现在等待大家进行手动验证。 测试完成后,点击下方链接审批该构建,即可触发生产环境部署: ${BUILD_URL}promotion/Manual%20Approval%20for%20Production%20Deployment/ 谢谢, Jenkins CI/CD团队 """ ) } } } }
脚本关键说明:
promote步骤会触发我们在项目配置中设置的审批工作流BUILD_URL是Jenkins内置变量,会自动替换为当前构建的URL。我们拼接上审批流程的路径(注意空格要转义为%20),让开发人员直接跳转到审批页面emailext步骤允许你自定义邮件主题和内容,让通知更清晰、更具操作性
3. 流程运行逻辑
- 当流水线成功完成后,
post-success块会执行:触发审批流程,并给开发团队发送通知邮件 - 开发人员点击邮件中的审批链接,检查构建情况后完成审批
- 审批通过后,我们在Actions中配置的Trigger Job会自动启动生产环境流水线,开始生产构建/部署
进阶技巧
- 如果需要多阶段审批(比如开发审批→QA审批),可以创建多个相互关联的晋升流程
- 使用**Role-Based Access Control (RBAC)**插件限制审批权限,增加安全层级
- 先测试邮件链接是否正确指向审批页面(有时候审批流程名称里的空格需要正确转义)
内容的提问来源于stack exchange,提问作者Alien




