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
                    )
                }
            }
        }
    }
}
相关推荐
忘了ʷºᵇₐ1 小时前
在IDEA 2024.1版本中如何打开Remote Host及连接linux
linux·运维·服务器
upp5 小时前
[最新版本centos 10系统制作与安装]
linux·运维·centos
一战成名9965 小时前
ToDesk全球节点 vs TeamViewer、AnyDesk延迟与稳定性对比
运维·服务器·teamviewer
pupudawang7 小时前
Linux下安装Nginx服务及systemctl方式管理nginx详情
linux·运维·nginx
ipad协议开发7 小时前
视频号直播间全功能自动化场控插件开发指南:基于 API 调用的全链路拆解
运维·微信·自动化·视频·ipad
维度攻城狮7 小时前
Docker优雅地运行OpenClaw
运维·docker·容器·openclaw·openclaw安装
淼淼爱喝水8 小时前
openEuler 下 Ansible 基础命令详解与实操演示2
linux·运维·windows
拾贰_C8 小时前
【Ubuntu | install | 安装软件】 Ubuntu软件安装多种方式以及卸载
linux·运维·ubuntu
梦想的旅途28 小时前
效率革命:实现外部群聊信息的自动化同步方案
运维·自动化
杨云龙UP9 小时前
Linux生产环境下Oracle RMAN 备份、核查、清理与验证常用命令整理_20260330
linux·运维·服务器·数据库·oracle