|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.Extensions.Logging; |
| 8 | +using Dotnet.Docker.Model.Release; |
| 9 | +using System.Linq; |
| 10 | +using System.Text.RegularExpressions; |
| 11 | + |
| 12 | +namespace Dotnet.Docker; |
| 13 | + |
| 14 | +internal partial class FromStagingPipelineCommand(ILogger<FromStagingPipelineCommand> logger) |
| 15 | + : BaseCommand<FromStagingPipelineOptions> |
| 16 | +{ |
| 17 | + private readonly ILogger<FromStagingPipelineCommand> _logger = logger; |
| 18 | + |
| 19 | + public override async Task<int> ExecuteAsync(FromStagingPipelineOptions options) |
| 20 | + { |
| 21 | + if (options.Internal) |
| 22 | + { |
| 23 | + throw new NotImplementedException("Updating Dockerfiles for internal builds is not implemented yet."); |
| 24 | + } |
| 25 | + |
| 26 | + ArgumentException.ThrowIfNullOrWhiteSpace(options.AzdoOrganization); |
| 27 | + ArgumentException.ThrowIfNullOrWhiteSpace(options.AzdoProject); |
| 28 | + |
| 29 | + _logger.LogInformation( |
| 30 | + "Updating dependencies based on staging pipeline run ID {options.StagingPipelineRunId}", |
| 31 | + options.StagingPipelineRunId); |
| 32 | + |
| 33 | + // Each pipeline run has a corresponding blob container named stage-${options.StagingPipelineRunId}. |
| 34 | + // Release metadata is stored in metadata/ReleaseManifest.json. |
| 35 | + // Release assets are stored individually under in assets/shipping/assets/[Sdk|Runtime|aspnetcore|...]. |
| 36 | + |
| 37 | + // Get release manifest from staging storage account |
| 38 | + var storageAccount = new StorageAccount(options.StagingStorageAccount); |
| 39 | + var releaseManifestJson = await storageAccount.DownloadTextAsync( |
| 40 | + containerName: $"stage-{options.StagingPipelineRunId}", |
| 41 | + blobPath: "metadata/ReleaseManifest.json"); |
| 42 | + |
| 43 | + var buildManifest = BuildManifest.FromJson(releaseManifestJson); |
| 44 | + var allAssets = buildManifest.AllAssets.ToList(); |
| 45 | + |
| 46 | + // Look through all the assets and get the version of the highest SDK feature band |
| 47 | + var sdkAssets = allAssets |
| 48 | + .Where(asset => SdkRegex.IsMatch(asset.Name)) |
| 49 | + .ToList(); |
| 50 | + var sdkVersions = sdkAssets.Select(asset => asset.Version); |
| 51 | + string highestSdkVersion = VersionHelper.GetHighestSdkVersion(sdkVersions); |
| 52 | + string dockerfileVersion = VersionHelper.ResolveMajorMinorVersion(highestSdkVersion).ToString(); |
| 53 | + |
| 54 | + string runtimeVersion = allAssets |
| 55 | + .Where(asset => RuntimeRegex.IsMatch(asset.Name)) |
| 56 | + .Select(asset => asset.Version) |
| 57 | + .First(); |
| 58 | + |
| 59 | + string aspnetVersion = allAssets |
| 60 | + .Where(asset => AspNetCoreRegex.IsMatch(asset.Name)) |
| 61 | + .Select(asset => asset.Version) |
| 62 | + .First(); |
| 63 | + |
| 64 | + _logger.LogInformation( |
| 65 | + """ |
| 66 | + Resolved .NET versions: |
| 67 | + .NET: {dockerfileVersion} |
| 68 | + - SDK: {highestSdkVersion} |
| 69 | + - Runtime: {runtimeVersion} |
| 70 | + - ASP.NET Core: {aspnetVersion} |
| 71 | + """, |
| 72 | + dockerfileVersion, highestSdkVersion, runtimeVersion, aspnetVersion); |
| 73 | + |
| 74 | + // Run old update-dependencies command using the resolved versions |
| 75 | + var updateDependencies = new SpecificCommand(); |
| 76 | + var updateDependenciesOptions = new SpecificCommandOptions() |
| 77 | + { |
| 78 | + DockerfileVersion = dockerfileVersion.ToString(), |
| 79 | + ProductVersions = new Dictionary<string, string?>() |
| 80 | + { |
| 81 | + // "dotnet" version is required. It sets the "dotnet|*|product-version" |
| 82 | + // variable which is used for runtime-deps, runtime, and aspnet tags. |
| 83 | + { "dotnet", runtimeVersion }, |
| 84 | + { "runtime", runtimeVersion }, |
| 85 | + { "aspnet", aspnetVersion }, |
| 86 | + { "aspnet-composite", aspnetVersion }, |
| 87 | + { "sdk", highestSdkVersion }, |
| 88 | + }, |
| 89 | + |
| 90 | + // Pass through all properties of CreatePullRequestOptions |
| 91 | + User = options.User, |
| 92 | + Email = options.Email, |
| 93 | + Password = options.Password, |
| 94 | + AzdoOrganization = options.AzdoOrganization, |
| 95 | + AzdoProject = options.AzdoProject, |
| 96 | + AzdoRepo = options.AzdoRepo, |
| 97 | + VersionSourceName = options.VersionSourceName, |
| 98 | + SourceBranch = options.SourceBranch, |
| 99 | + TargetBranch = options.TargetBranch, |
| 100 | + }; |
| 101 | + |
| 102 | + return await updateDependencies.ExecuteAsync(updateDependenciesOptions); |
| 103 | + } |
| 104 | + |
| 105 | + // Examples: |
| 106 | + // - "Sdk/8.0.117-servicing.25269.14/dotnet-sdk-8.0.117-linux-x64.tar.gz" |
| 107 | + // - "Sdk/8.0.310-servicing.25113.14/dotnet-sdk-8.0.310-linux-musl-arm64.tar.gz" |
| 108 | + [GeneratedRegex(@"Sdk/.*/dotnet-sdk-.*-linux-x64.tar.gz$")] |
| 109 | + private static partial Regex SdkRegex { get; } |
| 110 | + |
| 111 | + // "aspnetcore/Runtime/8.0.14-servicing.25112.21/aspnetcore-runtime-8.0.14-linux-x64.tar.gz", |
| 112 | + [GeneratedRegex(@"aspnetcore/Runtime/.*/aspnetcore-runtime-.*-linux-x64.tar.gz")] |
| 113 | + private static partial Regex AspNetCoreRegex { get; } |
| 114 | + |
| 115 | + // "Runtime/8.0.14-servicing.25111.18/dotnet-runtime-8.0.14-linux-x64.tar.gz" |
| 116 | + [GeneratedRegex(@"Runtime/.*/dotnet-runtime-.*-linux-x64.tar.gz")] |
| 117 | + private static partial Regex RuntimeRegex { get; } |
| 118 | +} |
0 commit comments