Skip to content

Commit 9c4305a

Browse files
remove file with retry on windows
1 parent df97225 commit 9c4305a

File tree

2 files changed

+93
-12
lines changed

2 files changed

+93
-12
lines changed

.github/workflows/scripts/windows/install-vsb.ps1

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,40 @@ function Invoke-WebRequestWithRetry {
9292
return $false
9393
}
9494

95+
function Remove-FileWithRetry {
96+
param (
97+
[string]$Path
98+
)
99+
100+
$attempt = 1
101+
102+
while ($attempt -le $MaxRetries) {
103+
try {
104+
if (Test-Path $Path) {
105+
Remove-Item -Force $Path -ErrorAction Stop
106+
Write-Host "Successfully removed $Path"
107+
return $true
108+
} else {
109+
return $true
110+
}
111+
}
112+
catch {
113+
if ($attempt -eq $MaxRetries) {
114+
Write-Host "Warning: Failed to remove $Path after $MaxRetries attempts: $($_.Exception.Message)"
115+
Write-Host "The file may be locked by another process. It will be cleaned up later."
116+
return $false
117+
}
118+
119+
Write-Host "Attempt $attempt to remove $Path failed, retrying in ${RetryDelay}s..."
120+
Start-Sleep -Seconds $RetryDelay
121+
}
122+
123+
$attempt++
124+
}
125+
126+
return $false
127+
}
128+
95129
function Install-VisualStudioBuildTools {
96130
param (
97131
[string]$Url,
@@ -111,15 +145,17 @@ function Install-VisualStudioBuildTools {
111145
}
112146
catch {
113147
Write-Host "Download FAILED: $($_.Exception.Message)"
148+
Remove-FileWithRetry -Path $installerPath
114149
exit 1
115150
}
116151

117152
Write-Host -NoNewLine ('Verifying SHA256 ({0}) ... ' -f $Sha256)
118153
$Hash = Get-FileHash $installerPath -Algorithm sha256
119-
if ($Hash.Hash -eq $VSB_SHA256) {
154+
if ($Hash.Hash -eq $Sha256) {
120155
Write-Host 'SUCCESS'
121156
} else {
122157
Write-Host ('FAILED ({0})' -f $Hash.Hash)
158+
Remove-FileWithRetry -Path $installerPath
123159
exit 1
124160
}
125161

@@ -138,16 +174,17 @@ function Install-VisualStudioBuildTools {
138174
Write-Host 'SUCCESS'
139175
} else {
140176
Write-Host ('FAILED ({0})' -f $Process.ExitCode)
177+
Remove-FileWithRetry -Path $installerPath
141178
exit 1
142179
}
143180
}
144181
catch {
145182
Write-Host "FAILED: $($_.Exception.Message)"
146-
Remove-Item -Force $installerPath -ErrorAction SilentlyContinue
183+
Remove-FileWithRetry -Path $installerPath
147184
exit 1
148185
}
149186

150-
Remove-Item -Force $installerPath -ErrorAction SilentlyContinue
187+
Remove-FileWithRetry -Path $installerPath
151188
}
152189

153190
$VSB = 'https://download.visualstudio.microsoft.com/download/pr/5536698c-711c-4834-876f-2817d31a2ef2/c792bdb0fd46155de19955269cac85d52c4c63c23db2cf43d96b9390146f9390/vs_BuildTools.exe'

.github/workflows/scripts/windows/swift/install-swift.ps1

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,40 @@ function Invoke-WebRequestWithRetry {
9292
return $false
9393
}
9494

95+
function Remove-FileWithRetry {
96+
param (
97+
[string]$Path
98+
)
99+
100+
$attempt = 1
101+
102+
while ($attempt -le $MaxRetries) {
103+
try {
104+
if (Test-Path $Path) {
105+
Remove-Item -Force $Path -ErrorAction Stop
106+
Write-Host "Successfully removed $Path"
107+
return $true
108+
} else {
109+
return $true
110+
}
111+
}
112+
catch {
113+
if ($attempt -eq $MaxRetries) {
114+
Write-Host "Warning: Failed to remove $Path after $MaxRetries attempts: $($_.Exception.Message)"
115+
Write-Host "The file may be locked by another process. It will be cleaned up later."
116+
return $false
117+
}
118+
119+
Write-Host "Attempt $attempt to remove $Path failed, retrying in ${RetryDelay}s..."
120+
Start-Sleep -Seconds $RetryDelay
121+
}
122+
123+
$attempt++
124+
}
125+
126+
return $false
127+
}
128+
95129
function Install-Swift {
96130
param (
97131
[string]$Url,
@@ -108,6 +142,7 @@ function Install-Swift {
108142
}
109143
catch {
110144
Write-Host "FAILED: $($_.Exception.Message)"
145+
Remove-FileWithRetry -Path installer.exe
111146
exit 1
112147
}
113148

@@ -117,18 +152,27 @@ function Install-Swift {
117152
Write-Host 'SUCCESS'
118153
} else {
119154
Write-Host ('FAILED ({0})' -f $Hash.Hash)
155+
Remove-FileWithRetry -Path installer.exe
120156
exit 1
121157
}
122158
Write-Host -NoNewLine 'Installing Swift ... '
123-
$Process = Start-Process installer.exe -Wait -PassThru -NoNewWindow -ArgumentList @(
124-
'/quiet',
125-
'/norestart'
126-
)
127-
if ($Process.ExitCode -eq 0) {
128-
Write-Host 'SUCCESS'
129-
} else {
130-
Write-Host ('FAILED ({0})' -f $Process.ExitCode)
159+
try {
160+
$Process = Start-Process installer.exe -Wait -PassThru -NoNewWindow -ArgumentList @(
161+
'/quiet',
162+
'/norestart'
163+
)
164+
if ($Process.ExitCode -eq 0) {
165+
Write-Host 'SUCCESS'
166+
} else {
167+
Write-Host ('FAILED ({0})' -f $Process.ExitCode)
168+
Remove-FileWithRetry -Path installer.exe
169+
exit 1
170+
}
171+
}
172+
catch {
173+
Write-Host "FAILED: $($_.Exception.Message)"
174+
Remove-FileWithRetry -Path installer.exe
131175
exit 1
132176
}
133-
Remove-Item -Force installer.exe
177+
Remove-FileWithRetry -Path installer.exe
134178
}

0 commit comments

Comments
 (0)