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
                    )
                }
            }
        }
    }
}
相关推荐
The star"'13 小时前
mysql(1-3)
运维·mysql·云计算
model200514 小时前
Alibaba linux 3安装LAMP(5)
linux·运维·服务器
weixin_3077791315 小时前
Jenkins中的Jakarta Activation API插件:功能、使用与最佳实践
运维·开发语言·ci/cd·自动化·jenkins
王 富贵17 小时前
【Linux】防火墙常用命令(iptables/firewalld/ufw)
linux·运维·服务器
一条咸鱼¥¥¥17 小时前
【运维经济】思科交换机和路由器的密码重置方法
运维·网络·智能路由器
写代码的【黑咖啡】17 小时前
Linux系统简介及常用命令分类详解
linux·运维·服务器
❀͜͡傀儡师17 小时前
docker一键部署夜莺监控
运维·docker·容器
0思必得017 小时前
[Web自动化] HTML列表标签
运维·python·自动化·html·web自动化
松涛和鸣18 小时前
DAY27 Linux File IO and Standard IO Explained: From Concepts to Practice
linux·运维·服务器·c语言·嵌入式硬件·ubuntu
GeniuswongAir18 小时前
飞牛NAS死机排查
linux·运维·服务器