Skip to content

Commit e67b1f4

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 8381d6d commit e67b1f4

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

Diff for: action.yml

+29-18
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,44 @@ runs:
6868
$setupExe = "$vol\setup.exe"
6969
$setupFileName = "setup-$platform.exe"
7070
71-
$maxRetries = 5
72-
$retryCount = 0
73-
$success = $false
74-
$delay = 2
75-
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
71+
function Invoke-WebRequest-With-Retry {
72+
param (
73+
$Uri,
74+
$OutFile
75+
)
76+
77+
$maxRetries = 5
78+
$retryCount = 0
79+
$success = $false
80+
$delay = 2
81+
82+
while (-not $success -and $retryCount -lt $maxRetries) {
83+
try {
84+
Invoke-WebRequest -Uri $Uri -OutFile $OutFile
85+
$success = $true
86+
} catch [System.Net.WebException] {
87+
Write-Output "Attempt $($retryCount + 1) failed. Retrying..."
88+
Start-Sleep -Seconds $delay
89+
$retryCount++
90+
$delay += $delay
91+
}
8592
}
86-
}
8793
88-
if (-not $success) {
89-
throw "Failed to download $setupFileName after $maxRetries attempts."
94+
if (-not $success) {
95+
throw "Failed to download $setupFileName after $maxRetries attempts."
96+
}
9097
}
9198
99+
Invoke-WebRequest-With-Retry "https://cygwin.com/$setupFileName" $setupExe
100+
92101
if ((Get-Item -LiteralPath $setupExe).Length -eq 0) {
93102
throw "The downloaded setup has a zero length!"
94103
}
95104
96105
if ('${{ inputs.check-hash }}' -eq 'true') {
97-
$expectedHashLines = $(Invoke-WebRequest -Uri https://cygwin.com/sha512.sum).ToString() -split "`n"
106+
$hashFile = "$vol\sha512.sum"
107+
Invoke-WebRequest-With-Retry https://cygwin.com/sha512.sum $hashFile
108+
$expectedHashLines = Get-Content $hashFile
98109
$expectedHash = ''
99110
foreach ($expectedHashLine in $expectedHashLines) {
100111
if ($expectedHashLine.EndsWith(" $setupFileName")) {

0 commit comments

Comments
 (0)