Skip to content

(build) improvements in the CI solution #4252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build/artifacts/Tasks/ArtifactsDotnetToolTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Artifacts.Tasks;

[TaskName(nameof(ArtifactsDotnetToolTest))]
[TaskDescription("Tests the dotnet global tool in docker container")]
[TaskArgument(Arguments.DockerRegistry, Constants.DockerHub, Constants.GitHub)]
[TaskArgument(Arguments.DockerDotnetVersion, Constants.VersionCurrent, Constants.VersionLatest)]
[TaskArgument(Arguments.DockerDistro, Constants.AlpineLatest, Constants.DebianLatest, Constants.UbuntuLatest)]
[DockerRegistryArgument]
[DockerDotnetArgument]
[DockerDistroArgument]
[IsDependentOn(typeof(ArtifactsPrepare))]
public class ArtifactsDotnetToolTest : FrostingTask<BuildContext>
{
Expand Down
6 changes: 3 additions & 3 deletions build/artifacts/Tasks/ArtifactsExecutableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ private static void PackageTest(BuildContextBase context, string packageToTest)

context.NuGetInstall(packageToTest, new NuGetInstallSettings
{
Source = new[]
{
Source =
[
context.MakeAbsolute(Paths.Nuget).FullPath
},
],
ExcludeVersion = true,
Prerelease = true,
OutputDirectory = outputDirectory
Expand Down
6 changes: 3 additions & 3 deletions build/artifacts/Tasks/ArtifactsMsBuildCoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Artifacts.Tasks;

[TaskName(nameof(ArtifactsMsBuildCoreTest))]
[TaskDescription("Tests the msbuild package in docker container")]
[TaskArgument(Arguments.DockerRegistry, Constants.DockerHub, Constants.GitHub)]
[TaskArgument(Arguments.DockerDotnetVersion, Constants.VersionCurrent, Constants.VersionLatest)]
[TaskArgument(Arguments.DockerDistro, Constants.AlpineLatest, Constants.DebianLatest, Constants.UbuntuLatest)]
[DockerRegistryArgument]
[DockerDotnetArgument]
[DockerDistroArgument]
[IsDependentOn(typeof(ArtifactsPrepare))]
public class ArtifactsMsBuildCoreTest : FrostingTask<BuildContext>
{
Expand Down
2 changes: 1 addition & 1 deletion build/artifacts/Tasks/ArtifactsMsBuildFullTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override void Run(BuildContext context)
Verbosity = DotNetVerbosity.Minimal,
Configuration = context.MsBuildConfiguration,
MSBuildSettings = dotnetMsBuildSettings,
Sources = new[] { nugetSource }
Sources = [nugetSource]
});

var exe = Paths.Integration.Combine("build").Combine(framework).CombineWithFilePath("app.dll");
Expand Down
6 changes: 3 additions & 3 deletions build/artifacts/Tasks/ArtifactsNativeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Artifacts.Tasks;

[TaskName(nameof(ArtifactsNativeTest))]
[TaskDescription("Tests the native executables in docker container")]
[TaskArgument(Arguments.DockerRegistry, Constants.DockerHub, Constants.GitHub)]
[TaskArgument(Arguments.DockerDotnetVersion, Constants.VersionCurrent, Constants.VersionLatest)]
[TaskArgument(Arguments.DockerDistro, Constants.AlpineLatest, Constants.DebianLatest, Constants.UbuntuLatest)]
[DockerRegistryArgument]
[DockerDotnetArgument]
[DockerDistroArgument]
[IsDependentOn(typeof(ArtifactsPrepare))]
public class ArtifactsNativeTest : FrostingTask<BuildContext>
{
Expand Down
6 changes: 3 additions & 3 deletions build/artifacts/Tasks/ArtifactsPrepare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Artifacts.Tasks;

[TaskName(nameof(ArtifactsPrepare))]
[TaskDescription("Pulls the docker images needed for testing the artifacts")]
[TaskArgument(Arguments.DockerRegistry, Constants.DockerHub, Constants.GitHub)]
[TaskArgument(Arguments.DockerDotnetVersion, Constants.VersionCurrent, Constants.VersionLatest)]
[TaskArgument(Arguments.DockerDistro, Constants.AlpineLatest, Constants.DebianLatest, Constants.UbuntuLatest)]
[DockerRegistryArgument]
[DockerDotnetArgument]
[DockerDistroArgument]
public class ArtifactsPrepare : FrostingTask<BuildContext>
{
public override bool ShouldRun(BuildContext context)
Expand Down
6 changes: 3 additions & 3 deletions build/artifacts/Tasks/ArtifactsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Artifacts.Tasks;

[TaskName(nameof(ArtifactsTest))]
[TaskDescription("Tests packages in docker container")]
[TaskArgument(Arguments.DockerRegistry, Constants.DockerHub, Constants.GitHub)]
[TaskArgument(Arguments.DockerDotnetVersion, Constants.VersionCurrent, Constants.VersionLatest)]
[TaskArgument(Arguments.DockerDistro, Constants.AlpineLatest, Constants.DebianLatest, Constants.UbuntuLatest)]
[DockerRegistryArgument]
[DockerDotnetArgument]
[DockerDistroArgument]
[IsDependentOn(typeof(ArtifactsNativeTest))]
[IsDependentOn(typeof(ArtifactsDotnetToolTest))]
[IsDependentOn(typeof(ArtifactsMsBuildCoreTest))]
Expand Down
3 changes: 2 additions & 1 deletion build/build/BuildLifetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public override void Setup(BuildContext context, ISetupContext info)
private static void SetMsBuildSettingsVersion(BuildContext context)
{
var msBuildSettings = context.MsBuildSettings;
var version = context.Version!;
ArgumentNullException.ThrowIfNull(context.Version);
var version = context.Version;

msBuildSettings.SetVersion(version.SemVersion);
msBuildSettings.SetAssemblyVersion(version.Version);
Expand Down
2 changes: 1 addition & 1 deletion build/build/Tasks/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override void Run(BuildContext context)
context.DotNetRestore(sln, new DotNetRestoreSettings
{
Verbosity = DotNetVerbosity.Minimal,
Sources = new[] { Constants.NugetOrgUrl },
Sources = [Constants.NugetOrgUrl],
MSBuildSettings = context.MsBuildSettings
});

Expand Down
2 changes: 1 addition & 1 deletion build/build/Tasks/BuildPrepare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override void Run(BuildContext context)
new()
{
Verbosity = DotNetVerbosity.Minimal,
Sources = new[] { Constants.NugetOrgUrl },
Sources = [Constants.NugetOrgUrl],
});

context.DotNetBuild("./src/GitVersion.App/GitVersion.App.csproj",
Expand Down
15 changes: 8 additions & 7 deletions build/build/Tasks/Package/PackageChocolatey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public override void Run(BuildContext context)
.Select(file => new ChocolateyNuSpecContent { Source = file.FullPath, Target = file.FullPath.Replace(artifactPath, "") })
.ToArray();

metaPackageSettings.Dependencies = new[]
{
metaPackageSettings.Dependencies =
[
new ChocolateyNuSpecDependency { Id = "GitVersion.Portable", Version = context.Version?.ChocolateyVersion }
};
];

context.ChocolateyPack(metaPackageSettings);
}
Expand All @@ -51,17 +51,18 @@ private static ChocolateyPackSettings GetChocolateyPackSettings(BuildContextBase
Version = context.Version?.ChocolateyVersion,
Title = "GitVersion",
Description = "Derives SemVer information from a repository following GitFlow or GitHubFlow.",
Authors = new[] { "GitTools and Contributors" },
Owners = new[] { "GitTools and Contributors" },
Authors = ["GitTools and Contributors"],
Owners = ["GitTools and Contributors"],
Copyright = $"Copyright GitTools {DateTime.Now.Year}",
DocsUrl = new Uri("https://gitversion.net/docs/"),
LicenseUrl = new Uri("https://opensource.org/license/mit/"),
ProjectUrl = new Uri("https://github.com/GitTools/GitVersion"),
ProjectSourceUrl = new Uri("https://github.com/GitTools/GitVersion"),
IconUrl = new Uri("https://raw.githubusercontent.com/GitTools/graphics/master/GitVersion/Color/icon_100x100.png"),
RequireLicenseAcceptance = false,
Tags = new[] { "Git", "Versioning", "GitVersion", "GitFlowVersion", "GitFlow", "GitHubFlow", "SemVer" },
ReleaseNotes = new[] { $"https://github.com/GitTools/GitVersion/releases/tag/{context.Version?.ChocolateyVersion}" },
Tags = ["Git", "Versioning", "GitVersion", "GitFlowVersion", "GitFlow", "GitHubFlow", "SemVer"],
ReleaseNotes = [$"https://github.com/GitTools/GitVersion/releases/tag/{context.Version?.ChocolateyVersion}"
],
OutputDirectory = Paths.Nuget,
LimitOutput = true,
};
Expand Down
4 changes: 2 additions & 2 deletions build/build/Tasks/Test/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Build.Tasks;

[TaskName(nameof(UnitTest))]
[TaskDescription("Run the unit tests")]
[TaskArgument(Arguments.DotnetTarget, Constants.VersionCurrent, Constants.VersionLatest)]
[DotnetArgument]
[IsDependentOn(typeof(Build))]
public class UnitTest : FrostingTask<BuildContext>
{
Expand Down Expand Up @@ -73,7 +73,7 @@ private static void TestProjectForTarget(BuildContext context, FilePath project,
};

var resultsPath = context.MakeAbsolute(testResultsPath.CombineWithFilePath($"{projectName}.results.xml"));
settings.Loggers = new[] { $"junit;LogFilePath={resultsPath}" };
settings.Loggers = [$"junit;LogFilePath={resultsPath}"];

var coverletSettings = new CoverletSettings
{
Expand Down
3 changes: 2 additions & 1 deletion build/build/Tasks/ValidateVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public class ValidateVersion : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
ArgumentNullException.ThrowIfNull(context.Version);
var gitVersionTool = context.GetGitVersionToolLocation();
context.ValidateOutput("dotnet", $"\"{gitVersionTool}\" -version", context.Version!.GitVersion!.InformationalVersion!);
context.ValidateOutput("dotnet", $"\"{gitVersionTool}\" -version", context.Version.GitVersion.InformationalVersion);
}
}
10 changes: 2 additions & 8 deletions build/common/Addins/GitVersion/GitVersionAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ public static class GitVersionAliases
[CakeMethodAlias]
public static GitVersion GitVersion(this ICakeContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
ArgumentNullException.ThrowIfNull(context);

return GitVersion(context, new GitVersionSettings());
}
Expand Down Expand Up @@ -104,10 +101,7 @@ public static GitVersion GitVersion(this ICakeContext context)
[CakeMethodAlias]
public static GitVersion GitVersion(this ICakeContext context, GitVersionSettings settings)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
ArgumentNullException.ThrowIfNull(context);

var gitVersionRunner = new GitVersionRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log);
return gitVersionRunner.Run(settings);
Expand Down
46 changes: 27 additions & 19 deletions build/common/Addins/GitVersion/GitVersionRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Common.Addins.GitVersion;
/// <summary>
/// The GitVersion runner.
/// </summary>
public sealed class GitVersionRunner : Tool<GitVersionSettings>
public sealed partial class GitVersionRunner : Tool<GitVersionSettings>
{
private readonly ICakeLog log;
private readonly ICakeLog _log;

/// <summary>
/// Initializes a new instance of the <see cref="GitVersionRunner"/> class.
Expand All @@ -23,7 +23,7 @@ public GitVersionRunner(
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools,
ICakeLog log) : base(fileSystem, environment, processRunner, tools) => this.log = log;
ICakeLog log) : base(fileSystem, environment, processRunner, tools) => this._log = log;

/// <summary>
/// Runs GitVersion and processes the results.
Expand All @@ -32,31 +32,27 @@ public GitVersionRunner(
/// <returns>A task with the GitVersion results.</returns>
public GitVersion Run(GitVersionSettings settings)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}
ArgumentNullException.ThrowIfNull(settings);

var output = string.Empty;
Run(settings, GetArguments(settings), new ProcessSettings { RedirectStandardOutput = true }, process =>
{
output = string.Join("\n", process.GetStandardOutput());
if (log.Verbosity < Verbosity.Diagnostic)
if (this._log.Verbosity >= Verbosity.Diagnostic) return;
var regex = ParseErrorRegex();
var errors = regex.Matches(output)
.SelectMany(match => new[] { match.Groups[1].Value, match.Groups[2].Value });
foreach (var error in errors)
{
var errors = Regex.Matches(output, @"( *ERROR:? [^\n]*)\n([^\n]*)")
.SelectMany(match => new[] { match.Groups[1].Value, match.Groups[2].Value });
foreach (var error in errors)
{
log.Error(error);
}
this._log.Error(error);
}
});

if (!settings.OutputTypes.Contains(GitVersionOutput.Json))
return new GitVersion();

var jsonStartIndex = output.IndexOf("{", StringComparison.Ordinal);
var jsonEndIndex = output.IndexOf("}", StringComparison.Ordinal);
var jsonStartIndex = output.IndexOf('{');
var jsonEndIndex = output.IndexOf('}');
var json = output.Substring(jsonStartIndex, jsonEndIndex - jsonStartIndex + 1);

return JsonConvert.DeserializeObject<GitVersion>(json) ?? new GitVersion();
Expand All @@ -71,6 +67,7 @@ private ProcessArgumentBuilder GetArguments(GitVersionSettings settings)
builder.Append("-output");
builder.Append("json");
}

if (settings.OutputTypes.Contains(GitVersionOutput.BuildServer))
{
builder.Append("-output");
Expand Down Expand Up @@ -119,7 +116,8 @@ private ProcessArgumentBuilder GetArguments(GitVersionSettings settings)
}
else
{
log.Warning("If you leave the branch name for GitVersion unset, it will fallback to the default branch for the repository.");
this._log.Warning(
"If you leave the branch name for GitVersion unset, it will fallback to the default branch for the repository.");
}

if (!string.IsNullOrWhiteSpace(settings.Commit))
Expand All @@ -146,13 +144,14 @@ private ProcessArgumentBuilder GetArguments(GitVersionSettings settings)
builder.Append("-nofetch");
}

var verbosity = settings.Verbosity ?? log.Verbosity;
var verbosity = settings.Verbosity ?? this._log.Verbosity;

if (verbosity != Verbosity.Normal)
{
builder.Append("-verbosity");
builder.Append(verbosity.ToString());
}

return builder;
}

Expand All @@ -166,5 +165,14 @@ private ProcessArgumentBuilder GetArguments(GitVersionSettings settings)
/// Gets the possible names of the tool executable.
/// </summary>
/// <returns>The tool executable name.</returns>
protected override IEnumerable<string> GetToolExecutableNames() => new[] { "GitVersion.exe", "dotnet-gitversion", "dotnet-gitversion.exe", "gitversion" };
protected override IEnumerable<string> GetToolExecutableNames() =>
[
"GitVersion.exe",
"dotnet-gitversion",
"dotnet-gitversion.exe",
"gitversion"
];

[GeneratedRegex(@"( *ERROR:? [^\n]*)\n([^\n]*)")]
private static partial Regex ParseErrorRegex();
}
9 changes: 8 additions & 1 deletion build/common/Lifetime/BuildLifetimeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ public override void Setup(T context, ISetupContext info)
context.Information("Running BuildPrepare...");
return;
}

var gitVersionPath = context.GetGitVersionDotnetToolLocation();
if (gitVersionPath is null || context.FileExists(gitVersionPath) is false)
{
throw new FileNotFoundException("Failed to locate the Release build of gitversion.dll in ./tools/gitversion. Try running \"./build.ps1 -Stage build -Target BuildPrepare\"");
}

var gitVersionSettings = new GitVersionSettings
{
OutputTypes = [GitVersionOutput.Json, GitVersionOutput.BuildServer],
ToolPath = context.Tools.Resolve(["dotnet.exe", "dotnet"]),
ArgumentCustomization = args => args.Prepend(context.GetGitVersionDotnetToolLocation()?.FullPath ?? throw new FileNotFoundException("Failed to locate the Release build of gitversion.dll in ./tools/gitversion. Try running \"./build.ps1 -Stage build -Target BuildPrepare\""))
ArgumentCustomization = args => args.Prepend(gitVersionPath.FullPath)
};

var gitVersion = context.GitVersion(gitVersionSettings);
Expand Down
2 changes: 1 addition & 1 deletion build/common/Utilities/BuildContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Common.Utilities;

public class BuildContextBase : FrostingContext
{
protected BuildContextBase(ICakeContext context) : base(context) => Platform = Environment.Platform.Family;
protected BuildContextBase(ICakeContext context) : base(context) => Platform = context.Environment.Platform.Family;
public PlatformFamily Platform { get; set; }
public BuildVersion? Version { get; set; }
public bool IsOriginalRepo { get; set; }
Expand Down
9 changes: 4 additions & 5 deletions build/common/Utilities/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ReSharper disable MemberCanBePrivate.Global
namespace Common.Utilities;

public static class Constants
Expand All @@ -7,12 +8,13 @@ public static class Constants

public const string VersionCurrent = "6.0";
public const string VersionLatest = "8.0";
public static readonly string[] Frameworks = [VersionCurrent, VersionLatest];

public const string DefaultBranch = "main";
public const string DefaultConfiguration = "Release";

public static readonly Architecture[] ArchToBuild = [Architecture.Amd64, Architecture.Arm64];
public static readonly string[] Frameworks = [VersionCurrent, VersionLatest];
public static readonly string[] Architectures = [nameof(Architecture.Amd64), nameof(Architecture.Arm64)];

public const string DockerBaseImageName = "gittools/build-images";
public const string DockerImageName = "gittools/gitversion";
Expand All @@ -21,9 +23,7 @@ public static class Constants
public const string GitHub = "github";
public const string DockerHubRegistry = "docker.io";
public const string GitHubContainerRegistry = "ghcr.io";

public const string Arm64 = "arm64";
public const string Amd64 = "amd64";
public static readonly string[] DockerRegistries = [DockerHub, GitHub];

public const string AlpineLatest = "alpine.3.20";
public const string CentosStreamLatest = "centos.stream.9";
Expand All @@ -32,7 +32,6 @@ public static class Constants
public const string Ubuntu2004 = "ubuntu.20.04";
public const string Ubuntu2204 = "ubuntu.22.04";
public const string Ubuntu2404 = "ubuntu.24.04";
public const string UbuntuLatest = Ubuntu2404;

public const string DockerDistroLatest = DebianLatest;

Expand Down
Loading