From 1810a3256ce29f87bc565b24be8f0a4a9f9b61b2 Mon Sep 17 00:00:00 2001 From: Sergey Koryshev Date: Fri, 17 Mar 2023 11:28:05 +0100 Subject: [PATCH] moved logic of checking if system supports .NET 6 to JobExtension --- src/Agent.Worker/JobExtension.cs | 50 ++++++++++++++++++++++++++++++++ src/Agent.Worker/JobRunner.cs | 46 ----------------------------- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/src/Agent.Worker/JobExtension.cs b/src/Agent.Worker/JobExtension.cs index 08e7878006..4f556dc94b 100644 --- a/src/Agent.Worker/JobExtension.cs +++ b/src/Agent.Worker/JobExtension.cs @@ -72,6 +72,52 @@ public async Task> InitializeJob(IExecutionContext jobContext, Pipel context.Start(); context.Section(StringUtil.Loc("StepStarting", StringUtil.Loc("InitializeJob"))); + // Check if a system supports .NET 6 + PackageVersion agentVersion = new PackageVersion(BuildConstants.AgentPackage.Version); + if (agentVersion.Major < 3) + { + try + { + Trace.Verbose("Checking if your system supports .NET 6"); + + string systemId = PlatformUtil.GetSystemId(); + SystemVersion systemVersion = PlatformUtil.GetSystemVersion(); + string notSupportNet6Message = null; + + if (await PlatformUtil.DoesSystemPersistsInNet6Whitelist()) + { + // Check version of the system + if (!await PlatformUtil.IsNet6Supported()) + { + notSupportNet6Message = $"The operating system the agent is running on is \"{systemId}\" ({systemVersion}), which will not be supported by the .NET 6 based v3 agent. Please upgrade the operating system of this host to ensure compatibility with the v3 agent. See https://aka.ms/azdo-pipeline-agent-version"; + if (AgentKnobs.AgentFailOnIncompatibleOS.GetValue(jobContext).AsBoolean() && + !AgentKnobs.AcknowledgeNoUpdates.GetValue(jobContext).AsBoolean()) + { + throw new UnsupportedOsException(StringUtil.Loc("FailAgentOnUnsupportedOs")); + } + } + } + else + { + notSupportNet6Message = $"The operating system the agent is running on is \"{systemId}\" ({systemVersion}), which has not been tested with the .NET 6 based v3 agent. The v2 agent wil not automatically upgrade to the v3 agent. You can manually download the .NET 6 based v3 agent from https://github.com/microsoft/azure-pipelines-agent/releases. See https://aka.ms/azdo-pipeline-agent-version"; + } + + if (!string.IsNullOrWhiteSpace(notSupportNet6Message)) + { + context.Warning(notSupportNet6Message); + } + } + catch (UnsupportedOsException) + { + throw; + } + catch (Exception ex) + { + Trace.Error($"Error has occurred while checking if system supports .NET 6: {ex}"); + context.Warning(ex.Message); + } + } + // Set agent version variable. context.SetVariable(Constants.Variables.Agent.Version, BuildConstants.AgentPackage.Version); context.Output(StringUtil.Loc("AgentNameLog", context.Variables.Get(Constants.Variables.Agent.Name))); @@ -592,4 +638,8 @@ private void OutputSetupInfo(IExecutionContext context) } } } + + public class UnsupportedOsException : Exception { + public UnsupportedOsException(string message) : base(message) { } + } } \ No newline at end of file diff --git a/src/Agent.Worker/JobRunner.cs b/src/Agent.Worker/JobRunner.cs index 12b8df185f..fbf9b1e647 100644 --- a/src/Agent.Worker/JobRunner.cs +++ b/src/Agent.Worker/JobRunner.cs @@ -102,52 +102,6 @@ public async Task RunAsync(Pipelines.AgentJobRequestMessage message, jobContext = HostContext.CreateService(); jobContext.InitializeJob(message, jobRequestCancellationToken); - // Check if a system supports .NET 6 - PackageVersion agentVersion = new PackageVersion(BuildConstants.AgentPackage.Version); - if (agentVersion.Major < 3) - { - try - { - Trace.Verbose("Checking if your system supports .NET 6"); - - string systemId = PlatformUtil.GetSystemId(); - SystemVersion systemVersion = PlatformUtil.GetSystemVersion(); - string notSupportNet6Message = null; - - if (await PlatformUtil.DoesSystemPersistsInNet6Whitelist()) - { - // Check version of the system - if (!await PlatformUtil.IsNet6Supported()) - { - notSupportNet6Message = $"The operating system the agent is running on is \"{systemId}\" ({systemVersion}), which will not be supported by the .NET 6 based v3 agent. Please upgrade the operating system of this host to ensure compatibility with the v3 agent. See https://aka.ms/azdo-pipeline-agent-version"; - if (AgentKnobs.AgentFailOnIncompatibleOS.GetValue(jobContext).AsBoolean() && - !AgentKnobs.AcknowledgeNoUpdates.GetValue(jobContext).AsBoolean()) - { - jobContext.Error(StringUtil.Loc("FailAgentOnUnsupportedOs")); - return await CompleteJobAsync(jobServer, jobContext, message, TaskResult.Failed); - } - } - } - else - { - notSupportNet6Message = $"The operating system the agent is running on is \"{systemId}\" ({systemVersion}), which has not been tested with the .NET 6 based v3 agent. The v2 agent wil not automatically upgrade to the v3 agent. You can manually download the .NET 6 based v3 agent from https://github.com/microsoft/azure-pipelines-agent/releases. See https://aka.ms/azdo-pipeline-agent-version"; - } - - if (!string.IsNullOrWhiteSpace(notSupportNet6Message)) - { - - - jobContext.AddIssue(new Issue() { Type = IssueType.Warning, Message = notSupportNet6Message }); - } - } - catch (Exception ex) - { - jobContext.Warning($"Error has occurred while checking if system supports .NET 6: {ex.Message}"); - Trace.Error($"Error has occurred while checking if system supports .NET 6: {ex}"); - - } - } - Trace.Info("Starting the job execution context."); jobContext.Start(); jobContext.Section(StringUtil.Loc("StepStarting", message.JobDisplayName));