Jenkins pipeline mail

The entrance to pipeline syntax

Open the pipeline task where you need to set up email sending, and you can see the entrance to "Pipeline Syntax"


Or enter its "Configuration" interface, scroll down to the bottom and you can also see the entrance to "Pipeline Syntax"

Jenkins pipeline mail syntax

Scroll down to find "mail" at the sample step and fill in the instructions according to the prompts to generate the instruction code snippet of the pipeline for sending emails.


A sample code snippet of instructions for a generated email sending pipeline


The instruction code snippet it finally generates looks like this:

mail bcc: '这里写密送', body: '这里写正文', cc: '这里写抄送', from: '', replyTo: '', subject: '这里写主题', to: '这里写收件人收件人'

Of course, it can also set some advanced options, such as media type, sender, character set and other information.

There are two choices for media type.

  • text/html web page
  • text/plain text

Based on the information on the "Pipeline Syntax" page, we can know that the capabilities of the "mail" component fall into the category of "Pipeline: Basic Steps" and do not require additional installation.

Replenish

  • jenkins https://plugins.jenkins.io/workflow-basic-steps/releases

  • We can see all Basic Steps in the gitHub source code of

    github https://github.com/jenkinsci/workflow-basic-steps-pluginworkflow-basic-steps-plugin

Jenkins configures QQ mail

Obtain the third-party account authorization code for QQ mailbox

Set the system administrator email address

Information about setting up email notifications



SMTP server: smtp.qq.com

User default email suffix: @qq.com

Turn on STMP authentication, the username is your personal QQ mailbox, and the password is the third-party account authorization code of the QQ mailbox you obtained earlier.

Use SSL protocol: Enable

Use TLS: Enable

SMTP port: 465 (default port)

Reply-To Address: Reply address

Character set: UTF-8

Send test


Send test successfully

Test in Pipeline

/**
 *  pipeline 0.0.1
 */
pipeline {
    
    environment {
        GIT_CREDENTIALSID = "your-git-credentials-id"
        GIT_URL = "[email protected]:x/y.git"
    }
    agent any

    parameters {
        string(name: 'branch', defaultValue: 'develop', description: 'Which branch?')
        string(name: 'tag', defaultValue: '', description: 'Which tag?')
    }

    tools {
        //工具名称必须在Jenkins全局工具配置中预配置。
        maven 'maven'
    }

    stages {
        stage('源码准备') { // for display purposes
            steps{
                echo "branch: ${params.branch} ,tag: ${params.tag}"
                // Get some code from a GitHub repository
                script {
                    if(params.tag==''){
                        git branch: "${params.branch}", credentialsId: "${GIT_CREDENTIALSID}", url: "${GIT_URL}"
                    }else {
                        checkout(
                                [$class: 'GitSCM', branches: [[name: "refs/tags/${params.tag}"]],
                                 doGenerateSubmoduleConfigurations: false, extensions: [],
                                 submoduleCfg: [],
                                 userRemoteConfigs: [
                                         [credentialsId: "${GIT_CREDENTIALSID}", url: "${GIT_URL}"]
                                 ]
                                ]
                        )
                    }

                }
            }
        }
    }

    post('通知') {
        success {
            mail to:"[email protected]", subject:"SUCCESS: ${currentBuild.fullDisplayName}",body: "Yay, we passed."
        }
        failure {
            mail to:"[email protected]", subject:"FAILURE: ${currentBuild.fullDisplayName}",body: "Boo, we failed."

        }
        unstable {
            mail to:"[email protected]", subject:"UNSTABLE: ${currentBuild.fullDisplayName}",body: "Huh, we're unstable."
        }
        changed {
            mail to:"[email protected]", subject:"CHANGED: ${currentBuild.fullDisplayName}", body: "Wow, our status changed!"
        }
    }
    
}

Executing the above pipeline script in Jenkins will trigger email sending

Please be sure to [email protected]replace it with your personal email when executing. Since the corresponding content

of the application in the script was written by me at will, what I received here was an email that failed to build, which was in line with expectations.GIT_URL

Guess you like

Origin blog.csdn.net/uwoerla/article/details/132420156