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

Azure Pipeline发送邮件任务如何发送最新带时间戳的HTML报告

解决Azure Pipeline SendEmail@1任务只发送最新带时间戳HTML报告的问题

当前配置用*.html会选中目标文件夹里所有HTML文件,要只发送最新的带时间戳报告,需要先通过脚本定位到最新文件,再将路径传给SendEmail任务,具体步骤如下:

步骤1:添加脚本任务获取最新报告路径

根据你的代理操作系统选择对应的脚本任务,放在SendEmail任务之前:

适用于Windows代理(PowerShell任务)

添加PowerShell@2任务,通过脚本找到最新修改的HTML文件,并设置管道变量:

$reportPath = "$(System.DefaultWorkingDirectory)/target/Extent-Reports/"
$latestFile = Get-ChildItem -Path $reportPath -Filter *.html | Sort-Object LastWriteTime -Descending | Select-Object -First 1

# 校验是否找到文件,避免后续任务出错
if ($latestFile -eq $null) {
    Write-Error "未在目标路径找到HTML报告文件"
    exit 1
}

Write-Host "##vso[task.setvariable variable=LatestReportPath]$($latestFile.FullName)"

适用于Linux/macOS代理(Bash任务)

添加Bash@3任务,通过命令筛选最新的HTML文件并设置变量:

report_path="$(System.DefaultWorkingDirectory)/target/Extent-Reports/"
latest_file=$(ls -t "$report_path"*.html | head -n 1)

# 校验是否找到文件
if [ -z "$latest_file" ]; then
    echo "未在目标路径找到HTML报告文件"
    exit 1
fi

echo "##vso[task.setvariable variable=LatestReportPath]$latest_file"

步骤2:修改SendEmail@1任务配置

把原来的Attachment字段值替换为刚才设置的变量$(LatestReportPath),修改后的任务配置如下:

- task: SendEmail@1
  inputs:
    To: ''
    CC: ''
    From: ''
    Subject: 'Email Report'
    Body: 'Please Find the attachment'
    BodyAsHtml: false
    AddAttachment: true
    SmtpPort: '587'
    Attachment: '$(LatestReportPath)'
    SmtpServer: ''
    SmtpUsername: ''
    SmtpPassword: ''
    UseSSL: true

这样就能确保只发送目标文件夹里最新的那一份带时间戳的HTML报告了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 19:02:21