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

如何通过Jenkins Pipeline发送含双Git仓库变更的邮件?

Solution: Include Git Changes from Multiple Repos in Jenkins Pipeline Email

Got it, let's tweak your Jenkins pipeline to send emails with actual commit details from both your repos instead of that generic message. Here's how to modify your finally block to pull in changes from both repo1 and repo2:

Modified Pipeline Script Snippet

finally {
    // Set up the base email subject
    def DEFAULT_SUBJECT = "project Blah Build: #${BUILD_NUMBER} - ${currentBuild.result}"
    
    // Start building the email body
    def emailBody = "Commits made to project Blah:\n\n"

    // --- Fetch changes for repo1 ---
    // Replace 'path/to/repo1' with the actual directory where repo1 is checked out in your pipeline
    def repo1CommitLog = sh(
        script: "cd path/to/repo1 && git log --oneline ${env.GIT_PREVIOUS_COMMIT}..${env.GIT_COMMIT}",
        returnStdout: true
    ).trim()

    // --- Fetch changes for repo2 ---
    // Adjust commit variables if repo2 uses different Jenkins environment vars
    // Replace 'path/to/repo2' with repo2's checkout directory
    def repo2CommitLog = sh(
        script: "cd path/to/repo2 && git log --oneline ${env.GIT_PREVIOUS_COMMIT_REPO2}..${env.GIT_COMMIT_REPO2}",
        returnStdout: true
    ).trim()

    // Add repo1 changes to the email if any exist
    if (repo1CommitLog) {
        emailBody += "=== repo1 (userbar/repo1) Changes ===\n${repo1CommitLog}\n\n"
    }

    // Add repo2 changes to the email if any exist
    if (repo2CommitLog) {
        emailBody += "=== repo2 (userfoo/repo2) Changes ===\n${repo2CommitLog}\n\n"
    }

    // Fallback message if there are no changes in either repo
    if (!repo1CommitLog && !repo2CommitLog) {
        emailBody += "No new commits detected in either repository.\n"
    }

    // Send the updated email
    mail to: 'josh.kirby@wysdom.ai', subject: DEFAULT_SUBJECT, body: emailBody
}

Key Notes to Make This Work:

  • Adjust checkout paths: Replace path/to/repo1 and path/to/repo2 with the actual directories where each repo is checked out in your Jenkins workspace. If you used checkout scm for both repos, they might be in subdirectories matching the repo names.
  • Commit variable names: If your pipeline checks out repo2 separately, the Jenkins environment variables for its commits might not be GIT_PREVIOUS_COMMIT_REPO2 and GIT_COMMIT_REPO2. If you used a named checkout (like checkout([$class: 'GitSCM', ...], changelog: true, poll: true, name: 'repo2')), you can access its commit info via env.GIT_PREVIOUS_COMMIT_repo2 and env.GIT_COMMIT_repo2 (note the lowercase suffix).
  • Customize commit format: The git log --oneline gives a short, clean output (hash + commit message). If you want more details (author, timestamp, etc.), replace --oneline with a custom format like:
    git log --pretty=format:"%h - %an, %ar : %s" ${startCommit}..${endCommit}
    
    This will show: <short-hash> - <author-name>, <relative-time> : <commit-message>.
  • Handle first builds: If it's the first build of the pipeline, GIT_PREVIOUS_COMMIT might be empty. To avoid errors, adjust the git log command to fall back to recent commits:
    git log --oneline ${env.GIT_PREVIOUS_COMMIT ?: 'HEAD~10'}..${env.GIT_COMMIT}
    
    This will show the last 10 commits if there's no previous build commit to compare against.

That should get your email showing actual changes from both repos instead of the static message!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:16:37