Skip to content

Commit

Permalink
refactor(test): Require GithubToken only when pushing image
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnatamo committed Aug 2, 2024
1 parent eea9f4b commit d227af9
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .build/Build.Clean.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using JetBrains.Annotations;

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

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

Target Compile => _ => _
.Description("Compiles the entire build tree")
.DependsOn(Restore)
.Executes(() =>
DotNetTasks.DotNetBuild(c => c
Expand Down
20 changes: 18 additions & 2 deletions .build/Build.Docker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

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)
Expand All @@ -18,17 +21,30 @@ partial class Build : NukeBuild

private string[] DockerImagePlatforms => ["linux/amd64", "linux/arm", "linux/arm64"];

Target AssertDockerPush => _ => _
.Unlisted()
.Description("Asserts whether the built Docker image can be pushed.")
.Before(Restore)
.Executes(() =>
{
if(this.PushImage && string.IsNullOrEmpty(this.GithubToken))
{
Assert.Fail("Cannot push Docker image, when GitHub token is not set.");
}
});

Target BuildImage => _ => _
.Description("Builds the Docker image of Fetcharr, and optionally pushes it to GHCR.")
.DependsOn(AssertDockerPush)
.DependsOn(Test)
.DependsOn(Format)
.Requires(() => this.GithubToken)
.Executes(() =>
DockerBuildxBuild(x => x
.SetPath(".")
.SetFile("Dockerfile")
.SetTag(this.DockerImageTags)
.SetPlatform(string.Join(",", this.DockerImagePlatforms))
.SetPush(true)
.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")
Expand Down
1 change: 1 addition & 0 deletions .build/Build.Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
partial class Build : NukeBuild
{
Target Format => _ => _
.Description("Performs linting on the build tree")
.DependsOn(Restore)
.Executes(() =>
DotNetFormat(c => c
Expand Down
1 change: 1 addition & 0 deletions .build/Build.Release.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ partial class Build : NukeBuild
readonly bool PreRelease;

Target Release => _ => _
.Description("Creates and pushes a new release to GitHub")
.DependsOn(BuildImage)
.Requires(() => this.GithubToken)
.Executes(async () =>
Expand Down
1 change: 1 addition & 0 deletions .build/Build.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ partial class Build : NukeBuild
readonly bool IncludeIntegrationTests = false;

Target Test => _ => _
.Description("Runs test suites within the build tree")
.DependsOn(Compile)
.Executes(() =>
DotNetTasks.DotNetTest(c => c
Expand Down
8 changes: 2 additions & 6 deletions .build/Build.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using Nuke.Common;
using Nuke.Common.Execution;

[UnsetVisualStudioEnvironmentVariables]
partial class Build : NukeBuild
{
/// Support plugins are available for:
/// - JetBrains ReSharper https://nuke.build/resharper
/// - JetBrains Rider https://nuke.build/rider
/// - Microsoft VisualStudio https://nuke.build/visualstudio
/// - Microsoft VSCode https://nuke.build/vscode

public static int Main() => Execute<Build>(x => x.Compile);

[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ jobs:

- name: Push development image
if: ${{ github.ref == 'refs/heads/develop' }}
run: ./build.cmd BuildImage
run: ./build.cmd BuildImage --include-integration-tests --push-image
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6 changes: 6 additions & 0 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"type": "string"
}
},
"PushImage": {
"type": "boolean",
"description": "Whether to push the built Docker image to GHCR"
},
"Root": {
"type": "string",
"description": "Root directory during build execution"
Expand All @@ -84,6 +88,7 @@
"items": {
"type": "string",
"enum": [
"AssertDockerPush",
"BuildImage",
"Clean",
"Compile",
Expand All @@ -100,6 +105,7 @@
"items": {
"type": "string",
"enum": [
"AssertDockerPush",
"BuildImage",
"Clean",
"Compile",
Expand Down

0 comments on commit d227af9

Please sign in to comment.