如何通过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/repo1andpath/to/repo2with the actual directories where each repo is checked out in your Jenkins workspace. If you usedcheckout scmfor 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_REPO2andGIT_COMMIT_REPO2. If you used a named checkout (likecheckout([$class: 'GitSCM', ...], changelog: true, poll: true, name: 'repo2')), you can access its commit info viaenv.GIT_PREVIOUS_COMMIT_repo2andenv.GIT_COMMIT_repo2(note the lowercase suffix). - Customize commit format: The
git log --onelinegives a short, clean output (hash + commit message). If you want more details (author, timestamp, etc.), replace--onelinewith a custom format like:
This will show:git log --pretty=format:"%h - %an, %ar : %s" ${startCommit}..${endCommit}<short-hash> - <author-name>, <relative-time> : <commit-message>. - Handle first builds: If it's the first build of the pipeline,
GIT_PREVIOUS_COMMITmight be empty. To avoid errors, adjust the git log command to fall back to recent commits:
This will show the last 10 commits if there's no previous build commit to compare against.git log --oneline ${env.GIT_PREVIOUS_COMMIT ?: 'HEAD~10'}..${env.GIT_COMMIT}
That should get your email showing actual changes from both repos instead of the static message!
内容的提问来源于stack exchange,提问作者user9753902
相关产品推荐
相关产品推荐

