简体   繁体   English

从 Jenkins 发送电子邮件通知

[英]Send email notification from Jenkins

I want to send email notification when any job gets done.我想在任何工作完成时发送电子邮件通知。 Please let me know how can we propagate.请让我知道我们如何传播。

You can configure Jenkins to send email when a job finishes either by using a plugin or not.您可以将 Jenkins 配置为在作业完成时发送电子邮件,无论是否使用插件。

Built-in内置

First you have to configure your mail server settings by clicking on Manage Jenkins > Configure System and find the E-mail Notification section near the bottom of the page.首先,您必须通过单击“ Manage Jenkins > Configure System来配置您的邮件服务器设置,然后在页面底部附近找到“ E-mail Notification部分。 Optionally also configure System Admin e-mail address in the Jenkins Location section.也可以选择在Jenkins Location部分配置System Admin e-mail address

Then for each job got to its configuration and Add post-build action , select E-mail Notification and configure as needed.然后对于每个作业进行其配置并Add post-build action ,选择E-mail Notification并根据需要进行配置。

Plugin插入

The issue with default jenkins email mechanism is that it has very limited customization.默认 jenkins 电子邮件机制的问题在于它的自定义非常有限。

The alternate approach is to use the Email-Ext plugin , a powerful email notification mechanism.另一种方法是使用Email-Ext 插件,这是一种强大的电子邮件通知机制。 You can define some global triggers but you can also customize the settings for each job.您可以定义一些全局触发器,但也可以自定义每个作业的设置。 Sending emails for success, failure or any other build status is supported.支持发送成功、失败或任何其他构建状态的电子邮件。

  1. Go to : manage jenkins --> manage plugins --> 'available' tab --> select 'email extension plugin' --> click on button 'install without restart'转到:管理 jenkins --> 管理插件 --> “可用”选项卡 --> 选择“电子邮件扩展插件” --> 单击按钮“无需重启即可安装”

  2. manage jenkins --> configure system --> enter details in 'email notification'管理 jenkins --> 配置系统 --> 在“电子邮件通知”中输入详细信息

Fill up the details as given below and save it:填写下面给出的详细信息并保存:

在此处输入图片说明

  1. In the configuration of project/job , tick on the 'email notification' ---> enter the details ---> save it ---> build the job/project在项目/作业的配置中,勾选“电子邮件通知”---> 输入详细信息 ---> 保存 ---> 构建作业/项目

有一个Jenkins email-ext插件可以添加触发器和收件人。

This answer is for sending mail using python script & outlook through Jenkins.此答案用于通过 Jenkins 使用 python 脚本和 Outlook 发送邮件。

You need to have PsExec.exe for this.为此,您需要拥有 PsExec.exe。 This runs the applications remotely.这将远程运行应用程序。

create a freestyle project & in that run following dos shell command:创建一个自由式项目 & 在该运行中遵循 dos shell 命令:

path\\to\\psexec.exe -u username -p password -i 1 cmd -accepteula /c python path\\to\\SendMail.py path\\to\\psexec.exe -u 用户名 -p 密码 -i 1 cmd -accepteula /c python path\\to\\SendMail.py

Username & password is for the windows user account where outlook runs.用户名和密码用于运行 Outlook 的 Windows 用户帐户。 path\\to\\SendMail.py is the location of the python script. path\\to\\SendMail.py 是 python 脚本的位置。 SendMail.py kinda looks like this: SendMail.py 有点像这样:

import win32com.client as win32
outlook=win32.Dispatch('outlook.application')
mail=outlook.CreateItem(0)
mail.To='abc@xyz.com'
mail.Subject="Test Mail"
mail.HTMLBody="Hiii, This is just a test mail."
mail.Send()

Here I'm using wind32com for executing outlook.这里我使用wind32com 来执行outlook。 The mail will be sent using the default account logged in outlook.邮件将使用登录 Outlook 的默认帐户发送。

You can trigger to build this project everytime after a job completes.每次作业完成后,您都可以触发构建此项目。

Hope this is of any help to anyone :)希望这对任何人都有帮助:)

All the above answers are about plug-in and its usage but not how to use it in pipeline execution.以上所有答案都是关于插件及其用法,而不是如何在管道执行中使用它。 Assuming smtp server is configured in jenkins and https://plugins.jenkins.io/email-ext/ plug-in is installed , we can write or use the plug-in ij the below format.假设在jenkins中配置了smtp服务器并安装了https://plugins.jenkins.io/email-ext/插件,我们可以编写或使用插件ij如下格式。

def csproj_path = 'dotnetcore_sample.csproj'

pipeline {
    agent{
        node {
            label 'dotnet-31' 
        }
    }

    stages {
        stage ('build locally'){
            steps{
                sh "dotnet build ${csproj_path}"
            }
        }
         stage ('Prompt check'){
            steps {
            mail to: 'bruce.wayne@gmail.com',
                 cc : 'clark.kent@gamil.com'
                subject: "INPUT: Build ${env.JOB_NAME}", 
                body: "Awaiting for your input ${env.JOB_NAME} build no: ${env.BUILD_NUMBER} .Click below to promote to production\n${env.JENKINS_URL}job/${env.JOB_NAME}\n\nView the log at:\n ${env.BUILD_URL}\n\nBlue Ocean:\n${env.RUN_DISPLAY_URL}"
                timeout(time: 60, unit: 'MINUTES'){
                    input message: "Promote to Production?", ok: "Promote"
                }
            }
        }
        
    }
    
    post {
        failure {
              mail to: 'mymail@gmail.com',
                 cc : 'ccedpeople@gamil.com'
                subject: "FAILED: Build ${env.JOB_NAME}", 
                body: "Build failed ${env.JOB_NAME} build no: ${env.BUILD_NUMBER}.\n\nView the log at:\n ${env.BUILD_URL}\n\nBlue Ocean:\n${env.RUN_DISPLAY_URL}"
        }
    
    success{
            mail to: 'mymail@gmail.com',
                 cc : 'ccedpeople@gamil.com'
                subject: "SUCCESSFUL: Build ${env.JOB_NAME}", 
                body: "Build Successful ${env.JOB_NAME} build no: ${env.BUILD_NUMBER}\n\nView the log at:\n ${env.BUILD_URL}\n\nBlue Ocean:\n${env.RUN_DISPLAY_URL}"
        }
        
        aborted{
            mail to: 'mymail@gmail.com',
                 cc : 'ccedpeople@gamil.com'
                subject: "ABORTED: Build ${env.JOB_NAME}", 
                body: "Build was aborted ${env.JOB_NAME} build no: ${env.BUILD_NUMBER}\n\nView the log at:\n ${env.BUILD_URL}\n\nBlue Ocean:\n${env.RUN_DISPLAY_URL}"
        }
    }
}

The above declarative pipeline consists of below features上述声明性管道由以下功能组成

  1. Send email for the respective user to promote the build to next level.为相应的用户发送电子邮件以将构建提升到下一个级别。
  2. Email when build is successful.构建成功时的电子邮件。
  3. Email when build is failed and also when it is aborted with link to the logs.构建失败以及中止时的电子邮件,并带有指向日志的链接。
  1. launch Jenkins.启动詹金斯。
  2. access Manage Jenkins link from the main page.从主页访问 Manage Jenkins 链接。

  3. go to the end of the page and fill Email notification details :转到页面末尾并填写电子邮件通知详细信息: 在此处输入图片说明

  4. Click on Test notification to verify it is sending an email.单击测试通知以验证它是否正在发送电子邮件。

  5. Please see the email sent from Jenkins.请参阅 Jenkins 发送的电子邮件。 在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Powershell解析日志文件,并发送电子邮件通知 - parse log file using powershell, and send email notification 是否可以从控制台应用程序发送 Toast 通知? - Is it possible to send Toast notification from console application? 以编程方式从共享邮箱发送 Outlook 电子邮件 - programmatically send outlook email from shared mailbox 从Powershell命令行发送带有凭据的电子邮件 - Send email from powershell command line with credentials 如何将推送通知从Azure发送到Windows Mobile Cordova App - How to send push notification from azure to windows mobile cordova app 如何从云服务器向Windows桌面应用程序发送通知? - How to send notification to windows desktop application from cloud server? 从没有云支持的远程服务器向UWP应用发送通知 - Send notification to UWP app from remote server without cloud support 在 Windows 和 Mac 上从 VBA 宏发送带有工作簿的电子邮件 - Send email with workbook from VBA macro on both Windows and Mac 如何从 Windows 批处理文件发送简单的电子邮件? - How to send a simple email from a Windows batch file? 每当从Windows 7中的共享文件夹中删除文件时发送电子邮件 - Send an email whenever file is deleted from shared folder in windows 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM