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

如何在Jenkinsfile(Groovy脚本)中实现构建后发送带Zip附件的邮件?

Solution to Zip Folder and Attach to Jenkins Emails via Jenkinsfile

Got it, let's get this sorted for you. You want to zip a specific folder and attach it to your build success/failure emails directly in the Jenkinsfile—no extra plugin configuration required. Here's how to modify your existing pipeline to make this work:

Modified Jenkinsfile with Zip & Attachment Logic

pipeline {
    agent any
    stages {
        stage('Testing') {
            steps {
                sh 'chmod +x mvnw'
                sh './mvnw clean verify serenity:aggregate'
            }
        }
    }
    post {
        failure {
            script {
                // Zip your target folder (update the path to match your actual folder)
                sh 'zip -r build-failure-artifacts.zip target/'
                
                // Send failure email with the zipped attachment
                mail (
                    to: 'email@gmail.com',
                    subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) failed",
                    body: "Please visit ${env.BUILD_URL} for further details.",
                    attachments: [file: 'build-failure-artifacts.zip']
                )
            }
        }
        success {
            script {
                // Zip the Serenity report folder (adjust path to your report location)
                sh 'zip -r serenity-test-report.zip target/site/serenity/'
                
                // Send success email with the zipped report
                mail (
                    to: 'email@gmail.com',
                    subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) succeeded!",
                    body: "Please visit ${env.BUILD_URL} for further details.",
                    attachments: [file: 'serenity-test-report.zip']
                )
            }
        }
    }
}

Key Details to Note:

  • Folder Path Adjustment: Replace the paths in the zip commands (like target/ or target/site/serenity/) with the actual folder you want to package. For example, if you have logs in a logs/ directory, use zip -r build-logs.zip logs/.
  • Zip Command: The zip -r flag ensures we recursively package all files and subfolders inside the target directory—no content gets left out.
  • Attachments Parameter: The attachments argument in the mail function takes a list of file paths. If you need multiple attachments, just add more entries: attachments: [file: 'report.zip', file: 'logs.zip'].
  • Windows Agent Compatibility: If your Jenkins agent runs on Windows, replace the sh zip command with a PowerShell command:
    powershell "Compress-Archive -Path target/site/serenity/* -DestinationPath serenity-test-report.zip -Force"
    
    The -Force flag overwrites the zip file if it already exists from a previous build.

This approach keeps everything contained within your Jenkinsfile, no extra plugin setup needed—exactly what you asked for.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:34:38