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
                    )
                }
            }
        }
    }
}
相关推荐
啊湘9 分钟前
服务器维护------日志大小控制
运维·服务器·日志大小
qq_366086221 小时前
SQL Server 之 Full-Text Search 全文搜索
运维·服务器·数据库
2401_873587821 小时前
Linux——应用层协议定制
linux·运维·网络协议
大榕树信息科技1 小时前
动环监控如何提升数据中心的运维效率和安全性?
运维·网络·物联网·机房管理系统·动环监控系统
AC赳赳老秦2 小时前
Confluence + DeepSeek:构建自动化、智能化的企业知识库文档生成与维护体系
大数据·运维·人工智能·自动化·jenkins·数据库架构·deepseek
馨谙3 小时前
shell编程三剑客------sed流编辑器基础应用大全以及运行示例
linux·运维·编辑器
驱动探索者3 小时前
Linux list 设计
linux·运维·list
遇见火星3 小时前
在Linux中使用parted对大容量磁盘进行分区详细过程
linux·运维·网络·分区·parted
zhangrelay4 小时前
linux下如何通过与AI对话设置thinkpad电池充电阈值
linux·运维·笔记·学习
hqwest5 小时前
WPF真入门教程36--真硬核【自动化生产管理平台】
运维·自动化·modbus通信·串口设备·自动化生产管理平台·wpf开发