Skip to content

Commit

Permalink
refactor(build): Clean up NUKE config
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnatamo committed Aug 2, 2024
1 parent d227af9 commit a847f81
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 36 deletions.
4 changes: 1 addition & 3 deletions .build/Build.Clean.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using JetBrains.Annotations;

using Nuke.Common;
using Nuke.Common.Tools.DotNet;

partial class Build : NukeBuild
{
Target Clean => _ => _
.Description("Cleans the build tree")
.Description("Cleans the build tree.\n")
.Before(Restore)
.Executes(() =>
DotNetTasks.DotNetClean(c => c.SetProject(SolutionFilePath)));
Expand Down
4 changes: 2 additions & 2 deletions .build/Build.Compile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
partial class Build : NukeBuild
{
Target Restore => _ => _
.Description("Downloads and install .NET packages")
.Description("Downloads and install .NET packages.\n")
.Executes(() =>
DotNetTasks.DotNetRestore(c => c.SetProjectFile(SolutionFilePath)));

Target Compile => _ => _
.Description("Compiles the entire build tree")
.Description("Compiles the entire build tree.\n")
.DependsOn(Restore)
.Executes(() =>
DotNetTasks.DotNetBuild(c => c
Expand Down
27 changes: 10 additions & 17 deletions .build/Build.Docker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,12 @@ partial class Build : NukeBuild
[Parameter("Whether to push the built Docker image to GHCR")]
readonly bool PushImage = false;

private string DockerImage => $"ghcr.io/fetcharr/fetcharr";

private string[] DockerVersionTags => GitVersion.BranchName.Equals("main", StringComparison.InvariantCultureIgnoreCase)
? ["latest", $"{GitVersion.Major}", $"{GitVersion.Major}.{GitVersion.Minor}", $"{GitVersion.MajorMinorPatch}"]
: ["develop", $"develop-{GitVersion.MajorMinorPatch}.{GitVersion.PreReleaseNumber}"];

private string[] DockerImageTags => DockerVersionTags.Select(version => $"{DockerImage}:{version}").ToArray();

private string[] DockerImagePlatforms => ["linux/amd64", "linux/arm", "linux/arm64"];
[Parameter("List of platforms to build the Docker image for")]
readonly string[] ImagePlatforms = ["linux/amd64", "linux/arm", "linux/arm64"];

Target AssertDockerPush => _ => _
.Unlisted()
.Description("Asserts whether the built Docker image can be pushed.")
.Description("Asserts whether the built Docker image can be pushed.\n")
.Before(Restore)
.Executes(() =>
{
Expand All @@ -34,23 +27,23 @@ partial class Build : NukeBuild
});

Target BuildImage => _ => _
.Description("Builds the Docker image of Fetcharr, and optionally pushes it to GHCR.")
.Description("Builds the Docker image of Fetcharr, and optionally pushes it to GHCR.\n")
.DependsOn(AssertDockerPush)
.DependsOn(Test)
.DependsOn(Format)
.Executes(() =>
DockerBuildxBuild(x => x
.SetPath(".")
.SetFile("Dockerfile")
.SetTag(this.DockerImageTags)
.SetPlatform(string.Join(",", this.DockerImagePlatforms))
.SetTag(this.VersionTags.Select(version => $"{DockerImage}:{version}").ToArray())
.SetPlatform(string.Join(",", this.ImagePlatforms))
.SetPush(this.PushImage && this.GithubToken is not null)
.AddCacheFrom("type=gha")
.AddCacheTo("type=gha,mode=max")
.AddLabel("org.opencontainers.image.source=https://github.com/fetcharr/fetcharr")
.AddLabel("org.opencontainers.image.url=https://github.com/fetcharr/fetcharr")
.AddLabel("org.opencontainers.image.description=Automatically sync Plex watchlist to your Sonarr and Radarr instances.")
.AddLabel("org.opencontainers.image.licenses=MIT")
.AddLabel($"org.opencontainers.image.source={RepositoryUrl}")
.AddLabel($"org.opencontainers.image.url={RepositoryUrl}")
.AddLabel($"org.opencontainers.image.description={RepositoryDescription}")
.AddLabel($"org.opencontainers.image.licenses={RepositoryLicense}")
.SetProcessLogger((outputType, output) =>
{
// Workaround for all Docker messages being logged as errors.
Expand Down
40 changes: 40 additions & 0 deletions .build/Build.Environment.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
using Nuke.Common;
using Nuke.Common.CI.GitHubActions;
using Nuke.Common.Git;
using Nuke.Common.IO;
using Nuke.Common.Tools.GitVersion;

partial class Build : NukeBuild
{
private const string RepositoryUrl = "https://github.com/fetcharr/fetcharr";

private const string RepositoryDescription = "Automatically sync Plex watchlist to your Sonarr and Radarr instances.";

private const string RepositoryLicense = "MIT";

private const string DockerImage = "ghcr.io/fetcharr/fetcharr";

private AbsolutePath SourceDirectory => RootDirectory / "src";

private AbsolutePath SolutionFilePath => SourceDirectory / "Fetcharr.sln";
Expand All @@ -14,4 +23,35 @@ partial class Build : NukeBuild

[GitRepository]
readonly GitRepository Repository;

GitHubActions GitHubActions => GitHubActions.Instance;

/// <summary>
/// Gets whether NUKE is building a release build or not.
/// </summary>
private bool IsReleaseBuild => GitVersion.BranchName.Equals("main", StringComparison.InvariantCultureIgnoreCase);

/// <summary>
/// Gets the version tag for the current build, with release version numbering.
/// </summary>
private string ReleaseVersionTag => GitVersion.MajorMinorPatch;

/// <summary>
/// Gets the version tag for the current build, with development version numbering.
/// </summary>
private string DevelopmentVersionTag => $"develop-{GitVersion.MajorMinorPatch}.{GitVersion.PreReleaseNumber}";

/// <summary>
/// Gets the primary version tag for the current version.
/// </summary>
private string VersionTag => this.IsReleaseBuild
? ReleaseVersionTag
: DevelopmentVersionTag;

/// <summary>
/// Gets the version tags for the current version.
/// </summary>
private string[] VersionTags => this.IsReleaseBuild
? ["latest", $"{GitVersion.Major}", $"{GitVersion.Major}.{GitVersion.Minor}", ReleaseVersionTag]
: ["develop", DevelopmentVersionTag];
}
2 changes: 1 addition & 1 deletion .build/Build.Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
partial class Build : NukeBuild
{
Target Format => _ => _
.Description("Performs linting on the build tree")
.Description("Performs linting on the build tree.\n")
.DependsOn(Restore)
.Executes(() =>
DotNetFormat(c => c
Expand Down
11 changes: 4 additions & 7 deletions .build/Build.Release.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@

partial class Build : NukeBuild
{
[Parameter("Whether the release is a pre-release or not.")]
readonly bool PreRelease;

Target Release => _ => _
.Description("Creates and pushes a new release to GitHub")
.Description("Creates and pushes a new release to GitHub.\n")
.DependsOn(BuildImage)
.Requires(() => this.GithubToken)
.Executes(async () =>
Expand All @@ -20,10 +17,10 @@ partial class Build : NukeBuild
Credentials = new Credentials(this.GithubToken)
};

NewRelease release = new(GitVersion.MajorMinorPatch)
NewRelease release = new(this.VersionTag)
{
Name = GitVersion.MajorMinorPatch,
Prerelease = this.PreRelease,
Name = this.VersionTag,
Prerelease = !this.IsReleaseBuild,
Draft = false,
GenerateReleaseNotes = true,
MakeLatest = MakeLatestQualifier.True,
Expand Down
2 changes: 1 addition & 1 deletion .build/Build.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ partial class Build : NukeBuild
readonly bool IncludeIntegrationTests = false;

Target Test => _ => _
.Description("Runs test suites within the build tree")
.Description("Runs test suites within the build tree.\n")
.DependsOn(Compile)
.Executes(() =>
DotNetTasks.DotNetTest(c => c
Expand Down
12 changes: 11 additions & 1 deletion .build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ partial class Build : NukeBuild
? Configuration.Debug
: Configuration.Release;

[Parameter]
[Secret]
[Parameter("GitHub Token for pushing Docker images to GHCR")]
readonly string GithubToken;

protected override void OnBuildInitialized()
{
Serilog.Log.Information("🔥 Build process started");
Serilog.Log.Information(" Repository: {Repository}", this.Repository.HttpsUrl);
Serilog.Log.Information(" Version: {Version}", this.VersionTag);
Serilog.Log.Information(" Tags: {VersionTags}", this.VersionTags);
Serilog.Log.Information(" IsRelease: {IsReleaseBuild}", this.IsReleaseBuild);

if(this.GitHubActions is not null)
{
Serilog.Log.Information(" Branch: {BranchName}", this.GitHubActions.Ref);
Serilog.Log.Information(" Commit: {CommitSha}", this.GitHubActions.Sha);
}

base.OnBuildInitialized();
}
Expand Down
12 changes: 8 additions & 4 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"GithubToken": {
"type": "string",
"description": "GitHub Token for pushing Docker images to GHCR",
"default": "Secrets must be entered via 'nuke :secrets [profile]'"
},
"Help": {
Expand Down Expand Up @@ -47,6 +48,13 @@
"VSCode"
]
},
"ImagePlatforms": {
"type": "array",
"description": "List of platforms to build the Docker image for",
"items": {
"type": "string"
}
},
"IncludeIntegrationTests": {
"type": "boolean",
"description": "Whether to include integration tests (default: false)"
Expand All @@ -63,10 +71,6 @@
"type": "boolean",
"description": "Shows the execution plan (HTML)"
},
"PreRelease": {
"type": "boolean",
"description": "Whether the release is a pre-release or not"
},
"Profile": {
"type": "array",
"description": "Defines the profiles to load",
Expand Down

0 comments on commit a847f81

Please sign in to comment.