Skip to content

adding slack notification to channel if it loops for more than 5 mins #1112

adding slack notification to channel if it loops for more than 5 mins

adding slack notification to channel if it loops for more than 5 mins #1112

Workflow file for this run

name: CI
on:
pull_request:
jobs:
CI:
name: "CI"
runs-on: Windows
timeout-minutes: 60
steps:
- name: Check out the GM-TestFramework repo
uses: actions/checkout@v4
with:
path: GM-TF
# - name: Wait for 10 Minutes
# run: Start-Sleep -Seconds 300
# shell: pwsh
- run: .\setup.bat
working-directory: GM-TF
shell: cmd
- name: Testing on PR
run: |
$startTime = Get-Date
$timeoutMinutes = 5
$maxTime = $startTime.AddMinutes($timeoutMinutes)
$logDirectory = "${{ github.workspace }}\GM-TF\logs"
# Ensure the log directory exists
if (-not (Test-Path -Path $logDirectory)) {
New-Item -Path $logDirectory -ItemType Directory | Out-Null
}
$stdoutFile = Join-Path -Path $logDirectory -ChildPath "build_stdout.txt"
$stderrFile = Join-Path -Path $logDirectory -ChildPath "build_stderr.txt"
# Function to send Slack notification
function Send-SlackNotification {
param (
[string]$message
)
$slackPayload = @{
text = $message
} | ConvertTo-Json -Depth 10
Invoke-RestMethod -Uri "${{ secrets.SLACK_WEBHOOK_URL }}" -Method POST -ContentType "application/json" -Body $slackPayload
}
Write-Host "Starting build..."
try {
# Initialize the process start info
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "cmd.exe"
$startInfo.Arguments = "/c run_ci.bat C:\GM-TestFramework\configs\config_windows.json https://gms.yoyogames.com/Zeus-Runtime-Nocturnus-I.rss"
$startInfo.RedirectStandardOutput = $true
$startInfo.RedirectStandardError = $true
$startInfo.UseShellExecute = $false
$startInfo.CreateNoWindow = $true
# Start the process
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null
# Open the log files for writing
$stdoutWriter = [System.IO.StreamWriter]::new($stdoutFile, $false)
$stderrWriter = [System.IO.StreamWriter]::new($stderrFile, $false)
# # Event handlers for asynchronous read
# $outputLines = New-Object System.Collections.ArrayList
# $errorLines = New-Object System.Collections.ArrayList
# $process.add_OutputDataReceived({
# param([System.Object] $sender, [System.Diagnostics.DataReceivedEventArgs] $e)
# if ($e.Data) {
# $outputLines.Add($e.Data) | Out-Null
# Write-Host $e.Data
# }
# })
# $process.add_ErrorDataReceived({
# param([System.Object] $sender, [System.Diagnostics.DataReceivedEventArgs] $e)
# if ($e.Data) {
# $errorLines.Add($e.Data) | Out-Null
# Write-Host $e.Data
# }
# })
# # Start the process and begin asynchronous read
# $process.Start() | Out-Null
# $process.BeginOutputReadLine()
# $process.BeginErrorReadLine()
# Monitor the process for timeout
while (-not $process.HasExited) {
if ((Get-Date) -gt $maxTime) {
# If execution exceeds the timeout, send a Slack notification and terminate the process
Send-SlackNotification "TestFramework build ${{ github.run_number }} exceeded the $timeoutMinutes-minute timeout and was terminated."
$process.Kill()
throw "Test Framework Build exceeded the $timeoutMinutes-minute timeout and was terminated."
}
# Read and display standard output
while (-not $process.StandardOutput.EndOfStream) {
$outputLine = $process.StandardOutput.ReadLine()
Write-Host $outputLine
$stdoutWriter.WriteLine($outputLine)
}
# Read and display standard error
while (-not $process.StandardError.EndOfStream) {
$errorLine = $process.StandardError.ReadLine()
Write-Host $errorLine
$stderrWriter.WriteLine($errorLine)
}
Start-Sleep -Milliseconds 100
}
# Ensure all output is processed after the process exits
while (-not $process.StandardOutput.EndOfStream) {
$outputLine = $process.StandardOutput.ReadLine()
Write-Host $outputLine
$stdoutWriter.WriteLine($outputLine)
}
while (-not $process.StandardError.EndOfStream) {
$errorLine = $process.StandardError.ReadLine()
Write-Host $errorLine
$stderrWriter.WriteLine($errorLine)
}
# Close the log files
# $stdoutWriter.Close()
# $stderrWriter.Close()
# # Wait for the process to exit
# $process.WaitForExit()
# # Write output to log files
# Set-Content -Path $stdoutFile -Value ($outputLines -join [Environment]::NewLine)
# Set-Content -Path $stderrFile -Value ($errorLines -join [Environment]::NewLine)
# Check the exit code of the process
if ($process.ExitCode -ne 0) {
# $errorOutput = $errorLines -join [Environment]::NewLine
$errorOutput = Get-Content -Path $stderrFile -Raw
Send-SlackNotification "TestFramework build ${{ github.run_number }} failed with exit code $($process.ExitCode). Error details: $errorOutput"
throw "Build process exited with code $($process.ExitCode)."
} else {
Write-Host "Build completed successfully."
}
} catch {
Write-Error "An error occurred: $_"
Send-SlackNotification "An error occurred during the build process: $_"
throw
}
working-directory: GM-TF
shell: pwsh
- name: Upload Results Artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: summary_file
path: ${{ github.workspace }}\GM-TF\results