Skip to content

Commit 772b895

Browse files
committed
Failed job notification
1 parent a65934d commit 772b895

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
</head>
7+
8+
<body>
9+
<h1 style="color: #FF0000; font-size: 24px; font-weight: bold;">Pipeline Failure Alert 🚨</h1>
10+
<p style="font-size: 18px;">The pipeline **{{ pipelineName }}** has failed.</p>
11+
<p style="font-size: 16px;">You can review the details at the following links:</p>
12+
<ul style="list-style-type: none; padding-left: 0;">
13+
<li style="font-size: 16px; margin: 10px 0;">
14+
<a href="{{ pipelineUrl }}" style="color: #0078D4; text-decoration: none;">📝 Pipeline Overview</a>
15+
</li>
16+
<li style="font-size: 16px; margin: 10px 0;">
17+
<a href="{{ runUrl }}" style="color: #0078D4; text-decoration: none;">❌ Failed Run Details</a>
18+
</li>
19+
</ul>
20+
<hr style="border: none; height: 1px; background-color: #0078D4; margin: 20px 0;">
21+
<p style="font-size: 16px;">Please check and address the issue as soon as possible.</p>
22+
<p style="font-size: 16px; margin-top: 20px; font-style: italic;">Sincerely,</p>
23+
<p style="font-size: 16px; font-weight: bold;">Your Azure CLI Tools Team</p>
24+
</body>
25+
26+
</html>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
param (
2+
[string]$RepoRoot
3+
)
4+
5+
$utilFilePath = Join-Path $RepoRoot '.azure-pipelines' 'PipelineSteps' 'BatchGeneration' 'util.psm1'
6+
Import-Module $utilFilePath -Force
7+
8+
$batchGenSubscribers = @(
9+
10+
)
11+
$receivers = $batchGenSubscribers -join ","
12+
13+
$pipelineName = $env:BUILD_DEFINITIONNAME
14+
$pipelineUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECT)/_build?definitionId=$($env:SYSTEM_DEFINITIONID)"
15+
$runUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECT)/_build/results?buildId=$($env:BUILD_BUILDID)"
16+
17+
$templateFilePath = Join-Path $RepoRoot '.azure-pipelines' 'PipelineSteps' 'BatchGeneration' 'FailedJobTeamsTemplate.html'
18+
$notificationTemplate = Get-Content -Path $templateFilePath -Raw
19+
$notificationContent = $notificationTemplate -replace "{{ pipelineName }}", $pipelineName `
20+
-replace "{{ pipelineUrl }}", $pipelineUrl `
21+
-replace "{{ runUrl }}", $runUrl
22+
23+
Send-Teams `
24+
-to $receivers `
25+
-title "Batch Generation Job Failed" `
26+
-content $notificationContent
27+

.azure-pipelines/PipelineSteps/BatchGeneration/util.psm1

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,42 @@ function Get-Targets {
109109
Write-Host
110110
return $targetGroup
111111
}
112+
113+
function Send-Teams {
114+
param (
115+
[string]$to,
116+
[string]$title,
117+
[string]$content
118+
)
119+
120+
$teamsUrl = $env:TEAMS_URL
121+
if ([string]::IsNullOrEmpty($teamsUrl)) {
122+
Write-Host "TEAMS_URL environment variable is not set." -ForegroundColor Red
123+
exit 1
124+
}
125+
126+
if ([string]::IsNullOrEmpty($to)) {
127+
Write-Host "'to' parameter is empty, nothing to send." -ForegroundColor Yellow
128+
return 0
129+
}
130+
131+
$body = @{
132+
to = $to
133+
title = $title
134+
content = $content
135+
} | ConvertTo-Json -Depth 3
136+
137+
try {
138+
Invoke-RestMethod -Uri $teamsUrl -Method Post -Headers @{
139+
'Accept' = 'application/json'
140+
'Content-Type' = 'application/json'
141+
} -Body $body
142+
143+
Write-Host "Message sent successfully."
144+
return 0
145+
}
146+
catch {
147+
Write-Host "Failed to send message: $_" -ForegroundColor Red
148+
exit 1
149+
}
150+
}

.azure-pipelines/batch-generation.yml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,4 +458,26 @@ stages:
458458
inputs:
459459
targetPath: artifacts
460460
artifact: 'reports'
461-
condition: always()
461+
condition: always()
462+
463+
- stage: FailedJobNotification
464+
dependsOn:
465+
- Generate
466+
- Build
467+
- Test
468+
- Report
469+
condition: failed()
470+
jobs:
471+
- job: FailJobNotification
472+
steps:
473+
- task: PowerShell@2
474+
name: notification
475+
displayName: "teams notification on failed job"
476+
inputs:
477+
targetType: inline
478+
pwsh: true
479+
script: |
480+
$notificationScriptPath = Join-Path "$(Build.SourcesDirectory)" '.azure-pipelines' 'PipelineSteps' 'BatchGeneration' 'notify-failed-job.ps1'
481+
& $notificationScriptPath -RepoRoot "$(Build.SourcesDirectory)"
482+
env:
483+
TEAMS_URL: $(TEAMS_URL)

0 commit comments

Comments
 (0)