diff --git a/build/Build.DetermineAffectedTests.cs b/build/Build.DetermineAffectedTests.cs index 5071a5747..c0f478bca 100644 --- a/build/Build.DetermineAffectedTests.cs +++ b/build/Build.DetermineAffectedTests.cs @@ -19,6 +19,7 @@ partial class Build target => target .Executes(() => { + /* if (GitVersionInfo is null) throw new ArgumentNullException(nameof(GitVersionInfo)); @@ -54,5 +55,6 @@ partial class Build { Log.Warning("Did not publish affected.proj artifact"); } + */ }); } \ No newline at end of file diff --git a/build/Build.cs b/build/Build.cs index 1ddaedcc9..cfebcea40 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Runtime.InteropServices; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; using Calamari.ConsolidateCalamariPackages; using NuGet.Packaging; @@ -16,6 +17,7 @@ using Nuke.Common.Tools.DotNet; using Nuke.Common.Tools.GitVersion; using Nuke.Common.Tools.NuGet; +using Nuke.Common.Tools.OctoVersion; using Nuke.Common.Utilities.Collections; using Serilog; using static Nuke.Common.IO.FileSystemTasks; @@ -87,8 +89,9 @@ partial class Build : NukeBuild [Parameter] readonly string? TargetRuntime; - [GitVersion] - readonly GitVersion? GitVersionInfo; + // See constructor for explanation + //[OctoVersion(BranchMember = nameof(BranchName), Framework = "net8.0")] + public Lazy OctoVersionInfo; static readonly List CalamariProjectsToSkipConsolidation = new() { "Calamari.CloudAccounts", "Calamari.Common", "Calamari.ConsolidateCalamariPackages" }; @@ -96,10 +99,27 @@ partial class Build : NukeBuild List ProjectCompressionTasks = new(); + const string CiBranchNameEnvVariable = "OCTOVERSION_CurrentBranch"; + [Parameter($"The name of the current git branch. OctoVersion will use this to calculate the version number. This can be set via the environment variable {CiBranchNameEnvVariable}.", Name = CiBranchNameEnvVariable)] + string? BranchName { get; set; } + public Build() { - NugetVersion = new Lazy(GetNugetVersion); + //NugetVersion = new Lazy(GetNugetVersion); + + // Mimic the behaviour of this attribute, but lazily so we don't pay the OctoVersion cost when it isn't needed + //[OctoVersion(BranchMember = nameof(BranchName), Framework = "net8.0")] + OctoVersionInfo = new Lazy(() => + { + var attribute = new OctoVersionAttribute { BranchMember = nameof(BranchName), Framework = "net8.0" }; + + // the Attribute does all the work such as calling TeamCity.Instance?.SetBuildNumber for us + var version = attribute.GetValue(null!, this); + return version as OctoVersionInfo; + + + }, LazyThreadSafetyMode.ExecutionAndPublication); // This initialisation is required to ensure the build script can // perform actions such as GetRuntimeIdentifiers() on projects. ProjectModelTasks.Initialize(); @@ -114,10 +134,11 @@ public Build() static AbsolutePath ConsolidatedPackageDirectory => ArtifactsDirectory / "consolidated"; static AbsolutePath LegacyCalamariDirectory = PublishDirectory / "Calamari.Legacy"; - Lazy NugetVersion { get; } + /*Lazy NugetVersion { get; }*/ Target CheckForbiddenWords => - _ => _.Executes(() => + _ => _ + .OnlyWhenStatic(() => !IsLocalBuild).Executes(() => { Log.Information("Checking codebase for forbidden words"); @@ -174,17 +195,16 @@ public Build() DotNetBuild(_ => _.SetProjectFile(SourceDirectory / "Calamari.FullFrameworkTools"/ "Calamari.FullFrameworkTools.csproj") .SetConfiguration(Configuration) - .SetNoRestore(false) + .SetNoRestore(true) .SetVersion(OctoVersionInfo.Value?.FullSemVer) - .SetVersion(NugetVersion.Value) - .SetInformationalVersion(GitVersionInfo?.InformationalVersion)); + .SetInformationalVersion(OctoVersionInfo.Value?.InformationalVersion)); Log.Information("Done"); DotNetBuild(_ => _.SetProjectFile(Solution) .SetConfiguration(Configuration) .SetNoRestore(true) - .SetVersion(NugetVersion.Value) - .SetInformationalVersion(GitVersionInfo?.InformationalVersion)); + .SetVersion(OctoVersionInfo.Value?.FullSemVer) + .SetInformationalVersion(OctoVersionInfo.Value?.InformationalVersion)); }); Target CompileFullFrameworkTools => @@ -196,14 +216,15 @@ public Build() { throw new Exception("This step can only be run on Windows"); } + var octoVersionInfo = OctoVersionInfo.Value ?? throw new InvalidOperationException("Required OctoVersionInfo was not populated"); - Log.Information("Compiling Calamari v{CalamariVersion}", NugetVersion.Value); - var nugetVersion = NugetVersion.Value; - var tools = DoPublish("Calamari.FullFrameworkTools", Frameworks.Net462, nugetVersion); - var tests = DoPublish("Calamari.FullFrameworkTools.Tests", Frameworks.Net462, nugetVersion); + Log.Information("Compiling Calamari v{CalamariVersion}", octoVersionInfo); + + var tools = DoPublish("Calamari.FullFrameworkTools", Frameworks.Net462, octoVersionInfo.FullSemVer); + var tests = DoPublish("Calamari.FullFrameworkTools.Tests", Frameworks.Net462, octoVersionInfo.FullSemVer); - tools.ZipTo($"{ArtifactsDirectory / "Calamari.FullFrameworkTools"}.{nugetVersion}.zip"); - tests.ZipTo($"{ArtifactsDirectory / "Calamari.FullFrameworkTools.Tests"}.{nugetVersion}.zip"); + tools.ZipTo($"{ArtifactsDirectory / "Calamari.FullFrameworkTools"}.{octoVersionInfo}.zip"); + tests.ZipTo($"{ArtifactsDirectory / "Calamari.FullFrameworkTools.Tests"}.{octoVersionInfo}.zip"); }); Target CalamariConsolidationTests => @@ -230,10 +251,11 @@ public Build() + "deployment steps in Octopus Server", RootProjectName, $"{RootProjectName}.{FixedRuntimes.Cloud}"); - var nugetVersion = NugetVersion.Value; + var octoVersionInfo = OctoVersionInfo.Value ?? throw new InvalidOperationException("Required OctoVersionInfo was not populated"); + var outputDirectory = DoPublish(RootProjectName, OperatingSystem.IsWindows() ? Frameworks.Net462 : Frameworks.Net60, - nugetVersion); + octoVersionInfo.FullSemVer); if (OperatingSystem.IsWindows()) { CopyDirectoryRecursively(outputDirectory, (LegacyCalamariDirectory / RootProjectName), DirectoryExistsPolicy.Merge); @@ -246,12 +268,12 @@ public Build() DoPublish(RootProjectName, OperatingSystem.IsWindows() ? Frameworks.Net462 : Frameworks.Net60, - nugetVersion, + octoVersionInfo.FullSemVer, FixedRuntimes.Cloud); // Create the self-contained Calamari packages for each runtime ID defined in Calamari.csproj foreach (var rid in GetRuntimeIdentifiers(Solution.GetProject(RootProjectName)!)!) - DoPublish(RootProjectName, Frameworks.Net60, nugetVersion, rid); + DoPublish(RootProjectName, Frameworks.Net60, octoVersionInfo.FullSemVer, rid); }); Target PublishCalamariFlavourProjects => @@ -405,7 +427,7 @@ void CompressCalamariProject(Project project) return; } Log.Verbose($"Compressing Calamari.Legacy"); - LegacyCalamariDirectory.ZipTo(ArtifactsDirectory / $"Calamari.Legacy.{NugetVersion.Value}.zip"); + LegacyCalamariDirectory.ZipTo(ArtifactsDirectory / $"Calamari.Legacy.{OctoVersionInfo.Value}.zip"); }); Target PackBinaries => @@ -413,7 +435,7 @@ void CompressCalamariProject(Project project) .DependsOn(PublishCalamariFlavourProjects) .Executes(async () => { - var nugetVersion = NugetVersion.Value; + var nugetVersion = OctoVersionInfo.Value.FullSemVer; var packageActions = new List { () => DoPackage(RootProjectName, @@ -460,7 +482,7 @@ void CompressCalamariProject(Project project) .DependsOn(PublishCalamariFlavourProjects) .Executes(async () => { - var nugetVersion = NugetVersion.Value; + var nugetVersion = OctoVersionInfo.Value?.FullSemVer ?? throw new InvalidOperationException("Required OctoVersionInfo was not populated"); var defaultTarget = OperatingSystem.IsWindows() ? Frameworks.Net462 : Frameworks.Net60; AbsolutePath binFolder = SourceDirectory / "Calamari.Tests" / "bin" / Configuration / defaultTarget; Directory.Exists(binFolder); @@ -543,7 +565,7 @@ void CompressCalamariProject(Project project) packageReferences.Add(new BuildPackageReference { Name = flavour, - Version = NugetVersion.Value, + Version = OctoVersionInfo.Value.FullSemVer, PackagePath = ArtifactsDirectory / $"{flavour}.zip" }); } @@ -562,9 +584,10 @@ void CompressCalamariProject(Project project) _ => _.DependsOn(PackageConsolidatedCalamariZip) .Executes(() => { + var nugetVersion = OctoVersionInfo.Value?.FullSemVer ?? throw new InvalidOperationException("Required OctoVersionInfo was not populated"); NuGetPack(s => s.SetTargetPath(BuildDirectory / "Calamari.Consolidated.nuspec") .SetBasePath(BuildDirectory) - .SetVersion(NugetVersion.Value) + .SetVersion(nugetVersion) .SetOutputDirectory(ArtifactsDirectory)); }); @@ -575,12 +598,13 @@ void CompressCalamariProject(Project project) .DependsOn(CopyToLocalPackages) .Executes(() => { + var nugetVersion = OctoVersionInfo.Value?.FullSemVer ?? throw new InvalidOperationException("Required OctoVersionInfo was not populated"); var serverProjectFile = RootDirectory / ".." / "OctopusDeploy" / "source" / "Octopus.Server" / "Octopus.Server.csproj"; if (File.Exists(serverProjectFile)) { Log.Information("Setting Calamari version in Octopus Server " + "project {ServerProjectFile} to {NugetVersion}", - serverProjectFile, NugetVersion.Value); + serverProjectFile, nugetVersion); SetOctopusServerCalamariVersion(serverProjectFile); } else @@ -588,13 +612,14 @@ void CompressCalamariProject(Project project) Log.Warning("Could not set Calamari version in Octopus Server project " + "{ServerProjectFile} to {NugetVersion} as could not find " + "project file", - serverProjectFile, NugetVersion.Value); + serverProjectFile, nugetVersion); } }); Target SetTeamCityVersion => _ => _.Executes(() => { - TeamCity.Instance?.SetBuildNumber(NugetVersion.Value); + var nugetVersion = OctoVersionInfo.Value?.FullSemVer ?? throw new InvalidOperationException("Required OctoVersionInfo was not populated"); + TeamCity.Instance?.SetBuildNumber(nugetVersion); }); Target BuildLocal => _ => _.DependsOn(PackCalamariConsolidatedNugetPackage) @@ -633,8 +658,8 @@ AbsolutePath DoPublish(string project, string framework, string version, string? DotNetPublish(_ => _.SetProject(Solution.GetProject(project)) .SetConfiguration(Configuration) .SetOutput(publishedTo) + .SetNoBuild(true) .SetFramework(framework) - .SetVersion(NugetVersion.Value) .SetVerbosity(BuildVerbosity) .SetRuntime(runtimeId) .SetVersion(version)); @@ -704,7 +729,7 @@ void DoPackage(string project, string framework, string version, string? runtime NuGetTasks.NuGetPack(_ => _.SetBasePath(publishedTo) .SetOutputDirectory(ArtifactsDirectory) .SetTargetPath(nuspec) - .SetVersion(NugetVersion.Value) + .SetVersion(version) .SetVerbosity(NuGetVerbosity.Normal) .SetProperties(nugetPackProperties)); } @@ -714,18 +739,10 @@ void SetOctopusServerCalamariVersion(string projectFile) { var text = File.ReadAllText(projectFile); text = Regex.Replace(text, @"", - $""); + $""); File.WriteAllText(projectFile, text); } - string GetNugetVersion() - { - return AppendTimestamp - ? $"{GitVersionInfo?.NuGetVersion}-{DateTime.Now:yyyyMMddHHmmss}" - : GitVersionInfo?.NuGetVersion - ?? throw new InvalidOperationException("Unable to retrieve valid Nuget Version"); - } - IReadOnlyCollection GetRuntimeIdentifiers(Project? project) { if (project is null) diff --git a/build/_build.csproj b/build/_build.csproj index 6819c0d13..80f3a4dd3 100644 --- a/build/_build.csproj +++ b/build/_build.csproj @@ -26,8 +26,8 @@ - - + +