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
                    )
                }
            }
        }
    }
}
相关推荐
志栋智能2 小时前
超自动化安全的报告自动生成与分发
运维·安全·自动化
开开心心就好4 小时前
定时结束进程的小工具,到点还能关机
linux·运维·服务器·智能手机·pdf·电脑·word
风翼靓崽4 小时前
ansible
linux·运维·ansible
zhengfei6116 小时前
AI 渗透项目深度研究:从工具自动化到自主攻击系统,下一代 AI 渗透平台应该如何构建?
运维·人工智能·ai·开源·自动化·ai编程·ai-native
zz0723207 小时前
深入浅出 Nginx:核心原理与企业级应用
运维·nginx
cndes8 小时前
出现 2013 lost connection to MySQL server during query
运维·服务器·数据库
考虑考虑9 小时前
EMQX认证回调
运维·后端·自动化运维
耗同学一米八9 小时前
湖南省网络建设与运维赛项指导培训:备赛要点全解析
运维·网络·php·网络运维·网络建设与运维·楚怡杯·湖南技能竞赛
master33610 小时前
Linux 用户与权限体系:由浅入深完全指南
linux·运维·服务器
Lsetea10 小时前
www 跳转裸域前先查哪张证书?用 CertbotX 验收两个 HTTPS 入口
运维·https·ssl证书·http重定向·域名迁移