Skip to content

Commit 532274a

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 1da1032 commit 532274a

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

Diff for: action.yml

+27-16
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,42 @@ runs:
6868
$setupExe = "$vol\setup.exe"
6969
$setupFileName = "setup-$platform.exe"
7070
71-
$maxRetries = 3
72-
$retryCount = 0
73-
$success = $false
74-
75-
while (-not $success -and $retryCount -lt $maxRetries) {
76-
try {
77-
Invoke-WebRequest -Uri "https://cygwin.com/$setupFileName" -OutFile $setupExe
78-
$success = $true
79-
} catch {
80-
Write-Output "Attempt $($retryCount + 1) failed. Retrying..."
81-
Start-Sleep -Seconds 3
82-
$retryCount++
71+
function Invoke-WebRequest-With-Retry {
72+
param (
73+
$Uri,
74+
$OutFile
75+
)
76+
77+
$maxRetries = 3
78+
$retryCount = 0
79+
$success = $false
80+
81+
while (-not $success -and $retryCount -lt $maxRetries) {
82+
try {
83+
Invoke-WebRequest -Uri $Uri -OutFile $OutFile
84+
$success = $true
85+
} catch [System.Net.WebException] {
86+
Write-Output "Attempt $($retryCount + 1) failed. Retrying..."
87+
Start-Sleep -Seconds 3
88+
$retryCount++
89+
}
8390
}
84-
}
8591
86-
if (-not $success) {
87-
throw "Failed to download $setupFileName after $maxRetries attempts."
92+
if (-not $success) {
93+
throw "Failed to download $setupFileName after $maxRetries attempts."
94+
}
8895
}
8996
97+
Invoke-WebRequest-With-Retry "https://cygwin.com/$setupFileName" $setupExe
98+
9099
if ((Get-Item -LiteralPath $setupExe).Length -eq 0) {
91100
throw "The downloaded setup has a zero length!"
92101
}
93102
94103
if ('${{ inputs.check-hash }}' -eq 'true') {
95-
$expectedHashLines = $(Invoke-WebRequest -Uri https://cygwin.com/sha512.sum).ToString() -split "`n"
104+
$hashFile = "$vol\sha512.sum"
105+
Invoke-WebRequest-With-Retry https://cygwin.com/sha512.sum $hashFile
106+
$expectedHashLines = Get-Content $hashFile
96107
$expectedHash = ''
97108
foreach ($expectedHashLine in $expectedHashLines) {
98109
if ($expectedHashLine.EndsWith(" $setupFileName")) {

0 commit comments

Comments
 (0)