Skip to content

Add a retry mechanism for fetching setup executable #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ runs:
$platform = $platform -replace '^i686$', 'x86'
# validate that platform is one of the expected values
if (($platform -ne 'x86') -and ($platform -ne 'x86_64')) {
echo "unknown platform $platform"
exit 1
throw "Unknown platform $platform."
}

$vol = '${{ inputs.work-vol }}'
Expand All @@ -68,13 +67,45 @@ runs:

$setupExe = "$vol\setup.exe"
$setupFileName = "setup-$platform.exe"
Invoke-WebRequest "https://cygwin.com/$setupFileName" -OutFile $setupExe

function Invoke-WebRequest-With-Retry {
param (
$Uri,
$OutFile
)

$maxRetries = 5
$retryCount = 0
$success = $false
$delay = 2

while (-not $success -and $retryCount -lt $maxRetries) {
try {
Invoke-WebRequest -Uri $Uri -OutFile $OutFile
$success = $true
} catch [System.Net.WebException] {
Write-Output "Attempt $($retryCount + 1) failed. Retrying..."
Start-Sleep -Seconds $delay
$retryCount++
$delay += $delay
}
}

if (-not $success) {
throw "Failed to download $setupFileName after $maxRetries attempts."
}
}

Invoke-WebRequest-With-Retry "https://cygwin.com/$setupFileName" $setupExe

if ((Get-Item -LiteralPath $setupExe).Length -eq 0) {
throw "The downloaded setup has a zero length!"
}

if ('${{ inputs.check-hash }}' -eq 'true') {
$expectedHashLines = $(Invoke-WebRequest -Uri https://cygwin.com/sha512.sum).ToString() -split "`n"
$hashFile = "$vol\sha512.sum"
Invoke-WebRequest-With-Retry https://cygwin.com/sha512.sum $hashFile
$expectedHashLines = Get-Content $hashFile
$expectedHash = ''
foreach ($expectedHashLine in $expectedHashLines) {
if ($expectedHashLine.EndsWith(" $setupFileName")) {
Expand Down
Loading