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 小时前
解决 Linux 中 version `CXXABI_1.3.8‘ not found 报错
linux·运维·服务器
0+1119 小时前
Linux --线程概念与控制
linux·运维·服务器·jvm
宽海智能仓储物流10 小时前
航发自动化立体库异构系统改造:宽海智能30天联调技术全解析
运维·数据仓库·自动化
鹤落晴春10 小时前
【K8s】从 ConfigMap 到 Argo CD:Kubernetes 配置管理链路
运维·云原生·容器·kubernetes
2601_9494999410 小时前
DT‑1414PTZ如何百分百兼容(安华高)HFBR‑1414PTZ
运维·网络·人工智能·科技·光模块
fengyehongWorld10 小时前
Linux 安装frp实现内网穿透
linux·运维·服务器
一技安身11 小时前
【信创】银河麒麟V10服务器使用安装光盘Packages自建本地yum源
linux·运维·服务器
码农客栈12 小时前
Linux I2C驱动实验
linux·运维·驱动开发
爱喝水的鱼丶13 小时前
SAP-ABAP:隐式/显式增强开发规范与避坑指南:10 类典型错误与运维方案
运维·性能优化·sap·abap·增强·经验交流
企业解惑小助手14 小时前
如何禁止文字复制?企业文档防复制项目需求拆解方案
运维·数据库·安全