From 5e78326f300eeb3b4a7f088f10eeb51d079efffe Mon Sep 17 00:00:00 2001 From: Matt Thalman Date: Mon, 29 Aug 2022 14:04:50 -0500 Subject: [PATCH] Convert get-drop-versions scripts to PowerShell (#4043) (cherry picked from commit 364628c6700a2832b8efd4a38282c46b42527ff4) --- eng/Get-DropVersions.ps1 | 129 ++++++++++++++++++ eng/Get-MonitorDropVersions.ps1 | 18 +++ eng/get-drop-versions-monitor.sh | 17 --- eng/get-drop-versions.sh | 90 ------------ .../steps/update-dotnet-dependencies.yml | 13 +- eng/pipelines/update-dependencies.yml | 2 +- 6 files changed, 160 insertions(+), 109 deletions(-) create mode 100644 eng/Get-DropVersions.ps1 create mode 100644 eng/Get-MonitorDropVersions.ps1 delete mode 100755 eng/get-drop-versions-monitor.sh delete mode 100755 eng/get-drop-versions.sh diff --git a/eng/Get-DropVersions.ps1 b/eng/Get-DropVersions.ps1 new file mode 100644 index 000000000..cffd5c69c --- /dev/null +++ b/eng/Get-DropVersions.ps1 @@ -0,0 +1,129 @@ +#!/usr/bin/env pwsh + +<# +.SYNOPSIS +Returns the various component versions of the latest .NET build. +#> +[cmdletbinding()] +param( + # The release channel to use for determining the latest .NET build. + [Parameter(Mandatory)] + [string] + $Channel, + + # Whether to target an internal .NET build + [switch] + $UseInternalBuild, + + # SAS query string used to access the internal blob storage location of the build + [string] + $BlobStorageSasQueryString, + + # PAT used to access the versions repo in AzDO + [string] + $AzdoVersionsRepoInfoAccessToken +) + +function GetSdkInfo() { + if ($UseInternalBuild) { + $Channel = "internal/$Channel" + $queryString = "$BlobStorageSasQueryString" + } + else { + $queryString = "" + } + + # Download the SDK + $sdkFile = "dotnet-sdk-win-x64.zip" + $akaMsUrl = "https://aka.ms/dotnet/$Channel/$sdkFile$queryString" + Write-Host "Downloading SDK from $akaMsUrl" + $sdkOutPath = "$tempDir/$sdkFile" + $response = Invoke-WebRequest -Uri $akaMsUrl -OutFile $sdkOutPath -PassThru + $sdkUrl = $response.BaseResponse.RequestMessage.RequestUri.AbsoluteUri + Write-Host "Resolved SDK URL: $sdkUrl" + + # Extract .version file from SDK zip + $zipFile = [IO.Compression.ZipFile]::OpenRead("$tempDir/$sdkFile") + try { + $zipFile.Entries | Where-Object { $_.FullName -like "sdk/*/.version" } | ForEach-Object { + [IO.Compression.ZipFileExtensions]::ExtractToFile($_, "$tempDir/$($_.Name)") + } + } + finally { + $zipFile.Dispose() + } + + # Get the commit SHA from the .version file + $commitSha = $(Get-Content "$tempDir/.version")[0].Trim() + + # Resolve the SDK build version from the SDK URL + # Example URL: https://dotnetcli.azureedge.net/dotnet/Sdk/6.0.100-rtm.21522.1/dotnet-sdk-6.0.100-win-x64.zip + # The command below extracts the 6.0.100-rtm.21522.1 from the URL + $sdkUrlPath = "/Sdk/" + $versionUrlPath = $sdkUrl.Substring($sdkUrl.IndexOf($sdkUrlPath) + $sdkUrlPath.Length) + $sdkVersion = $versionUrlPath.Substring(0, $versionUrlPath.IndexOf("/")) + + return @{ + SdkVersion = $sdkVersion; + CommitSha = $commitSha; + } +} + +function GetVersionDetails([string]$commitSha) { + $versionDetailsPath="eng/Version.Details.xml" + + if ($UseInternalBuild) { + $dotnetInstallerRepoId="c20f712b-f093-40de-9013-d6b084c1ff30" + $versionDetailsUrl="https://dev.azure.com/dnceng/internal/_apis/git/repositories/$dotnetInstallerRepoId/items?scopePath=/$versionDetailsPath&api-version=6.0&version=$commitSha&versionType=commit" + $base64AccessToken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$AzdoVersionsRepoInfoAccessToken")) + $headers = @{ + "Authorization" = "Basic $base64AccessToken" + } + } + else { + $versionDetailsUrl="https://raw.githubusercontent.com/dotnet/installer/$commitSha/$versionDetailsPath" + $headers = @{} + } + + Write-Host "Downloading version details from $versionDetailsUrl" + $versionDetails = [xml](Invoke-RestMethod -Uri $versionDetailsUrl -Headers $headers) + return $versionDetails +} + +function GetDependencyVersion([string]$dependencyName, [xml]$versionDetails) { + $result = Select-Xml -XPath "//ProductDependencies/Dependency[starts-with(@Name,'$dependencyName')]/@Version" -Xml $versionDetails + return $result.Node.Value +} + +$ErrorActionPreference = 'Stop' +$ProgressPreference = 'SilentlyContinue' +Set-StrictMode -Version 2.0 + +$tempDir = "$([System.IO.Path]::GetTempPath())/dotnet-docker-get-dropversions" +New-Item -Path $tempDir -ItemType Directory -Force | Out-Null + +try { + $sdkInfo = GetSdkInfo + $versionDetails = GetVersionDetails $sdkInfo.CommitSha + + $runtimeVersion = GetDependencyVersion "VS.Redist.Common.NetCore.SharedFramework.x64" $versionDetails + + $aspnetVersion = GetDependencyVersion "VS.Redist.Common.AspNetCore.SharedFramework.x64" $versionDetails + + if (-not $runtimeVersion) { + Write-Error "Unable to resolve the runtime version" + exit 1 + } + + if (-not $aspnetVersion) { + Write-Error "Unable to resolve the ASP.NET Core runtime version" + exit 1 + } + + Write-Output "##vso[task.setvariable variable=sdkVer]$($sdkInfo.SdkVersion)" + Write-Output "##vso[task.setvariable variable=runtimeVer]$runtimeVersion" + Write-Output "##vso[task.setvariable variable=aspnetVer]$aspnetVersion" +} +finally { + Remove-Item $tempDir -Force -Recurse -ErrorAction Ignore +} diff --git a/eng/Get-MonitorDropVersions.ps1 b/eng/Get-MonitorDropVersions.ps1 new file mode 100644 index 000000000..ca7c87a1b --- /dev/null +++ b/eng/Get-MonitorDropVersions.ps1 @@ -0,0 +1,18 @@ +#!/usr/bin/env pwsh + +<# +.SYNOPSIS +Returns the various component versions of the latest .NET Monitor build. +#> +[cmdletbinding()] +param( + # The release channel to use for determining the latest .NET Monitor build. + [Parameter(Mandatory)] + [string] + $Channel +) + +$url = "https://aka.ms/dotnet/diagnostics/monitor$Channel/dotnet-monitor.nupkg.buildversion" +$monitorVersion = $(Invoke-RestMethod $url).Trim() + +Write-Output "##vso[task.setvariable variable=monitorVer]$monitorVersion" diff --git a/eng/get-drop-versions-monitor.sh b/eng/get-drop-versions-monitor.sh deleted file mode 100755 index 92068e2b5..000000000 --- a/eng/get-drop-versions-monitor.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env bash - -# Stop script on NZEC -set -e -# Stop script if unbound variable found (use ${var:-} if intentional) -set -u - -channel=$1 - -curl -fSLo dotnet-monitor.nupkg.buildversion https://aka.ms/dotnet/diagnostics/monitor$channel/dotnet-monitor.nupkg.buildversion - -# Read version file and remove newlines -monitorVer=$(tr -d '\r\n' < dotnet-monitor.nupkg.buildversion) - -rm dotnet-monitor.nupkg.buildversion - -echo "##vso[task.setvariable variable=monitorVer]$monitorVer" diff --git a/eng/get-drop-versions.sh b/eng/get-drop-versions.sh deleted file mode 100755 index 126dbaf07..000000000 --- a/eng/get-drop-versions.sh +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/env bash - -# Stop script on NZEC -set -e -# Stop script if unbound variable found (use ${var:-} if intentional) -set -u - -channel=$1 -useInternalBuild=$2 -blobStorageSasQueryString=$3 -azdoVersionsRepoInfoAccessToken=$4 - -sudo apt-get update && \ - sudo apt-get install -y --no-install-recommends libxml2-utils - -if [ "$useInternalBuild" = "true" ]; then - channel="internal/$channel" - queryString="$blobStorageSasQueryString" -else - queryString="" -fi - -akaMsUrl="https://aka.ms/dotnet/$channel/dotnet-sdk-win-x64.zip$queryString" - -echo "Downloading SDK from $akaMsUrl" - -# Download the SDK and resolve the redirected URL -sdkUrl=$(curl -w %{url_effective} -sSLo sdk.zip $akaMsUrl) - -unzip -p sdk.zip "sdk/*/.version" > sdkversion - -commitSha=$(cat sdkversion | head -1) -commitSha=${commitSha%?} # Remove last character (newline) - -# Resolve the SDK build version from the SDK URL -# Example URL: https://dotnetcli.azureedge.net/dotnet/Sdk/6.0.100-rtm.21522.1/dotnet-sdk-6.0.100-win-x64.zip -# The sed command below extracts the 6.0.100-rtm.21522.1 from the URL -# We don't want the version contained in the SDK's .version file because that may be a stable branding version -# The sed command doesn't support non-greedy matching so we have to be careful with the trailing slash after -# the version because there may be more than one slash if a SAS token is present. To handle this, we just look -# for the beginning of the filename with starts with "dotnet". -sdkVer=$(echo $sdkUrl | sed 's|.*/Sdk/\(.*\)/dotnet.*|\1|g') - -rm sdkversion - -if [ -z "$sdkVer" ]; then - echo "Unable to resolve the SDK version" >&2 - exit 1 -fi - -versionDetailsPath="eng/Version.Details.xml" -if [ "$useInternalBuild" = "true" ]; then - dotnetInstallerRepoId="c20f712b-f093-40de-9013-d6b084c1ff30" - versionDetailsUrl="https://dev.azure.com/dnceng/internal/_apis/git/repositories/$dotnetInstallerRepoId/items?scopePath=/$versionDetailsPath&api-version=6.0&version=$commitSha&versionType=commit" - requestUserArgs="-u :$azdoVersionsRepoInfoAccessToken" -else - versionDetailsUrl="https://raw.githubusercontent.com/dotnet/installer/$commitSha/$versionDetailsPath" - requestUserArgs="" -fi - -curl $requestUserArgs -fSLo versionDetails.xml $versionDetailsUrl - -getDependencyVersion() { - local dependencyName=$1 - xmllint --xpath "string(//ProductDependencies/Dependency[starts-with(@Name,'$dependencyName')]/@Version)" versionDetails.xml -} - -runtimeVer=$(getDependencyVersion "VS.Redist.Common.NetCore.SharedFramework.x64") -if [ -z "$runtimeVer" ]; then - runtimeVer=$(getDependencyVersion "Microsoft.NETCore.App.Internal") -fi - -aspnetVer=$(getDependencyVersion "VS.Redist.Common.AspNetCore.SharedFramework.x64") - -rm sdk.zip -rm versionDetails.xml - -if [ -z "$runtimeVer" ]; then - echo "Unable to resolve the runtime version" >&2 - exit 1 -fi - -if [ -z "$aspnetVer" ]; then - echo "Unable to resolve the ASP.NET Core runtime version" >&2 - exit 1 -fi - -echo "##vso[task.setvariable variable=sdkVer]$sdkVer" -echo "##vso[task.setvariable variable=runtimeVer]$runtimeVer" -echo "##vso[task.setvariable variable=aspnetVer]$aspnetVer" diff --git a/eng/pipelines/steps/update-dotnet-dependencies.yml b/eng/pipelines/steps/update-dotnet-dependencies.yml index 09a18a125..135cca3ce 100644 --- a/eng/pipelines/steps/update-dotnet-dependencies.yml +++ b/eng/pipelines/steps/update-dotnet-dependencies.yml @@ -2,7 +2,18 @@ parameters: useInternalBuild: false steps: -- script: $(engPath)/get-drop-versions.sh $(channel) ${{parameters.useInternalBuild}} "$(dotnetbuilds-internal-container-read-token)" "$(dn-bot-devdiv-dnceng-rw-code-pat)" +- pwsh: | + $args = @{ + Channel = "$(channel)" + } + + if ("${{ parameters.useInternalBuild }}" -eq "true") { + $args["UseInternalBuild"] = $true + $args["BlobStorageSasQueryString"] = "$(dotnetbuilds-internal-container-read-token)" + $args["AzdoVersionsRepoInfoAccessToken"] = "$(dn-bot-devdiv-dnceng-rw-code-pat)" + } + + $(engPath)/Get-DropVersions.ps1 @args displayName: Get Versions - powershell: | $args = @{ diff --git a/eng/pipelines/update-dependencies.yml b/eng/pipelines/update-dependencies.yml index cd764a3c8..eece69852 100644 --- a/eng/pipelines/update-dependencies.yml +++ b/eng/pipelines/update-dependencies.yml @@ -38,7 +38,7 @@ stages: ChannelQuality: $(monitor7Quality) StableBranding: $(monitor7StableBranding) steps: - - script: $(engPath)/get-drop-versions-monitor.sh $(MajorMinorVersion)/$(ChannelQuality) + - pwsh: $(engPath)/Get-MonitorDropVersions.ps1 -Channel $(MajorMinorVersion)/$(ChannelQuality) displayName: Get Versions - template: steps/update-dependencies.yml parameters: