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
                    )
                }
            }
        }
    }
}
相关推荐
时间的拾荒人28 分钟前
Linux 系统命令与网络、磁盘、日志监控(下篇):性能原理与优化
linux·运维·网络
鹤落晴春6 小时前
MySQL日志梳理
运维·数据库·mysql
zly35007 小时前
esxi安装 windows2000 鼠标不动
运维·服务器
AR-26710-10 小时前
docker Day2——镜像 / 容器 / 仓库
运维·docker·容器
分布式存储与RustFS11 小时前
旧 ledger 堆满 Bookie 磁盘:用 Pulsar 分层存储把冷数据卸进 RustFS
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
Huangjin007_11 小时前
【Linux 系统篇(四十一)】线程(二):线程的优缺点、资源共享与上下文切换
linux·运维·服务器
码云社区12 小时前
Spring Boot云快充协议源码:充电桩远程重启、参数配置与升级运维方案
运维·spring boot·后端·充电桩·充电系统·充电软件
csdn_aspnet12 小时前
生产环境中 SQL Server 的 Docker 使用建议
运维·microsoft·docker·容器·sqlserver
csdn_aspnet13 小时前
如何在 Docker 上安装 SQL Server
运维·microsoft·docker·容器·sqlserver
分布式存储与RustFS13 小时前
Dify 容器一重建知识库就空了:把上传文件和密钥一起存进 RustFS
运维·云原生·开源·对象存储·分布式存储·s3·性能基准