You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkinsfile实现每次构建完成触发邮件发送的问题求助

Fix Jenkins Email Notifications to Trigger on Every Build

Hey Aman, let's get your Jenkins build emails working so you get notified every single time a build finishes—regardless of whether the status changed or not. Here's what's going on with your current setup and how to fix it:

Why Your Current Setup Isn't Working

The built-in Mailer step you're using has a default behavior: it only sends success emails when the build status changes from failure to success. The notifyEveryUnstableBuild: true flag only ensures emails for every unstable/failed build, but successful builds still follow the "status change only" rule. That's why you're missing emails for repeated successful builds.

This plugin gives you full control over email triggers and is the most reliable way to get notifications on every build. First, make sure the Extended E-mail Notification plugin is installed in Jenkins. Then update your Jenkinsfile like this:

pipeline {
    agent any
    stages {
        // Add your existing build/test stages here
        stage('Build') {
            steps {
                // Your build commands go here
            }
        }
    }
    post {
        always {
            emailext (
                subject: "Build ${currentBuild.fullDisplayName} - ${currentBuild.result}",
                body: """
                    Build Status: ${currentBuild.result}
                    Build URL: ${env.BUILD_URL}
                    Triggered by: ${currentBuild.startedBy}
                """,
                to: 'abc@gmail.com',
                recipientProviders: [[$class: 'RequesterRecipientProvider']] // Optional: sends to the user who started the build
            )
        }
    }
}

The post { always { ... } } block guarantees this runs no matter if the build succeeds, fails, or is aborted, and the emailext step lets you fully customize the email content while enforcing delivery on every run.

Solution 2: Use Jenkins' Native mail Step (No Extra Plugins)

If you don't want to install another plugin, you can use Jenkins' built-in mail step directly in the post block. This bypasses the Mailer plugin's status-change logic entirely:

pipeline {
    agent any
    stages {
        // Your existing stages here
    }
    post {
        always {
            mail (
                to: 'abc@gmail.com',
                subject: "Build ${currentBuild.fullDisplayName} Completed - ${currentBuild.result}",
                body: "Build URL: ${env.BUILD_URL}\nFinal Status: ${currentBuild.result}"
            )
        }
    }
}

Note: You'll need to configure Jenkins with your SMTP server details (via Manage Jenkins → Configure System → E-mail Notification) for this to work.

Solution 3: Adjust Your Existing Mailer Setup (Less Reliable)

If you want to stick with the original Mailer class, you can force it to run on every build using the post block, but keep in mind it might still skip some successful builds if Jenkins detects no status change. Here's how to tweak it:

pipeline {
    agent any
    stages {
        // Your existing stages here
    }
    post {
        always {
            step([
                $class: 'Mailer',
                notifyEveryUnstableBuild: true,
                recipients: 'abc@gmail.com',
                sendToIndividuals: true
            ])
        }
    }
}

This moves the Mailer step into the always post block, ensuring it runs after every build. However, the core Mailer logic may still suppress emails for unchanged successful statuses, so the first two solutions are better bets.

Key Takeaway

The post { always { ... } } block is your go-to for running logic after every build. Pairing it with either the Extended E-mail plugin or native mail step will guarantee you get an email for every build completion, no exceptions.

内容的提问来源于stack exchange,提问作者Aman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:54:49