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
                    )
                }
            }
        }
    }
}
相关推荐
草莓熊Lotso29 分钟前
Linux IPC 进阶:System V 消息队列与信号量(含内核管理深度解析)
linux·运维·服务器·数据库·c++·人工智能·mysql
s1kSec2 小时前
天翼云openclaw钉钉配置解决,解决404、401报错
运维·服务器
以太浮标8 小时前
华为eNSP模拟器综合实验之- ACL控制列表核心命令全解析及场景应用
运维·网络·网络协议·华为·信息与通信
巨斧空间掌门8 小时前
JDK17 下载 windows Linux
linux·运维·服务器
小挪号底迪滴8 小时前
Docker容器化实战:从“在我机器上能跑“到环境一致性
运维·docker·容器
江畔何人初8 小时前
kube-apiserver、kube-proxy、Calico 关系
运维·服务器·网络·云原生·kubernetes
_下雨天.9 小时前
Nginx性能调优与深度监控
运维·nginx
皮卡蛋炒饭.9 小时前
进程得控制
linux·运维·服务器
带娃的IT创业者11 小时前
WeClaw 日志分析实战:如何从海量日志中快速定位根因?
运维·python·websocket·jenkins·fastapi·架构设计·实时通信
杨云龙UP12 小时前
Oracle ASM磁盘组空间分配与冗余理解
linux·运维·数据库·sql·oracle