如何为所有新建项目添加默认的构建失败邮件通知构建后操作?
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:
- Locate your Jenkins home directory (usually
~/.jenkinson Linux/macOS, orC:\Program Files\Jenkinson Windows) - Create a folder named
init.groovy.dif it doesn’t already exist - Inside that folder, create a new file called
default-email-ext.groovywith 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() }
- 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
相关产品推荐
相关产品推荐

