diff --git a/images/windows/scripts/build/Install-ActionsCache.ps1 b/images/windows/scripts/build/Install-ActionsCache.ps1 index 95d5665ff..70f505884 100644 --- a/images/windows/scripts/build/Install-ActionsCache.ps1 +++ b/images/windows/scripts/build/Install-ActionsCache.ps1 @@ -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 diff --git a/images/windows/scripts/build/Install-AliyunCli.ps1 b/images/windows/scripts/build/Install-AliyunCli.ps1 index a3967fba1..29463fdd9 100644 --- a/images/windows/scripts/build/Install-AliyunCli.ps1 +++ b/images/windows/scripts/build/Install-AliyunCli.ps1 @@ -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 diff --git a/images/windows/scripts/build/Install-AndroidSDK.ps1 b/images/windows/scripts/build/Install-AndroidSDK.ps1 index 834e9f052..32eb14287 100644 --- a/images/windows/scripts/build/Install-AndroidSDK.ps1 +++ b/images/windows/scripts/build/Install-AndroidSDK.ps1 @@ -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 diff --git a/images/windows/scripts/build/Install-BizTalkBuildComponent.ps1 b/images/windows/scripts/build/Install-BizTalkBuildComponent.ps1 index ea48c2a33..8bfc6b13d 100644 --- a/images/windows/scripts/build/Install-BizTalkBuildComponent.ps1 +++ b/images/windows/scripts/build/Install-BizTalkBuildComponent.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-Chocolatey.ps1 b/images/windows/scripts/build/Install-Chocolatey.ps1 index afd481b8a..c3b2feffc 100644 --- a/images/windows/scripts/build/Install-Chocolatey.ps1 +++ b/images/windows/scripts/build/Install-Chocolatey.ps1 @@ -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 diff --git a/images/windows/scripts/build/Install-Chrome.ps1 b/images/windows/scripts/build/Install-Chrome.ps1 index e07920ffb..52cd2a5ea 100644 --- a/images/windows/scripts/build/Install-Chrome.ps1 +++ b/images/windows/scripts/build/Install-Chrome.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-CloudFoundryCli.ps1 b/images/windows/scripts/build/Install-CloudFoundryCli.ps1 index 8a806cd26..f989a63bb 100644 --- a/images/windows/scripts/build/Install-CloudFoundryCli.ps1 +++ b/images/windows/scripts/build/Install-CloudFoundryCli.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-CodeQLBundle.ps1 b/images/windows/scripts/build/Install-CodeQLBundle.ps1 index 381496a50..5a29fb7dd 100644 --- a/images/windows/scripts/build/Install-CodeQLBundle.ps1 +++ b/images/windows/scripts/build/Install-CodeQLBundle.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-Docker.ps1 b/images/windows/scripts/build/Install-Docker.ps1 index 5b2c9e734..fb6ad34b8 100644 --- a/images/windows/scripts/build/Install-Docker.ps1 +++ b/images/windows/scripts/build/Install-Docker.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-DockerCompose.ps1 b/images/windows/scripts/build/Install-DockerCompose.ps1 index 915aed715..ff982f32a 100644 --- a/images/windows/scripts/build/Install-DockerCompose.ps1 +++ b/images/windows/scripts/build/Install-DockerCompose.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-DockerWinCred.ps1 b/images/windows/scripts/build/Install-DockerWinCred.ps1 index a1d764bec..127d4e488 100644 --- a/images/windows/scripts/build/Install-DockerWinCred.ps1 +++ b/images/windows/scripts/build/Install-DockerWinCred.ps1 @@ -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 diff --git a/images/windows/scripts/build/Install-DotnetSDK.ps1 b/images/windows/scripts/build/Install-DotnetSDK.ps1 index f47fbe370..3eb9461bd 100644 --- a/images/windows/scripts/build/Install-DotnetSDK.ps1 +++ b/images/windows/scripts/build/Install-DotnetSDK.ps1 @@ -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) { diff --git a/images/windows/scripts/build/Install-EdgeDriver.ps1 b/images/windows/scripts/build/Install-EdgeDriver.ps1 index 5eacdf463..a4a4223c0 100644 --- a/images/windows/scripts/build/Install-EdgeDriver.ps1 +++ b/images/windows/scripts/build/Install-EdgeDriver.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-Firefox.ps1 b/images/windows/scripts/build/Install-Firefox.ps1 index d4fbe0bdf..66e6d443c 100644 --- a/images/windows/scripts/build/Install-Firefox.ps1 +++ b/images/windows/scripts/build/Install-Firefox.ps1 @@ -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 diff --git a/images/windows/scripts/build/Install-Haskell.ps1 b/images/windows/scripts/build/Install-Haskell.ps1 index 002644616..ba0219be5 100644 --- a/images/windows/scripts/build/Install-Haskell.ps1 +++ b/images/windows/scripts/build/Install-Haskell.ps1 @@ -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") diff --git a/images/windows/scripts/build/Install-IEWebDriver.ps1 b/images/windows/scripts/build/Install-IEWebDriver.ps1 index 79c0c6cdd..32dd00ccd 100644 --- a/images/windows/scripts/build/Install-IEWebDriver.ps1 +++ b/images/windows/scripts/build/Install-IEWebDriver.ps1 @@ -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)) { diff --git a/images/windows/scripts/build/Install-JavaTools.ps1 b/images/windows/scripts/build/Install-JavaTools.ps1 index 1b9e34d70..5769f38ff 100644 --- a/images/windows/scripts/build/Install-JavaTools.ps1 +++ b/images/windows/scripts/build/Install-JavaTools.ps1 @@ -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:\" diff --git a/images/windows/scripts/build/Install-Kotlin.ps1 b/images/windows/scripts/build/Install-Kotlin.ps1 index daeba3246..9d848abfa 100644 --- a/images/windows/scripts/build/Install-Kotlin.ps1 +++ b/images/windows/scripts/build/Install-Kotlin.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-KubernetesTools.ps1 b/images/windows/scripts/build/Install-KubernetesTools.ps1 index 5126ba52c..1e82244dc 100644 --- a/images/windows/scripts/build/Install-KubernetesTools.ps1 +++ b/images/windows/scripts/build/Install-KubernetesTools.ps1 @@ -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 diff --git a/images/windows/scripts/build/Install-Mingw64.ps1 b/images/windows/scripts/build/Install-Mingw64.ps1 index 27ec4f91b..cbb9b29d3 100644 --- a/images/windows/scripts/build/Install-Mingw64.ps1 +++ b/images/windows/scripts/build/Install-Mingw64.ps1 @@ -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 diff --git a/images/windows/scripts/build/Install-Msys2.ps1 b/images/windows/scripts/build/Install-Msys2.ps1 index 09aca9a12..09ba95a10 100644 --- a/images/windows/scripts/build/Install-Msys2.ps1 +++ b/images/windows/scripts/build/Install-Msys2.ps1 @@ -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 diff --git a/images/windows/scripts/build/Install-PyPy.ps1 b/images/windows/scripts/build/Install-PyPy.ps1 index 33a6c7b39..9fb92b718 100644 --- a/images/windows/scripts/build/Install-PyPy.ps1 +++ b/images/windows/scripts/build/Install-PyPy.ps1 @@ -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 diff --git a/images/windows/scripts/build/Install-Ruby.ps1 b/images/windows/scripts/build/Install-Ruby.ps1 index b3036cc07..dc49875fc 100644 --- a/images/windows/scripts/build/Install-Ruby.ps1 +++ b/images/windows/scripts/build/Install-Ruby.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-Runner.ps1 b/images/windows/scripts/build/Install-Runner.ps1 index 559875a11..e3f42cdfc 100644 --- a/images/windows/scripts/build/Install-Runner.ps1 +++ b/images/windows/scripts/build/Install-Runner.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-Rust.ps1 b/images/windows/scripts/build/Install-Rust.ps1 index 5778fd37a..032915af5 100644 --- a/images/windows/scripts/build/Install-Rust.ps1 +++ b/images/windows/scripts/build/Install-Rust.ps1 @@ -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 diff --git a/images/windows/scripts/build/Install-Selenium.ps1 b/images/windows/scripts/build/Install-Selenium.ps1 index dad6a4ac2..619cb8ffb 100644 --- a/images/windows/scripts/build/Install-Selenium.ps1 +++ b/images/windows/scripts/build/Install-Selenium.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-Stack.ps1 b/images/windows/scripts/build/Install-Stack.ps1 index 98c84ac07..85e1a1994 100644 --- a/images/windows/scripts/build/Install-Stack.ps1 +++ b/images/windows/scripts/build/Install-Stack.ps1 @@ -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 diff --git a/images/windows/scripts/build/Install-Toolset.ps1 b/images/windows/scripts/build/Install-Toolset.ps1 index e032464b6..19c1f8b4f 100644 --- a/images/windows/scripts/build/Install-Toolset.ps1 +++ b/images/windows/scripts/build/Install-Toolset.ps1 @@ -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")) { diff --git a/images/windows/scripts/build/Install-Zstd.ps1 b/images/windows/scripts/build/Install-Zstd.ps1 index 224f2cf5f..2e8ce0174 100644 --- a/images/windows/scripts/build/Install-Zstd.ps1 +++ b/images/windows/scripts/build/Install-Zstd.ps1 @@ -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 diff --git a/images/windows/scripts/helpers/ImageHelpers.psm1 b/images/windows/scripts/helpers/ImageHelpers.psm1 index c3bee64fa..fbc2ae041 100644 --- a/images/windows/scripts/helpers/ImageHelpers.psm1 +++ b/images/windows/scripts/helpers/ImageHelpers.psm1 @@ -16,7 +16,7 @@ Export-ModuleMember -Function @( 'Get-ToolsetContent' 'Get-TCToolVersionPath' 'Get-TCToolPath' - 'Start-DownloadWithRetry' + 'Invoke-DownloadWithRetry' 'Get-VsixExtenstionFromMarketplace' 'Install-VSIXFromFile' 'Install-VSIXFromUrl' diff --git a/images/windows/scripts/helpers/InstallHelpers.ps1 b/images/windows/scripts/helpers/InstallHelpers.ps1 index a658d0bd3..c6c05f5b1 100644 --- a/images/windows/scripts/helpers/InstallHelpers.ps1 +++ b/images/windows/scripts/helpers/InstallHelpers.ps1 @@ -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 } diff --git a/images/windows/scripts/helpers/VisualStudioHelpers.ps1 b/images/windows/scripts/helpers/VisualStudioHelpers.ps1 index fe613ceaa..33fd7ffbb 100644 --- a/images/windows/scripts/helpers/VisualStudioHelpers.ps1 +++ b/images/windows/scripts/helpers/VisualStudioHelpers.ps1 @@ -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"