Skip to content

Commit

Permalink
Lets use OctoVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
zentron committed Sep 4, 2024
1 parent 3c20156 commit 4516f53
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 40 deletions.
2 changes: 2 additions & 0 deletions build/Build.DetermineAffectedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ partial class Build
target => target
.Executes(() =>
{
/*
if (GitVersionInfo is null)
throw new ArgumentNullException(nameof(GitVersionInfo));
Expand Down Expand Up @@ -54,5 +55,6 @@ partial class Build
{
Log.Warning("Did not publish affected.proj artifact");
}
*/
});
}
93 changes: 55 additions & 38 deletions build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -87,19 +89,37 @@ 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?> OctoVersionInfo;

static readonly List<string> CalamariProjectsToSkipConsolidation = new() { "Calamari.CloudAccounts", "Calamari.Common", "Calamari.ConsolidateCalamariPackages" };

List<Task> SignDirectoriesTasks = new();

List<Task> 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<string>(GetNugetVersion);
//NugetVersion = new Lazy<string>(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<OctoVersionInfo?>(() =>
{
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();
Expand All @@ -114,10 +134,11 @@ public Build()
static AbsolutePath ConsolidatedPackageDirectory => ArtifactsDirectory / "consolidated";
static AbsolutePath LegacyCalamariDirectory = PublishDirectory / "Calamari.Legacy";

Lazy<string> NugetVersion { get; }
/*Lazy<string> NugetVersion { get; }*/

Target CheckForbiddenWords =>
_ => _.Executes(() =>
_ => _
.OnlyWhenStatic(() => !IsLocalBuild).Executes(() =>
{
Log.Information("Checking codebase for forbidden words");

Expand Down Expand Up @@ -173,15 +194,14 @@ public Build()
.SetConfiguration(Configuration)
.SetNoRestore(false)
.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 =>
Expand All @@ -193,14 +213,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 =>
Expand All @@ -227,10 +248,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);
Expand All @@ -243,12 +265,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 =>
Expand Down Expand Up @@ -402,15 +424,15 @@ 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 =>
_ => _.DependsOn(Publish)
.DependsOn(PublishCalamariFlavourProjects)
.Executes(async () =>
{
var nugetVersion = NugetVersion.Value;
var nugetVersion = OctoVersionInfo.Value.FullSemVer;
var packageActions = new List<Action>
{
() => DoPackage(RootProjectName,
Expand Down Expand Up @@ -457,7 +479,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);
Expand Down Expand Up @@ -540,7 +562,7 @@ void CompressCalamariProject(Project project)
packageReferences.Add(new BuildPackageReference
{
Name = flavour,
Version = NugetVersion.Value,
Version = OctoVersionInfo.Value.FullSemVer,
PackagePath = ArtifactsDirectory / $"{flavour}.zip"
});
}
Expand All @@ -559,9 +581,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));
});

Expand All @@ -572,26 +595,28 @@ 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
{
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)
Expand Down Expand Up @@ -630,8 +655,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));
Expand Down Expand Up @@ -701,7 +726,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));
}
Expand All @@ -711,18 +736,10 @@ void SetOctopusServerCalamariVersion(string projectFile)
{
var text = File.ReadAllText(projectFile);
text = Regex.Replace(text, @"<PackageReference Include=""Calamari.Consolidated"" Version=""([\S])+"" />",
$"<PackageReference Include=\"Calamari.Consolidated\" Version=\"{NugetVersion.Value}\" />");
$"<PackageReference Include=\"Calamari.Consolidated\" Version=\"{OctoVersionInfo.Value?.FullSemVer}\" />");
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<string> GetRuntimeIdentifiers(Project? project)
{
if (project is null)
Expand Down
4 changes: 2 additions & 2 deletions build/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
</ItemGroup>

<ItemGroup>
<PackageDownload Include="AzureSignTool" Version="[3.0.0]" />
<PackageDownload Include="GitVersion.Tool" Version="[5.12.0]" />
<PackageDownload Include="AzureSignTool" Version="[3.0.0]" />
<PackageReference Include="Octopus.OctoVersion.Tool" Version="0.3.400" ExcludeAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 4516f53

Please sign in to comment.