continuity/build/Build.cs

116 lines
3.7 KiB
C#
Raw Normal View History

2023-10-08 10:47:30 +00:00
using Nuke.Common;
using Nuke.Common.Git;
2023-10-08 10:47:30 +00:00
using Nuke.Common.IO;
using Nuke.Common.Tools.Docker;
using Nuke.Common.Tools.DotNet;
using Nuke.Common.Tools.SonarScanner;
using Serilog;
2023-10-08 10:47:30 +00:00
2023-10-27 18:19:37 +00:00
sealed class Build : NukeBuild
2023-10-08 10:47:30 +00:00
{
/// 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.Information, x => x.Deploy);
2023-10-08 10:47:30 +00:00
[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;
2023-10-27 18:19:37 +00:00
[Parameter] readonly bool SupportsDocker = true;
2023-10-27 17:47:17 +00:00
[Parameter] readonly string SonarToken;
[Parameter] readonly string SonarHostUrl;
GitRepository GitRepository => GitRepository.FromLocalDirectory(RootDirectory);
Target Information => _ => _
.Before(Clean)
.Executes(() =>
{
Log.Information("Branch Name: {BranchName}", GitRepository.Branch);
Log.Information("Commit: {Commit}", GitRepository.Commit);
});
2023-10-08 10:47:30 +00:00
Target Clean => _ => _
.Before(Restore)
2023-10-12 07:15:34 +00:00
.Executes(() => DotNetTasks.DotNetClean());
2023-10-08 10:47:30 +00:00
Target Restore => _ => _
2023-10-12 07:15:34 +00:00
.Executes(() => DotNetTasks.DotNetRestore());
2023-10-08 10:47:30 +00:00
Target Compile => _ => _
.DependsOn(Clean)
2023-10-08 10:47:30 +00:00
.DependsOn(Restore)
.Executes(() =>
{
2023-10-27 18:32:46 +00:00
if (IsLocalBuild && SupportsDocker)
2023-10-27 18:23:06 +00:00
{
2023-10-27 18:23:06 +00:00
DockerTasks.DockerStackRm(settings => settings.SetStacks("continuity"));
2023-10-27 18:23:06 +00:00
}
DotNetTasks.DotNetBuild(settings => settings.SetNoRestore(true));
2023-10-08 10:47:30 +00:00
});
Target Pack => _ => _
2023-10-27 17:47:17 +00:00
.DependsOn(SonarScan)
.Executes(() =>
{
2023-10-27 18:23:06 +00:00
if (!SupportsDocker)
{
return;
}
DockerTasks.DockerBuild(settings => settings
.SetPath(AbsolutePath.Create(RootDirectory))
.SetFile("Server/Dockerfile")
.SetTag("continuity-server:latest")
.SetCompress(true)
.SetProgress(ProgressType.auto));
if (!IsLocalBuild)
{
2023-10-27 17:47:17 +00:00
//DockerTasks.DockerPush(settings => settings.SetName("continuity-server:latest"));
}
});
2023-10-27 17:47:17 +00:00
Target SonarScan => _ => _.DependsOn(Compile).Executes(() =>
{
if (IsLocalBuild)
{
return;
}
2023-10-27 17:58:27 +00:00
2023-10-27 17:47:17 +00:00
var sonarScannerSettings = new SonarScannerBeginSettings()
2023-10-27 19:01:16 +00:00
.SetProjectKey("wonderking_continuity_AYeecUUTs-PH__JrTRky")
2023-10-27 18:49:06 +00:00
.SetLogin(SonarToken)
2023-10-27 17:47:17 +00:00
.SetServer(SonarHostUrl);
2023-10-27 19:15:36 +00:00
var scannerBegin = SonarScannerTasks.SonarScannerBegin(sonarScannerSettings);
2023-10-27 18:42:53 +00:00
DotNetTasks.DotNetBuild(settings => settings.SetNoRestore(true));
2023-10-27 19:15:36 +00:00
SonarScannerTasks.SonarScannerEnd(scannerBegin);
2023-10-27 17:58:27 +00:00
});
2023-10-27 17:47:17 +00:00
Target Deploy => _ => _.DependsOn(Pack).Executes(() =>
{
2023-10-27 18:23:06 +00:00
if (!SupportsDocker)
{
return;
}
DockerTasks.DockerPull(settings => settings.SetName("postgres:16.0-alpine"));
if (IsLocalBuild)
{
var dockerComposeFile = AbsolutePath.Create(RootDirectory / "Server" / "docker-compose.yml");
DockerTasks.DockerStackDeploy(settings => settings
.AddComposeFile(dockerComposeFile)
.SetStack("continuity")
.SetPrune(true)
);
return;
}
Log.Error("Deploy is only available on local builds.");
});
2023-10-08 10:47:30 +00:00
}