mirror of
https://github.com/actions/runner-images-sangeeth.git
synced 2025-12-20 06:29:50 +00:00
[ubuntu] Refactor PowerShell build scripts (#9064)
* [ubuntu] Refactor PowerShell build scripts * Add Module import * Add Invoke-DownloadWithRetry function * Fix temp download dir * Update function to Add-GlobalEnvironmentVariable
This commit is contained in:
committed by
GitHub
parent
62aeae3a20
commit
e9057a22fc
@@ -24,7 +24,7 @@ function Get-CommandResult {
|
||||
This command runs "ls -la" in bash and returns the output and exit code as hashtable.
|
||||
|
||||
#>
|
||||
param (
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $Command,
|
||||
[int[]] $ExpectedExitCode = 0,
|
||||
@@ -76,3 +76,77 @@ function Get-ToolsetContent {
|
||||
$toolsetJson = Get-Content -Path $toolsetPath -Raw
|
||||
ConvertFrom-Json -InputObject $toolsetJson
|
||||
}
|
||||
|
||||
function Invoke-DownloadWithRetry {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Downloads a file from a given URL with retry functionality.
|
||||
|
||||
.DESCRIPTION
|
||||
The Invoke-DownloadWithRetry function downloads a file from the specified URL
|
||||
to the specified path. It includes retry functionality in case the download fails.
|
||||
|
||||
.PARAMETER Url
|
||||
The URL of the file to download.
|
||||
|
||||
.PARAMETER Path
|
||||
The path where the downloaded file will be saved. If not provided, a temporary path
|
||||
will be used.
|
||||
|
||||
.EXAMPLE
|
||||
Invoke-DownloadWithRetry -Url "https://example.com/file.zip" -Path "/usr/local/bin"
|
||||
Downloads the file from the specified URL and saves it to the specified path.
|
||||
|
||||
.EXAMPLE
|
||||
Invoke-DownloadWithRetry -Url "https://example.com/file.zip"
|
||||
Downloads the file from the specified URL and saves it to a temporary path.
|
||||
|
||||
.OUTPUTS
|
||||
The path where the downloaded file is saved.
|
||||
#>
|
||||
param(
|
||||
[Parameter(Mandatory)]
|
||||
[string] $Url,
|
||||
[Alias("Destination")]
|
||||
[string] $DestinationPath
|
||||
)
|
||||
|
||||
if (-not $DestinationPath) {
|
||||
$invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
|
||||
$re = "[{0}]" -f [RegEx]::Escape($invalidChars)
|
||||
$fileName = [IO.Path]::GetFileName($Url) -replace $re
|
||||
|
||||
if ([String]::IsNullOrEmpty($fileName)) {
|
||||
$fileName = [System.IO.Path]::GetRandomFileName()
|
||||
}
|
||||
$DestinationPath = Join-Path -Path "/tmp" -ChildPath $fileName
|
||||
}
|
||||
|
||||
Write-Host "Downloading package from $Url to $DestinationPath..."
|
||||
|
||||
$interval = 30
|
||||
$downloadStartTime = Get-Date
|
||||
for ($retries = 20; $retries -gt 0; $retries--) {
|
||||
try {
|
||||
$attemptStartTime = Get-Date
|
||||
(New-Object System.Net.WebClient).DownloadFile($Url, $DestinationPath)
|
||||
$attemptSeconds = [math]::Round(($(Get-Date) - $attemptStartTime).TotalSeconds, 2)
|
||||
Write-Host "Package downloaded in $attemptSeconds seconds"
|
||||
break
|
||||
} catch {
|
||||
$attemptSeconds = [math]::Round(($(Get-Date) - $attemptStartTime).TotalSeconds, 2)
|
||||
Write-Warning "Package download failed in $attemptSeconds seconds"
|
||||
Write-Warning $_.Exception.Message
|
||||
}
|
||||
|
||||
if ($retries -eq 0) {
|
||||
$totalSeconds = [math]::Round(($(Get-Date) - $downloadStartTime).TotalSeconds, 2)
|
||||
throw "Package download failed after $totalSeconds seconds"
|
||||
}
|
||||
|
||||
Write-Warning "Waiting $interval seconds before retrying (retries left: $retries)..."
|
||||
Start-Sleep -Seconds $interval
|
||||
}
|
||||
|
||||
return $DestinationPath
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user