Skip to content

Commit b520762

Browse files
committed
Retry nuget restore when it fails
1 parent 2f0d948 commit b520762

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

build.targets.ps1

+27-4
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,34 @@ function UpdateNuspec($nuspecPath, $language) {
145145

146146
function RestorePkgs($sln) {
147147
Write-Host "Restoring $sln..." -ForegroundColor Green
148-
if (-Not (Test-Path $sln)) {
149-
Write-Host "File $sln does not exist, exiting." -ForegroundColor DarkRed
150-
return
148+
Retry {
149+
. $nugetExe restore $sln
150+
if ($LASTEXITCODE) { throw "Nuget restore for $sln failed." }
151151
}
152-
exec { . $nugetExe restore $sln }
152+
}
153+
154+
function Retry {
155+
Param (
156+
[parameter(Position=0,Mandatory=1)]
157+
[ScriptBlock]$cmd,
158+
[parameter(Position=1,Mandatory=0)]
159+
[int]$times = 3
160+
)
161+
$retrycount = 0
162+
while ($retrycount -lt $times){
163+
try {
164+
& $cmd
165+
if (!$?) {
166+
throw "Command failed."
167+
}
168+
return
169+
}
170+
catch {
171+
Write-Host -ForegroundColor Red "Failed: ($($_.Exception.Message)), retrying."
172+
}
173+
$retrycount++
174+
}
175+
throw "Command '$($cmd.ToString())' failed."
153176
}
154177

155178
function TestPath($paths) {

0 commit comments

Comments
 (0)