Jenkins pipeline 发送邮件及包含附件

Jenkins pipeline 发送邮件及包含附件

设置邮箱开启SMTP服务

此处适用163 邮箱

  1. 开启POP3/SMTP服务
  2. 通过短信获取TOKEN (保存TOKEN, 后面Jenkins会用到)

Jenkins 邮箱设置

安装 Build Timestamp插件

设置全局凭证

Dashboard -> Manage Jenkins -> Credentials -> Stores scoped to Jenkins -> Domains (global)

新增一个ID,配置对应的用户名,密码(163 开启POP3/SMTP 服务提供的 TOKEN)



设置邮箱

  1. Dashboard -> Manage Jenkins -> System -> Jenkins Location ->
    System Admin e-mail address: fernando_xxxx@163.com (开启了POP3/SMTP的邮箱地址)
  2. Dashboard -> Manage Jenkins -> System -> Jenkins Location ->
    Extended E-mail Notification:
    SMTP server: smtp.163.com
    SMTP Port: 25
    Advanced -> Credentials: 163_email_user
    Default user e-mail suffix: 163.com
    Default Content Type: HTML (text/html)

Jenkins pipeline 验证

groovy 复制代码
pipeline {
    agent any
    stages {
       stage('Check and Create Directory') {
            steps {
                script {
                    def dir = "zip_files"
                    sh 'rm -fr ${dir}'
                    if (!fileExists(dir)) {
                        sh "mkdir -p ${dir}"
                    }
                }
            }
        }
        stage('Execute Python Statement') {
            steps {
                sh 'python3 -c "print(\'Hello from Python in Jenkins\')"'
                
                writeFile file: './zip_files/file1.txt', text: 'This is the content of file1'
                writeFile file: './zip_files/file2.txt', text: 'This is the content of file2'
                
                script {
                    env.ZIP_FILE = "test.zip"
                }
                sh "zip -r ${env.ZIP_FILE} ./zip_files/*.txt"
            }
        }
        
         stage('Send Email') {
            steps {
                script {
                    def version = "1.0.${env.BUILD_NUMBER}"
                    emailext (
                        subject: "Build and Package Report for Version ${version}",
                        body: "Please find the attached packaged zip file.",
                        // hide the mail address
                        to: "9949****@qq.com",
                        attachmentsPattern: env.ZIP_FILE
                    )
                }
            }
        }
    }
}
相关推荐
2601_966799041 小时前
酷嗨米J300:一台相机同步推三平台
运维·服务器·负载均衡
AI行业说1 小时前
2026年清洁耗材智能模板机品牌排名:扫地机拖布、美妆粉扑工厂自动化选型指南
运维·自动化
派大鑫wink1 小时前
Dify 工作流搭建操作手册:以信息化运维周报自动生成为例
运维
志栋智能1 小时前
超自动化运维:支持微服务架构的运维模式
运维·架构·自动化
资深电气设计2 小时前
技术分析:空调机房水泵性能优势与系统适配性研究——水力能效、结构集成与智能运维维度
大数据·运维
Brilliantwxx2 小时前
【Linux】 进程(7)深度剖析 进程内核O(1)调度队列 + 饥饿问题
linux·运维·服务器
疯狂打码的少年2 小时前
【数据库技术】关系完整性约束(实体/参照/用户定义)
运维·服务器·数据库·笔记
小谈不敲代码2 小时前
【12-kubenetes的持久化存储】
运维·kubernetes
梦想的旅途23 小时前
企业微信API如何实现自动化办公
运维·自动化·企业微信
刚入门的大一新生3 小时前
Linux-命令行参数和环境变量
linux·运维·服务器