jenkins allure配置邮件
时间: 2025-06-07 13:25:46 浏览: 12
### 配置 Jenkins 中的 Allure 测试报告并通过邮件发送
在 Jenkins 中配置 Allure 生成的测试报告并通过邮件发送,需要完成以下任务:安装必要的插件、配置 Jenkins 构建任务以生成 Allure 报告,并通过 Email Extension Plugin 发送邮件通知。以下是详细的说明:
#### 1. 安装必要的插件
确保 Jenkins 已安装以下插件:
- **Allure Jenkins Plugin**:用于集成 Allure 测试报告。
- **Email Extension Plugin**:用于发送邮件通知。
可以通过 Jenkins 的插件管理界面(`Manage Jenkins > Manage Plugins`)搜索并安装这些插件[^1]。
#### 2. 配置项目构建任务
在 Jenkins 的 Pipeline 或自由风格项目中,配置以下内容:
##### (1) 配置 Allure 测试报告生成
在构建步骤中,执行测试用例并生成 Allure 报告。例如,使用 Python 和 pytest 执行测试时,可以添加以下命令:
```bash
python3 -m pytest --alluredir=./allure-results
```
这会将测试结果保存到 `allure-results` 目录中[^4]。
##### (2) 在 Post 构建步骤中配置 Allure 报告
在 Pipeline 的 `post` 阶段或自由风格项目的 Post 构建步骤中,添加 Allure 报告的生成命令。例如,在 Pipeline 中可以添加以下代码:
```groovy
post {
always {
script {
echo "Generating Allure Report..."
allure includeProperties: false, jdk: '', results: [[path: 'allure-results']]
}
}
}
```
此代码会从 `allure-results` 目录中读取测试结果并生成 Allure 报告[^4]。
#### 3. 配置邮件通知
在 Jenkins 的 Pipeline 或自由风格项目中,配置邮件通知功能。具体步骤如下:
##### (1) 配置全局工具
进入 `Manage Jenkins > Configure System`,找到 `Extended E-mail Notification` 部分,配置 SMTP 服务器信息。例如:
- SMTP Server: `smtp.example.com`
- Default User E-mail Suffix: `@example.com`
##### (2) 在 Post 构建步骤中添加邮件通知
在 Pipeline 的 `post` 阶段中,添加邮件发送逻辑。例如:
```groovy
post {
always {
script {
echo "Sending email notification..."
emailext(
subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - ${currentBuild.result}",
body: '''
<h3>构建详情</h3>
<p>构建 Url: <a href="${BUILD_URL}">${BUILD_URL}</a></p>
<p>测试报告: <a href="${BUILD_URL}allure/">点击查看测试报告</a></p>
''',
to: '[email protected]'
)
}
}
}
```
此代码会在构建完成后发送一封包含构建 URL 和测试报告链接的邮件给指定的收件人。
#### 4. 示例完整的 Pipeline 脚本
以下是一个完整的 Jenkins Pipeline 脚本示例,展示了如何生成 Allure 报告并通过邮件发送:
```groovy
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git url: 'https://github.com/your-repo.git', branch: 'main'
}
}
stage('Run Tests') {
steps {
sh 'python3 -m pytest --alluredir=./allure-results'
}
}
}
post {
always {
script {
echo "Generating Allure Report..."
allure includeProperties: false, jdk: '', results: [[path: 'allure-results']]
echo "Sending email notification..."
emailext(
subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - ${currentBuild.result}",
body: '''
<h3>构建详情</h3>
<p>构建 Url: <a href="${BUILD_URL}">${BUILD_URL}</a></p>
<p>测试报告: <a href="${BUILD_URL}allure/">点击查看测试报告</a></p>
''',
to: '[email protected]'
)
}
}
}
}
```
#### 注意事项
- 确保 Jenkins 能够访问测试报告目录(如 `allure-results`),并且有权限读取其中的文件。
- 如果使用 Docker 部署 Jenkins,请确保挂载了正确的卷以持久化数据[^3]。
---
###
阅读全文
相关推荐

















