From 19c4d141f0b0930dc51e63153a7bbe93860fc659 Mon Sep 17 00:00:00 2001 From: Jon Turney Date: Tue, 8 Apr 2025 21:33:11 +0100 Subject: [PATCH 1/3] Consistently use throw to terminate with an error --- action.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/action.yml b/action.yml index efad135..5154fe2 100644 --- a/action.yml +++ b/action.yml @@ -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 }}' From 82a91d5027d2523e3d3a8276f4f8e3672b64661a Mon Sep 17 00:00:00 2001 From: Jon Turney Date: Wed, 26 Mar 2025 14:25:51 +0000 Subject: [PATCH 2/3] Add a retry mechanism for fetching setup executable --- action.yml | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 5154fe2..36dc46b 100644 --- a/action.yml +++ b/action.yml @@ -67,7 +67,28 @@ runs: $setupExe = "$vol\setup.exe" $setupFileName = "setup-$platform.exe" - Invoke-WebRequest "https://cygwin.com/$setupFileName" -OutFile $setupExe + + $maxRetries = 5 + $retryCount = 0 + $success = $false + $delay = 2 + + while (-not $success -and $retryCount -lt $maxRetries) { + try { + Invoke-WebRequest -Uri "https://cygwin.com/$setupFileName" -OutFile $setupExe + $success = $true + } catch { + 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." + } + if ((Get-Item -LiteralPath $setupExe).Length -eq 0) { throw "The downloaded setup has a zero length!" } From 9c0a082d01fd46a52f924c3ca0c99865ec2e8f16 Mon Sep 17 00:00:00 2001 From: Jon Turney Date: Tue, 8 Apr 2025 21:50:04 +0100 Subject: [PATCH 3/3] Factor out download retry as a function, to use on sha512.sum file also Also, don't use a naked catch, so we don't trap sytnax or usage errors etc., only network failures. --- action.yml | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/action.yml b/action.yml index 36dc46b..9af86f1 100644 --- a/action.yml +++ b/action.yml @@ -68,33 +68,44 @@ runs: $setupExe = "$vol\setup.exe" $setupFileName = "setup-$platform.exe" - $maxRetries = 5 - $retryCount = 0 - $success = $false - $delay = 2 - - while (-not $success -and $retryCount -lt $maxRetries) { - try { - Invoke-WebRequest -Uri "https://cygwin.com/$setupFileName" -OutFile $setupExe - $success = $true - } catch { - Write-Output "Attempt $($retryCount + 1) failed. Retrying..." - Start-Sleep -Seconds $delay - $retryCount++ - $delay += $delay + 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." + 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")) {