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

基于部署分支配置Email-ext插件邮件收件人及预发送脚本问题

Great question! Let's break this down for you and tie it directly to your branch-based recipient requirement:

1. Can you write bash scripts in the pre-send script?

Email-ext’s pre-send script uses Groovy as its core language, but you can absolutely execute bash commands within it. Groovy provides simple ways to spawn shell processes and capture their output—so you’re not limited to pure Groovy logic. For example, you can run bash commands using Groovy’s execute() method, or (if you’re in a Jenkins pipeline context) use the built-in sh step to run bash scripts and fetch results.

2. Can variables set in bash scripts be used in the pre-send script?

Yes, but you need to bridge the bash and Groovy contexts properly:

  • Capture bash command output into Groovy variables: This is the most common approach. When you run a bash command from Groovy, you can capture its stdout and assign it to a Groovy variable for later use.
  • Access environment variables: If you set environment variables in your bash script, you can retrieve them in Groovy using System.getenv("VAR_NAME").

Example Script for Your Branch-Based Recipient Logic

Here’s a complete Groovy script that implements your requirement—using bash to fetch the branch name, then setting recipients based on whether it’s master/develop or another branch:

// Fetch the current deployment branch using bash
def branch = "git rev-parse --abbrev-ref HEAD".execute().text.trim()

// Define your two recipient groups
def coreRecipients = "lead-dev@yourcompany.com,dev-team@yourcompany.com"
def featureRecipients = "feature-author@yourcompany.com,peer-reviewers@yourcompany.com"

// Update the email recipients based on the branch
if (branch == "master" || branch == "develop") {
    msg.setRecipients(
        javax.mail.Message.RecipientType.TO,
        javax.mail.internet.InternetAddress.parse(coreRecipients)
    )
} else {
    msg.setRecipients(
        javax.mail.Message.RecipientType.TO,
        javax.mail.internet.InternetAddress.parse(featureRecipients)
    )
}

Quick Notes

  • If you’re running this in a Jenkins pipeline, you can use the sh step instead of execute() for better integration:
    def branch = sh(script: 'echo $BRANCH_NAME', returnStdout: true).trim()
    
  • Make sure the user running the script has permission to execute git commands (or whatever bash logic you need) in your environment.
  • The msg object is provided by Email-ext and lets you modify the email’s recipients, subject, body, etc., before sending.

内容的提问来源于stack exchange,提问作者Tara Prasad Gurung

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:54:31