diff --git a/eng/common/pipelines/templates/steps/sparse-checkout.yml b/eng/common/pipelines/templates/steps/sparse-checkout.yml index 24f4f5474..9e3cc3164 100644 --- a/eng/common/pipelines/templates/steps/sparse-checkout.yml +++ b/eng/common/pipelines/templates/steps/sparse-checkout.yml @@ -30,7 +30,34 @@ steps: # 7.2 behavior for command argument passing. Newer behaviors will result # in errors from git.exe. $PSNativeCommandArgumentPassing = 'Legacy' - + + function Retry() + { + Run 3 @args + } + + function Run() + { + $retries, $command, $arguments = $args + if ($retries -isnot [int]) { + $command, $arguments = $args + $retries = 0 + } + Write-Host "==>" $command $arguments + $attempt = 0 + $sleep = 5 + + while ($true) { + $attempt++ + & $command $arguments + if (!$LASTEXITCODE) { return } + if ($attempt -gt $retries) { exit $LASTEXITCODE } + Write-Warning "Attempt $attempt failed: $_. Trying again in $sleep seconds..." + Start-Sleep -Seconds $sleep + $sleep *= 2 + } + } + function SparseCheckout([Array]$paths, [Hashtable]$repository) { $dir = $repository.WorkingDirectory @@ -47,23 +74,18 @@ steps: Write-Host "Repository $($repository.Name) is being initialized." if ($repository.Commitish -match '^refs/pull/\d+/merge$') { - Write-Host "git clone --no-checkout --filter=tree:0 -c remote.origin.fetch='+$($repository.Commitish):refs/remotes/origin/$($repository.Commitish)' https://github.com/$($repository.Name) ." - git clone --no-checkout --filter=tree:0 -c remote.origin.fetch=''+$($repository.Commitish):refs/remotes/origin/$($repository.Commitish)'' https://github.com/$($repository.Name) . + Retry git clone --no-checkout --filter=tree:0 -c remote.origin.fetch=''+$($repository.Commitish):refs/remotes/origin/$($repository.Commitish)'' https://github.com/$($repository.Name) . } else { - Write-Host "git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) ." - git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) . + Retry git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) . } # Turn off git GC for sparse checkout. Note: The devops checkout task does this by default - Write-Host "git config gc.auto 0" - git config gc.auto 0 + Run git config gc.auto 0 - Write-Host "git sparse-checkout init" - git sparse-checkout init + Run git sparse-checkout init # Set non-cone mode otherwise path filters will not work in git >= 2.37.0 # See https://github.blog/2022-06-27-highlights-from-git-2-37/#tidbits - Write-Host "git sparse-checkout set --no-cone '/*' '!/*/' '/eng'" git sparse-checkout set --no-cone '/*' '!/*/' '/eng' } @@ -82,10 +104,8 @@ steps: $commitish = $repository.Commitish -replace '^refs/heads/', '' # use -- to prevent git from interpreting the commitish as a path - Write-Host "git -c advice.detachedHead=false checkout $commitish --" - # This will use the default branch if repo.Commitish is empty - git -c advice.detachedHead=false checkout $commitish -- + Retry git -c advice.detachedHead=false checkout $commitish -- } else { Write-Host "Skipping checkout as repo has already been initialized" }