[Windows] Rename and refactor downloading function (#8906)

This commit is contained in:
Vasilii Polikarpov
2023-11-29 13:02:29 +01:00
committed by GitHub
parent bfe32a2b12
commit 7dba8776df
32 changed files with 105 additions and 130 deletions

View File

@@ -15,7 +15,7 @@ $downloadUrl = Resolve-GithubReleaseAssetUrl `
-Asset "action-versions.zip"
Write-Host "Download Latest action-versions archive from $downloadUrl"
$actionVersionsArchivePath = Start-DownloadWithRetry -Url $downloadUrl -Name "action-versions.zip"
$actionVersionsArchivePath = Invoke-DownloadWithRetry $downloadUrl
Write-Host "Expand action-versions archive"
Expand-7ZipArchive -Path $actionVersionsArchivePath -DestinationPath $env:ACTIONS_RUNNER_ACTION_ARCHIVE_CACHE

View File

@@ -9,7 +9,7 @@ $repoUrl = "https://api.github.com/repos/aliyun/aliyun-cli/releases/latest"
$installerFileName = "aliyun-cli-windows"
$assets = (Invoke-RestMethod -Uri $repoUrl).assets
$downloadUrl = ($assets.browser_download_url -ilike "*aliyun-cli-windows-*-amd64.zip*") | Select-Object -First 1
$packagePath = Start-DownloadWithRetry -Url $downloadUrl -Name "$installerFileName.zip"
$packagePath = Invoke-DownloadWithRetry $downloadUrl
#region Supply chain security - Alibaba Cloud CLI
$fileHash = (Get-FileHash -Path $packagePath -Algorithm SHA256).Hash

View File

@@ -61,7 +61,7 @@ $androidToolset = (Get-ToolsetContent).android
# Newer version(s) require Java 11 by default
# See https://github.com/actions/runner-images/issues/6960
$cmdlineToolsUrl = $androidToolset.commandline_tools_url
$cmdlineToolsArchPath = Start-DownloadWithRetry -Url $cmdlineToolsUrl -Name "cmdline-tools.zip"
$cmdlineToolsArchPath = Invoke-DownloadWithRetry $cmdlineToolsUrl
#region Supply chain security
$localFileHash = (Get-FileHash -Path $cmdlineToolsArchPath -Algorithm SHA256).Hash

View File

@@ -7,13 +7,13 @@ $BuildComponentUri = "https://aka.ms/BuildComponentSetup.EN"
$BuildComponentSignatureThumbprint = "8740DF4ACB749640AD318E4BE842F72EC651AD80"
Write-Host "Downloading BizTalk Project Build Component archive..."
$setupZipFile = Start-DownloadWithRetry -Url $BuildComponentUri -Name "BuildComponentSetup.EN.zip"
$zipFile = Invoke-DownloadWithRetry $BuildComponentUri
$setupPath = Join-Path $env:TEMP "BizTalkBuildComponent"
if (-not (Test-Path -Path $setupPath)) {
$null = New-Item -Path $setupPath -ItemType Directory -Force
}
Expand-7ZipArchive -Path $setupZipFile -DestinationPath $setupPath
Expand-7ZipArchive -Path $zipFile -DestinationPath $setupPath
Write-Host "Installing BizTalk Project Build Component..."
Install-Binary `
@@ -25,7 +25,4 @@ Install-Binary `
-ExtraInstallArgs ("/l*v", "$setupPath\buildComponentSetup.log") `
-ExpectedSignature $BuildComponentSignatureThumbprint
Remove-Item $setupZipFile
Remove-Item $setupPath -Recurse -Force
Invoke-PesterTests -TestFile "BizTalk" -TestName "BizTalk Build Component Setup"

View File

@@ -24,7 +24,7 @@ if ($userPath) {
# Verify and run choco installer
$signatureThumbprint = "83AC7D88C66CB8680BCE802E0F0F5C179722764B"
$InstallScriptPath = Start-DownloadWithRetry -Url 'https://chocolatey.org/install.ps1'
$InstallScriptPath = Invoke-DownloadWithRetry 'https://chocolatey.org/install.ps1'
Test-FileSignature -FilePath $InstallScriptPath -ExpectedThumbprint $signatureThumbprint
Invoke-Expression $InstallScriptPath

View File

@@ -72,7 +72,7 @@ Write-Host "Chrome WebDriver version to install is $($ChromeDriverVersion.versio
$ChromeDriverZipDownloadUrl = ($ChromeDriverVersion.downloads.chromedriver | Where-Object platform -eq "win64").url
Write-Host "Download Chrome WebDriver from $ChromeDriverZipDownloadUrl..."
$ChromeDriverArchPath = Start-DownloadWithRetry -Url $ChromeDriverZipDownloadUrl
$ChromeDriverArchPath = Invoke-DownloadWithRetry $ChromeDriverZipDownloadUrl
Write-Host "Expand Chrome WebDriver archive (without using directory names)..."
Expand-7ZipArchive -Path $ChromeDriverArchPath -DestinationPath $ChromeDriverPath -ExtractMethod "e"

View File

@@ -4,10 +4,9 @@
################################################################################
# Download the latest cf cli exe
$CloudFoundryCliName = "cf-cli.zip"
$CloudFoundryCliUrl = "https://packages.cloudfoundry.org/stable?release=windows64-exe&source=github"
$CloudFoundryArchPath = Start-DownloadWithRetry -Url $CloudFoundryCliUrl -Name $CloudFoundryCliName
$CloudFoundryArchPath = Invoke-DownloadWithRetry $CloudFoundryCliUrl
# Create directory for cf cli
$CloudFoundryCliPath = "C:\cf-cli"

View File

@@ -11,7 +11,7 @@ $TagName = "codeql-bundle-v" + $CliVersion
Write-Host "Downloading CodeQL bundle $($CliVersion)..."
# Note that this is the all-platforms CodeQL bundle, to support scenarios where customers run
# different operating systems within containers.
$CodeQLBundlePath = Start-DownloadWithRetry -Url "https://github.com/github/codeql-action/releases/download/$($TagName)/codeql-bundle.tar.gz" -Name "codeql-bundle.tar.gz"
$CodeQLBundlePath = Invoke-DownloadWithRetry "https://github.com/github/codeql-action/releases/download/$($TagName)/codeql-bundle.tar.gz"
$DownloadDirectoryPath = (Get-Item $CodeQLBundlePath).Directory.FullName
$CodeQLToolcachePath = Join-Path $Env:AGENT_TOOLSDIRECTORY -ChildPath "CodeQL" | Join-Path -ChildPath $CliVersion | Join-Path -ChildPath "x64"

View File

@@ -21,14 +21,14 @@ if (-not $mobyRelease) {
$mobyReleaseUrl = $dockerceUrl + $mobyRelease
Write-Host "Install Moby $mobyRelease..."
$mobyArchivePath = Start-DownloadWithRetry -Url $mobyReleaseUrl -Name $mobyRelease
$mobyArchivePath = Invoke-DownloadWithRetry $mobyReleaseUrl
Expand-Archive -Path $mobyArchivePath -DestinationPath $env:TEMP
$dockerPath = "$env:TEMP\docker\docker.exe"
$dockerdPath = "$env:TEMP\docker\dockerd.exe"
Write-Host "Install Docker CE"
$instScriptUrl = "https://raw.githubusercontent.com/microsoft/Windows-Containers/Main/helpful_tools/Install-DockerCE/install-docker-ce.ps1"
$instScriptPath = Start-DownloadWithRetry -Url $instScriptUrl -Name "install-docker-ce.ps1"
$instScriptPath = Invoke-DownloadWithRetry $instScriptUrl
& $instScriptPath -DockerPath $dockerPath -DockerDPath $dockerdPath
if ($LastExitCode -ne 0) {
Write-Host "Docker installation failed with exit code $LastExitCode"

View File

@@ -12,6 +12,6 @@ Write-Host "Install-Package Docker-Compose v2"
$dockerComposev2Url = "https://github.com/docker/compose/releases/latest/download/docker-compose-windows-x86_64.exe"
$cliPluginsDir = "C:\ProgramData\docker\cli-plugins"
New-Item -Path $cliPluginsDir -ItemType Directory
Start-DownloadWithRetry -Url $dockerComposev2Url -Name docker-compose.exe -DownloadPath $cliPluginsDir
Invoke-DownloadWithRetry -Url $dockerComposev2Url -Path "$cliPluginsDir\docker-compose.exe"
Invoke-PesterTests -TestFile "Docker" -TestName "DockerCompose"

View File

@@ -19,7 +19,7 @@ function Get-DockerWincredHash {
Write-Host "Install docker-wincred"
$dockerCredLatestRelease = Invoke-RestMethod -Uri "https://api.github.com/repos/docker/docker-credential-helpers/releases/latest"
$dockerCredDownloadUrl = $dockerCredLatestRelease.assets.browser_download_url -match "docker-credential-wincred-.+\.exe" | Select-Object -First 1
Start-DownloadWithRetry -Url $dockerCredDownloadUrl -DownloadPath "C:\Windows\System32" -Name "docker-credential-wincred.exe"
Invoke-DownloadWithRetry -Url $dockerCredDownloadUrl -Path "C:\Windows\System32\docker-credential-wincred.exe"
#region Supply chain security
$distributor_file_hash = Get-DockerWincredHash -Release $dockerCredLatestRelease.name

View File

@@ -16,22 +16,21 @@
function Get-SDKVersionsToInstall (
$DotnetVersion
) {
$releaseJson = "https://dotnetcli.blob.core.windows.net/dotnet/release-metadata/${DotnetVersion}/releases.json"
$releasesJsonPath = Start-DownloadWithRetry -Url $releaseJson -Name "releases-${DotnetVersion}.json"
$currentReleases = Get-Content -Path $releasesJsonPath | ConvertFrom-Json
$metadataJsonUri = "https://dotnetcli.blob.core.windows.net/dotnet/release-metadata/${DotnetVersion}/releases.json"
$currentReleases = Invoke-DownloadWithRetry $metadataJsonUri | Get-Item | Get-Content | ConvertFrom-Json
# filtering out the preview/rc releases
$currentReleases = $currentReleases.'releases' | Where-Object { !$_.'release-version'.Contains('-') }
$sdks = @()
ForEach ($release in $currentReleases)
{
ForEach ($release in $currentReleases) {
$sdks += $release.'sdk'
$sdks += $release.'sdks'
}
return $sdks.version | Sort-Object { [Version] $_ } -Unique `
| Group-Object { $_.Substring(0, $_.LastIndexOf('.') + 2) } `
| Foreach-Object { $_.Group[-1] }
return $sdks.version `
| Sort-Object { [Version] $_ } -Unique `
| Group-Object { $_.Substring(0, $_.LastIndexOf('.') + 2) } `
| Foreach-Object { $_.Group[-1] }
}
function Invoke-Warmup (
@@ -55,23 +54,19 @@ function InstallSDKVersion (
$SdkVersion,
$dotnetVersion,
$Warmup
)
{
if (!(Test-Path -Path "C:\Program Files\dotnet\sdk\$sdkVersion"))
{
) {
if (!(Test-Path -Path "C:\Program Files\dotnet\sdk\$sdkVersion")) {
Write-Host "Installing dotnet $sdkVersion"
$ZipPath = [System.IO.Path]::combine([System.IO.Path]::GetTempPath(), [System.IO.Path]::GetRandomFileName())
.\dotnet-install.ps1 -Version $sdkVersion -InstallDir $(Join-Path -Path $env:ProgramFiles -ChildPath 'dotnet') -ZipPath $ZipPath -KeepZip
#region Supply chain security
$distributorFileHash = (Invoke-RestMethod -Uri "https://dotnetcli.blob.core.windows.net/dotnet/release-metadata/$dotnetVersion/releases.json").releases.sdks.Where({$_.version -eq $SdkVersion}).files.Where({ $_.name -eq 'dotnet-sdk-win-x64.zip'}).hash
$distributorFileHash = (Invoke-RestMethod -Uri "https://dotnetcli.blob.core.windows.net/dotnet/release-metadata/$dotnetVersion/releases.json").releases.sdks.Where({ $_.version -eq $SdkVersion }).files.Where({ $_.name -eq 'dotnet-sdk-win-x64.zip' }).hash
$localFileHash = (Get-FileHash -Path $ZipPath -Algorithm 'SHA512').Hash
Use-ChecksumComparison -LocalFileHash $localFileHash -DistributorFileHash $distributorFileHash
#endregion
}
else
{
} else {
Write-Host "Sdk version $sdkVersion already installed"
}
@@ -80,8 +75,7 @@ function InstallSDKVersion (
}
}
function InstallAllValidSdks()
{
function InstallAllValidSdks() {
# Consider all channels except preview/eol channels.
# Sort the channels in ascending order
$dotnetToolset = (Get-ToolsetContent).dotnet
@@ -91,7 +85,7 @@ function InstallAllValidSdks()
# Download installation script.
$installationName = "dotnet-install.ps1"
$installationUrl = "https://dot.net/v1/${installationName}"
Start-DownloadWithRetry -Url $installationUrl -Name $installationName -DownloadPath ".\"
Invoke-DownloadWithRetry -Url $installationUrl -Path ".\$installationName"
ForEach ($dotnetVersion in $dotnetVersions)
{

View File

@@ -6,43 +6,32 @@
# Disable Edge auto-updates
Rename-Item -Path "C:\Program Files (x86)\Microsoft\EdgeUpdate\MicrosoftEdgeUpdate.exe" -NewName "Disabled_MicrosoftEdgeUpdate.exe" -ErrorAction Stop
# Install Microsoft Edge WebDriver
Write-Host "Install Edge WebDriver..."
$EdgeDriverPath = "$($env:SystemDrive)\SeleniumWebDrivers\EdgeDriver"
if (-not (Test-Path -Path $EdgeDriverPath)) {
New-Item -Path $EdgeDriverPath -ItemType Directory -Force
Write-Host "Get the Microsoft Edge WebDriver version..."
$edgeBinaryPath = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe").'(default)'
[version] $edgeVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($edgeBinaryPath).ProductVersion
$edgeDriverPath = "$($env:SystemDrive)\SeleniumWebDrivers\EdgeDriver"
if (-not (Test-Path -Path $edgeDriverPath)) {
New-Item -Path $edgeDriverPath -ItemType Directory -Force
}
Write-Host "Get the Microsoft Edge WebDriver version..."
$RegistryPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths"
$EdgePath = (Get-ItemProperty "$RegistryPath\msedge.exe").'(default)'
[version]$EdgeVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($EdgePath).ProductVersion
$EdgeDriverVersionUrl = "https://msedgedriver.azureedge.net/LATEST_RELEASE_$($EdgeVersion.Major)_WINDOWS"
$EdgeDriverVersionFile = Start-DownloadWithRetry -Url $EdgeDriverVersionUrl -Name "versioninfo.txt" -DownloadPath $EdgeDriverPath
$versionInfoUrl = "https://msedgedriver.azureedge.net/LATEST_RELEASE_$($edgeVersion.Major)_WINDOWS"
$versionInfoFile = Invoke-DownloadWithRetry -Url $versionInfoUrl -Path "$edgeDriverPath\versioninfo.txt"
$latestVersion = Get-Content -Path $versionInfoFile
Write-Host "Download Microsoft Edge WebDriver..."
$EdgeDriverLatestVersion = Get-Content -Path $EdgeDriverVersionFile
$EdgeDriverArchName = "edgedriver_win64.zip"
$EdgeDriverDownloadUrl = "https://msedgedriver.azureedge.net/${EdgeDriverLatestVersion}/${EdgeDriverArchName}"
$EdgeDriverArchPath = Start-DownloadWithRetry -Url $EdgeDriverDownloadUrl -Name $EdgeDriverArchName
$downloadUrl = "https://msedgedriver.azureedge.net/$latestVersion/edgedriver_win64.zip"
$archivePath = Invoke-DownloadWithRetry $downloadUrl
Write-Host "Expand Microsoft Edge WebDriver archive..."
Expand-7ZipArchive -Path $EdgeDriverArchPath -DestinationPath $EdgeDriverPath
Expand-7ZipArchive -Path $archivePath -DestinationPath $edgeDriverPath
#Validate the EdgeDriver signature
$EdgeDriverSignatureThumbprint = "CB9C4FBEA1D87D2D468AC5A9CAAB0163F6AD8401"
Test-FileSignature -FilePath "$EdgeDriverPath\msedgedriver.exe" -ExpectedThumbprint $EdgeDriverSignatureThumbprint
$signatureThumbprint = "CB9C4FBEA1D87D2D468AC5A9CAAB0163F6AD8401"
Test-FileSignature -FilePath "$edgeDriverPath\msedgedriver.exe" -ExpectedThumbprint $signatureThumbprint
Write-Host "Setting the environment variables..."
[Environment]::SetEnvironmentVariable("EdgeWebDriver", $EdgeDriverPath, "Machine")
$regEnvKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\'
$PathValue = Get-ItemPropertyValue -Path $regEnvKey -Name 'Path'
$PathValue += ";$EdgeDriverPath\"
Set-ItemProperty -Path $regEnvKey -Name 'Path' -Value $PathValue
Add-MachinePathItem "$edgeDriverPath\"
Invoke-PesterTests -TestFile "Browsers" -TestName "Edge"

View File

@@ -42,10 +42,8 @@ $GeckoDriverVersion = $GeckoDriverJson.tag_name
$GeckoDriverVersion.Substring(1) | Out-File -FilePath "$GeckoDriverPath\versioninfo.txt" -Force;
Write-Host "Download Gecko WebDriver WebDriver..."
$GeckoDriverArchName = $GeckoDriverWindowsAsset.name
$GeckoDriverDownloadUrl = $GeckoDriverWindowsAsset.browser_download_url
$GeckoDriverArchPath = Start-DownloadWithRetry -Url $GeckoDriverDownloadUrl -Name $GeckoDriverArchName
$GeckoDriverArchPath = Invoke-DownloadWithRetry $GeckoDriverDownloadUrl
Write-Host "Expand Gecko WebDriver archive..."
Expand-7ZipArchive -Path $GeckoDriverArchPath -DestinationPath $GeckoDriverPath

View File

@@ -20,7 +20,7 @@ $ghcupDownloadURL = "https://downloads.haskell.org/~ghcup/x86_64-mingw64-ghcup.e
New-Item -Path "$ghcupPrefix\ghcup" -ItemType 'directory' -ErrorAction SilentlyContinue | Out-Null
New-Item -Path "$ghcupPrefix\ghcup\bin" -ItemType 'directory' -ErrorAction SilentlyContinue | Out-Null
Start-DownloadWithRetry -Url $ghcupDownloadURL -Name "ghcup.exe" -DownloadPath "$ghcupPrefix\ghcup\bin"
Invoke-DownloadWithRetry -Url $ghcupDownloadURL -Path "$ghcupPrefix\ghcup\bin\ghcup.exe"
[System.Environment]::SetEnvironmentVariable("GHCUP_INSTALL_BASE_PREFIX", $ghcupPrefix, "Machine")
[System.Environment]::SetEnvironmentVariable("GHCUP_MSYS2", $msysPath, "Machine")

View File

@@ -10,13 +10,8 @@ $ieDriverUrl = Resolve-GithubReleaseAssetUrl `
-Asset "IEDriverServer_x64_*.zip"
# Download IE selenium driver
try {
Write-Host "Selenium IEDriverServer download and install..."
$driverZipFile = Start-DownloadWithRetry -Url $ieDriverUrl -Name "SeleniumWebDrivers.zip"
} catch {
Write-Error "[!] Failed to download $ieDriverUrl"
exit 1
}
Write-Host "Selenium IEDriverServer download and install..."
$driverZipFile = Invoke-DownloadWithRetry $ieDriverUrl
$ieDriverPath = "C:\SeleniumWebDrivers\IEDriver"
if (-not (Test-Path -Path $ieDriverPath)) {

View File

@@ -63,7 +63,7 @@ function Install-JavaJDK {
# Download and extract java binaries to temporary folder
$downloadUrl = $asset.binary.package.link
$archivePath = Start-DownloadWithRetry -Url $downloadUrl -Name $([IO.Path]::GetFileName($downloadUrl))
$archivePath = Invoke-DownloadWithRetry $downloadUrl
#region Supply chain security - JDK
$fileHash = (Get-FileHash -Path $archivePath -Algorithm SHA256).Hash
@@ -137,7 +137,7 @@ $uri = 'https://repo1.maven.org/maven2/net/sourceforge/cobertura/cobertura/2.1.1
$sha256sum = '79479DDE416B082F38ECD1F2F7C6DEBD4D0C2249AF80FD046D1CE05D628F2EC6'
$coberturaPath = "C:\cobertura-2.1.1"
$archivePath = Start-DownloadWithRetry -Url $uri -Name "cobertura.zip"
$archivePath = Invoke-DownloadWithRetry $uri
$fileHash = (Get-FileHash -Path $archivePath -Algorithm SHA256).Hash
Use-ChecksumComparison $fileHash $sha256sum
Expand-7ZipArchive -Path $archivePath -DestinationPath "C:\"

View File

@@ -11,17 +11,17 @@ $kotlinDownloadUrl = Resolve-GithubReleaseAssetUrl `
-Repo "JetBrains/kotlin" `
-Version $kotlinVersion `
-Asset "kotlin-compiler-*.zip"
$kotlinInstallerPath = Start-DownloadWithRetry -Url $kotlinDownloadUrl -Name "kotlin-compiler.zip"
$kotlinArchivePath = Invoke-DownloadWithRetry $kotlinDownloadUrl
#region Supply chain security
$fileHash = (Get-FileHash -Path $kotlinInstallerPath -Algorithm SHA256).Hash
$fileHash = (Get-FileHash -Path $kotlinArchivePath -Algorithm SHA256).Hash
$externalHash = Get-HashFromGitHubReleaseBody -RepoOwner "JetBrains" -RepoName "kotlin" -FileName "$kotlinBinaryName-*.zip" -Version $kotlinVersion -WordNumber 2
Use-ChecksumComparison $fileHash $externalHash
#endregion
Write-Host "Expand Kotlin archive"
$kotlinPath = "C:\tools"
Expand-7ZipArchive -Path $kotlinInstallerPath -DestinationPath $kotlinPath
Expand-7ZipArchive -Path $kotlinArchivePath -DestinationPath $kotlinPath
# Add to PATH
Add-MachinePathItem "$kotlinPath\kotlinc\bin"

View File

@@ -11,7 +11,7 @@ $assets = (Invoke-RestMethod -Uri $repoUrl).assets
[System.String] $kindDownloadLink = $assets.browser_download_url -match "kind-windows-amd64$"
$destFilePath = "C:\ProgramData\kind"
$null = New-Item -Path $destFilePath -ItemType Directory -Force
$packagePath = Start-DownloadWithRetry -Url $kindDownloadLink -Name "kind.exe" -DownloadPath $destFilePath
$packagePath = Invoke-DownloadWithRetry -Url $kindDownloadLink -Path "$destFilePath\kind.exe"
#region Supply chain security - Kind
$fileHash = (Get-FileHash -Path $packagePath -Algorithm SHA256).Hash

View File

@@ -18,7 +18,7 @@ if (Test-IsWin19) {
throw "Unknown architecture $_"
}
$packagePath = Start-DownloadWithRetry -Url $url -Name "$_.7z"
$packagePath = Invoke-DownloadWithRetry $url
$hash = Get-FileHash -Path $packagePath -Algorithm SHA256
if ($hash.Hash -ne $sha256sum) {
throw "Checksum verification failed for $packagePath"
@@ -55,7 +55,7 @@ if (Test-IsWin19) {
-Version "$version" `
-Asset "$arch-*-release-$threads-$exceptions-$runtime-*.7z"
$packagePath = Start-DownloadWithRetry -Url $url -Name "$_.7z"
$packagePath = Invoke-DownloadWithRetry $url
Expand-7ZipArchive -Path $packagePath -DestinationPath "C:\"
# Make a copy of mingw-make.exe to make.exe, which is a more discoverable name

View File

@@ -17,7 +17,7 @@ function Install-Msys2 {
# Download the latest msys2 x86_64, filename includes release date
Write-Host "Starting msys2 download using $($msys2Uri.split('/')[-1])"
$msys2File = Start-DownloadWithRetry -Url $msys2Uri
$msys2File = Invoke-DownloadWithRetry $msys2Uri
Write-Host "Finished download"
#region Supply chain security - Kind

View File

@@ -99,7 +99,7 @@ foreach($toolsetVersion in $toolsetVersions.versions)
{
$filename = $latestMajorPyPyVersion.filename
Write-Host "Found PyPy '$filename' package"
$tempPyPyPackagePath = Start-DownloadWithRetry -Url $latestMajorPyPyVersion.download_url -Name $filename
$tempPyPyPackagePath = Invoke-DownloadWithRetry $latestMajorPyPyVersion.download_url
#region Supply chain security
$localFileHash = (Get-FileHash -Path $tempPyPyPackagePath -Algorithm SHA256).Hash

View File

@@ -104,7 +104,7 @@ foreach ($rubyVersion in $rubyToolVersions) {
# Get url for the latest major Ruby version
$url = $rubyLatestMajorVersions[$rubyVersion]
if ($url) {
$tempRubyPackagePath = Start-DownloadWithRetry -Url $url
$tempRubyPackagePath = Invoke-DownloadWithRetry $url
Install-Ruby -PackagePath $tempRubyPackagePath
} else {
Write-Host "Url not found for the '$rubyVersion' version"

View File

@@ -8,7 +8,8 @@ Write-Host "Download latest Runner for GitHub Actions"
$release = Invoke-RestMethod -Uri "https://api.github.com/repos/actions/runner/releases/latest"
$version = $release.tag_name.Trim("v")
$downloadUrl = ($release.assets.browser_download_url -ilike "*actions-runner-win-x64-${version}.zip*") | Select-Object -First 1
$fileName = [System.IO.Path]::GetFileName($downloadUrl)
New-Item -Path "C:\ProgramData\runner" -ItemType Directory
$packagePath = Start-DownloadWithRetry -Url $downloadUrl -DownloadPath "C:\ProgramData\runner"
Invoke-DownloadWithRetry -Url $downloadUrl -Path "C:\ProgramData\runner\$fileName"
Invoke-PesterTests -TestFile "RunnerCache"

View File

@@ -10,7 +10,7 @@ $env:CARGO_HOME = "C:\Users\Default\.cargo"
# Download the latest rustup-init.exe for Windows x64
# See https://rustup.rs/#
$rustupPath = Start-DownloadWithRetry -Url "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe" -Name "rustup-init.exe"
$rustupPath = Invoke-DownloadWithRetry "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe"
#region Supply chain security
$localFileHash = (Get-FileHash -Path (Join-Path ${env:TEMP} 'rustup-init.exe') -Algorithm SHA256).Hash

View File

@@ -9,21 +9,20 @@ New-Item -ItemType directory -Path $seleniumDirectory
# Download Selenium
$seleniumMajorVersion = (Get-ToolsetContent).selenium.version
$seleniumFileName = "selenium-server.jar"
$seleniumDownloadUrl = Resolve-GithubReleaseAssetUrl `
-Repo "SeleniumHQ/selenium" `
-Version "$seleniumMajorVersion.*" `
-Asset "selenium-server-*.jar"
Start-DownloadWithRetry -Url $seleniumDownloadUrl -Name $seleniumFileName -DownloadPath $seleniumDirectory
$seleniumBinPath = Join-Path $seleniumDirectory "selenium-server.jar"
Invoke-DownloadWithRetry -Url $seleniumDownloadUrl -Path $seleniumBinPath
# Create an empty file to retrive Selenium version
$seleniumFullVersion = $seleniumDownloadUrl.Split("-")[1].Split("/")[0]
New-Item -Path $seleniumDirectory -Name "selenium-server-$seleniumFullVersion"
# Add SELENIUM_JAR_PATH environment variable
$seleniumBinPath = Join-Path $seleniumDirectory $seleniumFileName
[Environment]::SetEnvironmentVariable("SELENIUM_JAR_PATH", $seleniumBinPath, "Machine")
Invoke-PesterTests -TestFile "Browsers" -TestName "Selenium"

View File

@@ -13,7 +13,7 @@ $DownloadUrl = $StackReleasesJson.assets | Where-Object { $_.name.EndsWith($Down
Write-Host "Download stack archive"
$StackToolcachePath = Join-Path $Env:AGENT_TOOLSDIRECTORY "stack\$Version"
$DestinationPath = Join-Path $StackToolcachePath "x64"
$StackArchivePath = Start-DownloadWithRetry -Url $DownloadUrl
$StackArchivePath = Invoke-DownloadWithRetry $DownloadUrl
#region Supply chain security - Stack
$fileHash = (Get-FileHash -Path $StackArchivePath -Algorithm SHA256).Hash

View File

@@ -14,7 +14,7 @@ Function Install-Asset {
$releaseAssetName = [System.IO.Path]::GetFileNameWithoutExtension($ReleaseAsset.filename)
$assetFolderPath = Join-Path $env:TEMP $releaseAssetName
$assetArchivePath = Start-DownloadWithRetry -Url $ReleaseAsset.download_url -Name $ReleaseAsset.filename
$assetArchivePath = Invoke-DownloadWithRetry $ReleaseAsset.download_url
Write-Host "Extract $($ReleaseAsset.filename) content..."
if ($assetArchivePath.EndsWith(".tar.gz")) {

View File

@@ -6,7 +6,7 @@
$url = "https://api.github.com/repos/facebook/zstd/releases/latest"
# Explicitly set type to string since match returns array by default
[System.String] $zstdLatest = (Invoke-RestMethod -Uri $url).assets.browser_download_url -match "zstd-.+-win64.zip$"
$zstdArchivePath = Start-DownloadWithRetry -Url $zstdLatest -Name "zstd-win64.zip"
$zstdArchivePath = Invoke-DownloadWithRetry $zstdLatest
$toolPath = "C:\tools"
$zstdPath = Join-Path $toolPath zstd

View File

@@ -16,7 +16,7 @@ Export-ModuleMember -Function @(
'Get-ToolsetContent'
'Get-TCToolVersionPath'
'Get-TCToolPath'
'Start-DownloadWithRetry'
'Invoke-DownloadWithRetry'
'Get-VsixExtenstionFromMarketplace'
'Install-VSIXFromFile'
'Install-VSIXFromUrl'

View File

@@ -66,9 +66,11 @@ function Install-Binary {
if ($Type -ne "MSI" -and $Type -ne "EXE") {
throw "Cannot determine the file type from the URL. Please specify the Type parameter."
}
$fileName = [System.IO.Path]::GetFileName($Url)
} else {
$fileName = [System.IO.Path]::GetFileNameWithoutExtension([System.IO.Path]::GetRandomFileName()) + ".$Type".ToLower()
}
$fileName = [System.IO.Path]::GetFileNameWithoutExtension([System.IO.Path]::GetRandomFileName()) + ".$Type".ToLower()
$filePath = Start-DownloadWithRetry -Url $Url -Name $fileName
$filePath = Invoke-DownloadWithRetry -Url $Url -Path "${env:Temp}\$fileName"
}
if ($PSBoundParameters.ContainsKey('ExpectedSignature')) {
@@ -130,49 +132,53 @@ function Install-Binary {
}
}
function Start-DownloadWithRetry {
function Invoke-DownloadWithRetry {
Param
(
[Parameter(Mandatory)]
[string] $Url,
[string] $Name,
[string] $DownloadPath = "${env:Temp}",
[int] $Retries = 20
[Alias("Destination")]
[string] $Path
)
if ([String]::IsNullOrEmpty($Name)) {
$Name = [IO.Path]::GetFileName($Url)
if (-not $Path) {
$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()
}
$Path = Join-Path -Path "${env:Temp}" -ChildPath $fileName
}
$filePath = Join-Path -Path $DownloadPath -ChildPath $Name
$downloadStartTime = Get-Date
Write-Host "Downloading package from $Url to $Path..."
# Default retry logic for the package.
Write-Host "Downloading package from: $Url to path $filePath."
while ($Retries -gt 0) {
$interval = 30
$downloadStartTime = Get-Date
for ($retries = 20; $retries -gt 0; $retries--) {
try {
$downloadAttemptStartTime = Get-Date
(New-Object System.Net.WebClient).DownloadFile($Url, $filePath)
$attemptStartTime = Get-Date
(New-Object System.Net.WebClient).DownloadFile($Url, $Path)
$attemptSeconds = [math]::Round(($(Get-Date) - $attemptStartTime).TotalSeconds, 2)
Write-Host "Package downloaded in $attemptSeconds seconds"
break
} catch {
$failTime = [math]::Round(($(Get-Date) - $downloadStartTime).TotalSeconds, 2)
$attemptTime = [math]::Round(($(Get-Date) - $downloadAttemptStartTime).TotalSeconds, 2)
Write-Host "There is an error encounterd after $attemptTime seconds during package downloading:`n$($_.Exception.ToString())"
$Retries--
if ($Retries -eq 0) {
Write-Host "Package download failed after $failTime seconds"
exit 1
}
Write-Host "Waiting 30 seconds before retrying. Retries left: $Retries"
Start-Sleep -Seconds 30
$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
}
$downloadCompleteTime = [math]::Round(($(Get-Date) - $downloadStartTime).TotalSeconds, 2)
Write-Host "Package downloaded successfully in $downloadCompleteTime seconds"
return $filePath
return $Path
}
function Get-VsixExtenstionFromMarketplace {
@@ -273,8 +279,7 @@ function Install-VSIXFromUrl {
[int] $Retries = 20
)
$name = [System.IO.Path]::GetFileNameWithoutExtension([System.IO.Path]::GetRandomFileName()) + ".vsix"
$filePath = Start-DownloadWithRetry -Url $Url -Name $Name
$filePath = Invoke-DownloadWithRetry $Url
Install-VSIXFromFile -FilePath $filePath -Retries $Retries
Remove-Item -Force -Confirm:$false $filePath
}

View File

@@ -38,8 +38,7 @@ Function Install-VisualStudio {
$productId = "Microsoft.VisualStudio.Product.${Edition}"
Write-Host "Downloading Bootstrapper ..."
$BootstrapperName = [IO.Path]::GetFileName($BootstrapperUrl)
$bootstrapperFilePath = Start-DownloadWithRetry -Url $BootstrapperUrl -Name $BootstrapperName
$bootstrapperFilePath = Invoke-DownloadWithRetry $BootstrapperUrl
# Verify that the bootstrapper is signed by Microsoft
Test-FileSignature -FilePath $bootstrapperFilePath -ExpectedThumbprint $SignatureThumbprint
@@ -85,8 +84,7 @@ Function Install-VisualStudio {
# Try to download tool to collect logs
$collectExeUrl = "https://aka.ms/vscollect.exe"
$collectExeName = [IO.Path]::GetFileName($collectExeUrl)
$collectExePath = Start-DownloadWithRetry -Url $collectExeUrl -Name $collectExeName
$collectExePath = Invoke-DownloadWithRetry -Url $collectExeUrl
# Collect installation logs using the collect.exe tool and check if it is successful
& "$collectExePath"