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

如何为所有新建项目添加默认的构建失败邮件通知构建后操作?

Awesome question—having to manually set up email notifications for every new Jenkins project is such a hassle, so I totally get why you want to make this a default. Here are three reliable solutions to get this done:

Solution 1: Use a Template Project (Quick & Simple)

This is the most straightforward approach for most teams:

  • Create a new freestyle project (name it something like Default-Email-Template)
  • Edit its configuration, add the Editable Email Notification post-build action from the email-ext plugin
  • Set up your preferred default settings: trigger on build failure, default recipients, email subject/body, etc.
  • From now on, whenever you create a new project, select the "Copy from existing project" option and pick this template. All the email settings will be pre-configured automatically.
Solution 2: Set Global Defaults via Groovy Script (Set It & Forget It)

If you want every new project to inherit the email-ext settings without having to select a template, use a Groovy init script:

  1. Locate your Jenkins home directory (usually ~/.jenkins on Linux/macOS, or C:\Program Files\Jenkins on Windows)
  2. Create a folder named init.groovy.d if it doesn’t already exist
  3. Inside that folder, create a new file called default-email-ext.groovy with this code:
import jenkins.model.Jenkins
import hudson.plugins.emailext.ExtendedEmailPublisher
import hudson.plugins.emailext.plugins.trigger.FailureTrigger

def jenkins = Jenkins.instance
def jobDescriptor = jenkins.getDescriptorByType(hudson.model.Job.DescriptorImpl.class)

def defaultPublishers = jobDescriptor.getDefaultPublishers()
def hasEmailExt = defaultPublishers.any { it instanceof ExtendedEmailPublisher }

if (!hasEmailExt) {
    def emailPublisher = new ExtendedEmailPublisher()
    
    // Add failure trigger (customize this as needed)
    def failureTrigger = new FailureTrigger()
    failureTrigger.setSendToDevelopers(true)
    failureTrigger.setSendToRequester(true)
    emailPublisher.setTriggers([failureTrigger])
    
    // Optional: Set default recipients, subject, or body
    // emailPublisher.setDefaultRecipients("your-team@example.com")
    // emailPublisher.setDefaultSubject("Build Failed: \${PROJECT_NAME} #\${BUILD_NUMBER}")
    
    defaultPublishers.add(emailPublisher)
    jobDescriptor.setDefaultPublishers(defaultPublishers)
    jobDescriptor.save()
}
  1. Restart Jenkins, and every new project you create will automatically have the email-ext failure notification set up.
Solution 3: Use Job DSL for Scalable Templates

For teams that create projects in bulk or need complex default configurations, the Job DSL plugin is a great fit:

  • Install the Job DSL Plugin from Jenkins’ plugin manager
  • Create a "seed project" (freestyle job)
  • Add a "Process Job DSLs" build step, then paste a script like this:
job {
    name('Base-Project-Template')
    publishers {
        extendedEmail {
            triggers {
                failure {
                    sendTo('developers', 'requester')
                    subject('Build Failed: ${PROJECT_NAME} #${BUILD_NUMBER}')
                    body('''Build failed for project ${PROJECT_NAME}
Build URL: ${BUILD_URL}
Failure Cause: ${BUILD_CAUSE}''')
                }
            }
        }
    }
}
  • Run the seed project to generate your template. You can then use this DSL script to create new projects, or extend it to include other default settings.
Important Notes
  • Before setting up any of these solutions, make sure you’ve configured the global SMTP settings for email-ext in Jenkins’ System Configuration (under Extended E-mail Notification). This ensures emails can actually be sent.
  • If you use the template project approach, updating the template will only affect new projects—existing ones won’t inherit changes unless you re-copy the template.
  • For the Groovy script method, double-check file permissions to ensure Jenkins can read and execute the script.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:02:23