Skip to content

Commit 88fc5c1

Browse files
committed
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.
1 parent 82a91d5 commit 88fc5c1

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

action.yml

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,45 @@ runs:
6868
$setupExe = "$vol\setup.exe"
6969
$setupFileName = "setup-$platform.exe"
7070
71-
$maxRetries = 5
72-
$retryCount = 0
73-
$success = $false
71+
function Invoke-WebRequest-With-Retry {
72+
param (
73+
$Uri,
74+
$OutFile
75+
)
76+
77+
$maxRetries = 5
78+
$retryCount = 0
79+
$delay = 2
80+
$success = $false
7481
$delay = 2
7582
76-
while (-not $success -and $retryCount -lt $maxRetries) {
77-
try {
78-
Invoke-WebRequest -Uri "https://cygwin.com/$setupFileName" -OutFile $setupExe
79-
$success = $true
80-
} catch {
81-
Write-Output "Attempt $($retryCount + 1) failed. Retrying..."
82-
Start-Sleep -Seconds $delay
83-
$retryCount++
84-
$delay += $delay
83+
while (-not $success -and $retryCount -lt $maxRetries) {
84+
try {
85+
Invoke-WebRequest -Uri $Uri -OutFile $OutFile
86+
$success = $true
87+
} catch [System.Net.WebException] {
88+
Write-Output "Attempt $($retryCount + 1) failed. Retrying..."
89+
Start-Sleep -Seconds $delay
90+
$retryCount++
91+
$delay += $delay
92+
}
8593
}
86-
}
8794
88-
if (-not $success) {
89-
throw "Failed to download $setupFileName after $maxRetries attempts."
95+
if (-not $success) {
96+
throw "Failed to download $setupFileName after $maxRetries attempts."
97+
}
9098
}
9199
100+
Invoke-WebRequest-With-Retry "https://cygwin.com/bad/$setupFileName" $setupExe
101+
92102
if ((Get-Item -LiteralPath $setupExe).Length -eq 0) {
93103
throw "The downloaded setup has a zero length!"
94104
}
95105
96106
if ('${{ inputs.check-hash }}' -eq 'true') {
97-
$expectedHashLines = $(Invoke-WebRequest -Uri https://cygwin.com/sha512.sum).ToString() -split "`n"
107+
$hashFile = "$vol\sha512.sum"
108+
Invoke-WebRequest-With-Retry https://cygwin.com/sha512.sum $hashFile
109+
$expectedHashLines = Get-Content $hashFile
98110
$expectedHash = ''
99111
foreach ($expectedHashLine in $expectedHashLines) {
100112
if ($expectedHashLine.EndsWith(" $setupFileName")) {

0 commit comments

Comments
 (0)