client-dotnet/build/Build.cs

138 lines
4.8 KiB
C#
Raw Normal View History

using System.IO;
2021-02-22 12:54:22 +00:00
using JetBrains.Annotations;
using Nuke.Common;
using Nuke.Common.CI;
2021-02-22 12:54:22 +00:00
using Nuke.Common.CI.GitLab;
using Nuke.Common.Execution;
using Nuke.Common.Git;
using Nuke.Common.IO;
using Nuke.Common.ProjectModel;
using Nuke.Common.Tooling;
using Nuke.Common.Tools.Docker;
using Nuke.Common.Tools.DotNet;
using Nuke.Common.Tools.GitVersion;
using Nuke.Common.Utilities.Collections;
using static Nuke.Common.IO.FileSystemTasks;
using static Nuke.Common.Tools.DotNet.DotNetTasks;
[CheckBuildProjectConfigurations]
[ShutdownDotNetAfterServerBuild]
class Build : NukeBuild
{
2021-02-22 14:10:34 +00:00
private const string NuGetSourceName = "GitLab";
public static int Main() => Execute<Build>(x => x.Compile);
[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;
[Solution] readonly Solution Solution;
[GitRepository] readonly GitRepository GitRepository;
2021-02-16 09:01:08 +00:00
[GitVersion(NoFetch = true, Framework = "net5.0")] readonly GitVersion GitVersion;
2021-02-22 14:10:34 +00:00
[CanBeNull] GitLab CI => GitLab.Instance;
2021-02-22 12:54:22 +00:00
AbsolutePath SourceDirectory => RootDirectory / "src";
AbsolutePath TestsDirectory => RootDirectory / "tests";
AbsolutePath ArtifactsDirectory => RootDirectory / "artifacts";
2021-02-22 14:10:34 +00:00
2021-02-22 12:54:22 +00:00
Target PrintEnv => _ => _
.Executes(() =>
{
if (CI == null)
{
Logger.Info("Running in local environment");
Logger.Info($"Git commit: {GitRepository.Commit}");
}
else
{
Logger.Info("Running in GitLab CI");
Logger.Info($"Git commit: {CI.CommitSha}");
Logger.Info($"Pipeline ID: {CI.PipelineId}");
}
});
Target Clean => _ => _
.Before(Restore)
.Executes(() =>
{
SourceDirectory.GlobDirectories("**/bin", "**/obj").ForEach(DeleteDirectory);
TestsDirectory.GlobDirectories("**/bin", "**/obj").ForEach(DeleteDirectory);
EnsureCleanDirectory(ArtifactsDirectory);
});
Target Restore => _ => _
.Executes(() =>
{
DotNetRestore(s => s
.SetProjectFile(Solution));
});
Target Compile => _ => _
2021-02-22 12:54:22 +00:00
.DependsOn(Restore, PrintEnv)
.Executes(() =>
{
DotNetBuild(s => s
.SetProjectFile(Solution)
.SetConfiguration(Configuration)
.SetAssemblyVersion(GitVersion.AssemblySemVer)
.SetFileVersion(GitVersion.AssemblySemFileVer)
.SetInformationalVersion(GitVersion.InformationalVersion)
.EnableNoRestore());
});
Target IntegrationTestImage => _ => _
.Executes(() => DockerTasks.DockerBuild(s => s
.SetFile(Path.Join("assets", "integration-tests.dockerfile"))
.SetTag("inetmock-root")
.SetPath(".")));
Target Test => _ => _
.DependsOn(Compile, IntegrationTestImage)
.Executes(() => DotNetTest(s => s
.SetProjectFile(Solution)
.SetConfiguration(Configuration)
.EnableNoRestore()
.EnableNoBuild()
.EnableProcessLogOutput()));
2021-02-22 14:10:34 +00:00
Target AddNugetSource => _ => _
.OnlyWhenStatic(() => GitRepository.IsOnMasterBranch())
.OnlyWhenStatic(() => CI != null)
2021-02-22 15:26:04 +00:00
.ProceedAfterFailure()
2021-02-22 14:10:34 +00:00
.Executes(() => DotNetNuGetAddSource(s => s
.SetName(NuGetSourceName)
2021-02-22 15:26:04 +00:00
//.SetSource($"https://gitlab.com/api/v4/projects/{CI.ProjectId}/packages/nuget/index.json")
.SetSource($"https://gitlab.com/api/v4/projects/24385200/packages/nuget/index.json")
.SetUsername("baez90")
.SetPassword("RcMwfaXgvBxSWt4ZMB6z")
2021-02-22 14:10:34 +00:00
.EnableStorePasswordInClearText()));
Target Pack => _ => _
.DependsOn(Test)
.OnlyWhenStatic(() => GitRepository.IsOnMasterBranch())
.Executes(() => SourceDirectory
.GlobFiles("**/*.csproj")
.ForEach(csproj => DotNetPack(s => s
.SetProject(csproj)
.SetConfiguration(Configuration)
.SetOutputDirectory(ArtifactsDirectory)
.SetVersion(GitVersion.FullSemVer)
.EnableIncludeSource()
.EnableIncludeSymbols()
.EnableNoRestore()
.EnableNoBuild()
.EnableProcessLogOutput()
)));
2021-02-22 14:10:34 +00:00
2021-02-22 15:26:04 +00:00
Target NuGetPush => _ => _
2021-02-22 14:10:34 +00:00
.DependsOn(Pack, AddNugetSource)
2021-02-22 15:26:04 +00:00
.OnlyWhenStatic(() => GitRepository.IsOnMasterBranch())
.Executes(() => ArtifactsDirectory
.GlobFiles("**/*.nupkg")
.ForEach(nupkg => DotNetNuGetPush(s => s
.SetSource(NuGetSourceName)
.SetTargetPath(nupkg)
.EnableProcessLogOutput())));
}