Fix tag build

This commit is contained in:
Peter 2022-01-27 11:03:21 +01:00
parent 752bd9d8f5
commit c564229fb2
Signed by: prskr
GPG key ID: C1DB5D2E8DB512F9
3 changed files with 31 additions and 27 deletions

View file

@ -56,7 +56,6 @@ nuget-publish:
only: only:
refs: refs:
- tags - tags
- main
script: script:
- dotnet tool restore - dotnet tool restore
- dotnet nuke NuGetPush - dotnet nuke NuGetPush --nuget-username "${NUGET_USERNAME}" --nuget-password "${NUGET_PASSWORD}"

View file

@ -1,5 +1,6 @@
using System; using System;
using JetBrains.Annotations; using JetBrains.Annotations;
using NuGet.Versioning;
using Nuke.Common; using Nuke.Common;
using Nuke.Common.CI; using Nuke.Common.CI;
using Nuke.Common.CI.GitLab; using Nuke.Common.CI.GitLab;
@ -25,18 +26,17 @@ class Build : NukeBuild
readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release; readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;
[Parameter("Username to use for publishing NuGet packages", Name = "nuget-username")] [Parameter("Username to use for publishing NuGet packages", Name = "nuget-username")]
string NuGetUsername { get; } = Environment.GetEnvironmentVariable("NUGET_USERNAME") ?? string.Empty; string NuGetUsername { get; set; } = Environment.GetEnvironmentVariable("NUGET_USERNAME") ?? string.Empty;
[Parameter("Password to use for publishing NuGet packages", Name = "nuget-password")] [Parameter("Password to use for publishing NuGet packages", Name = "nuget-password")]
string NuGetPassword { get; } = Environment.GetEnvironmentVariable("NUGET_PASSWORD") ?? string.Empty; string NuGetPassword { get; set; } = Environment.GetEnvironmentVariable("NUGET_PASSWORD") ?? string.Empty;
[Solution] readonly Solution Solution; [Solution] readonly Solution? Solution;
[GitRepository] readonly GitRepository GitRepository; [GitRepository] readonly GitRepository? GitRepository;
[GitVersion(NoFetch = true)] readonly GitVersion GitVersion; [GitVersion(NoFetch = true, Framework = "net6.0")] readonly GitVersion? GitVersion;
[CanBeNull] GitLab? CI => GitLab.Instance;
GitLab CI => GitLab.Instance;
AbsolutePath SourceDirectory => RootDirectory / "src"; AbsolutePath SourceDirectory => RootDirectory / "src";
AbsolutePath TestsDirectory => RootDirectory / "tests"; AbsolutePath TestsDirectory => RootDirectory / "tests";
@ -62,12 +62,17 @@ class Build : NukeBuild
.DependsOn(Restore) .DependsOn(Restore)
.Executes(() => .Executes(() =>
{ {
var assemblyVersion = GitVersion?.AssemblySemVer ??
SemanticVersion.Parse(CI?.CommitTag?.TrimStart('v') ?? "0.0.1").ToNormalizedString();
var informationalVersion = GitVersion?.InformationalVersion ??
SemanticVersion.Parse(CI?.CommitTag?.TrimStart('v') ?? "0.0.1").ToFullString();
DotNetBuild(s => s DotNetBuild(s => s
.SetProjectFile(Solution) .SetProjectFile(Solution)
.SetConfiguration(Configuration) .SetConfiguration(Configuration)
.SetAssemblyVersion(GitVersion.AssemblySemVer) .SetAssemblyVersion(assemblyVersion)
.SetFileVersion(GitVersion.AssemblySemFileVer) .SetFileVersion(assemblyVersion)
.SetInformationalVersion(GitVersion.InformationalVersion) .SetInformationalVersion(informationalVersion)
.EnableNoRestore()); .EnableNoRestore());
}); });
@ -84,28 +89,16 @@ class Build : NukeBuild
.EnableNoBuild() .EnableNoBuild()
.EnableProcessLogOutput())); .EnableProcessLogOutput()));
Target AddNugetSource => _ => _
.OnlyWhenStatic(() => GitRepository.IsOnMainBranch())
.OnlyWhenStatic(() => CI != null)
.OnlyWhenStatic(() => !string.IsNullOrEmpty(NuGetUsername) && !string.IsNullOrEmpty(NuGetPassword))
.ProceedAfterFailure()
.Executes(() => DotNetNuGetAddSource(s => s
.SetName(NuGetSourceName)
.SetSource($"https://gitlab.com/api/v4/projects/{CI.ProjectId}/packages/nuget/index.json")
.SetUsername(NuGetUsername)
.SetPassword(NuGetPassword)
.EnableStorePasswordInClearText()));
Target Pack => _ => _ Target Pack => _ => _
.DependsOn(Compile) .DependsOn(Compile)
.OnlyWhenStatic(() => GitRepository.IsOnMainBranch()) .OnlyWhenStatic(() => CI != null && !string.IsNullOrEmpty(CI.CommitTag))
.Executes(() => SourceDirectory .Executes(() => SourceDirectory
.GlobFiles("**/*.csproj") .GlobFiles("**/*.csproj")
.ForEach(csproj => DotNetPack(s => s .ForEach(csproj => DotNetPack(s => s
.SetProject(csproj) .SetProject(csproj)
.SetConfiguration(Configuration) .SetConfiguration(Configuration)
.SetOutputDirectory(ArtifactsDirectory) .SetOutputDirectory(ArtifactsDirectory)
.SetVersion(GitVersion.FullSemVer) .SetVersion(GitVersion?.FullSemVer ?? SemanticVersion.Parse(CI?.CommitTag?.TrimStart('v') ?? "0.0.1").ToNormalizedString())
.EnableIncludeSource() .EnableIncludeSource()
.EnableIncludeSymbols() .EnableIncludeSymbols()
.EnableNoRestore() .EnableNoRestore()
@ -113,9 +106,20 @@ class Build : NukeBuild
.EnableProcessLogOutput() .EnableProcessLogOutput()
))); )));
Target AddNugetSource => _ => _
.OnlyWhenStatic(() => CI != null && !string.IsNullOrEmpty(CI.CommitTag))
.OnlyWhenStatic(() => !string.IsNullOrEmpty(NuGetUsername) && !string.IsNullOrEmpty(NuGetPassword))
.ProceedAfterFailure()
.Executes(() => DotNetNuGetAddSource(s => s
.SetName(NuGetSourceName)
.SetSource($"https://gitlab.com/api/v4/projects/{CI?.ProjectId}/packages/nuget/index.json")
.SetUsername(NuGetUsername)
.SetPassword(NuGetPassword)
.EnableStorePasswordInClearText()));
Target NuGetPush => _ => _ Target NuGetPush => _ => _
.DependsOn(Pack, AddNugetSource) .DependsOn(Pack, AddNugetSource)
.OnlyWhenStatic(() => GitRepository.IsOnMainBranch()) .OnlyWhenStatic(() => CI != null && !string.IsNullOrEmpty(CI.CommitTag))
.Executes(() => ArtifactsDirectory .Executes(() => ArtifactsDirectory
.GlobFiles("**/*.nupkg") .GlobFiles("**/*.nupkg")
.ForEach(nupkg => DotNetNuGetPush(s => s .ForEach(nupkg => DotNetNuGetPush(s => s

View file

@ -9,6 +9,7 @@
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<NukeTelemetryVersion>1</NukeTelemetryVersion> <NukeTelemetryVersion>1</NukeTelemetryVersion>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>