From 7a8738d4e34a972c4188bae70cb9e16aff37afb9 Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Sat, 26 Aug 2023 09:12:26 +0300 Subject: [PATCH] Improve package publish logic * include release* branches * read version from branch name * change output from artifacts to publish (artifacts will be used by NET 8 for binaries) --- .github/workflows/CI.yml | 13 +++---- .github/workflows/PR.yml | 12 +++---- build/Build.GitHubAction.cs | 3 +- build/Build.cs | 70 ++++++++++++++++++++++++++++++------- 4 files changed, 72 insertions(+), 26 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index bc4467de0..570747496 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -21,6 +21,7 @@ on: branches: - main - master + - 'release*' jobs: windows-latest: @@ -31,11 +32,11 @@ jobs: - uses: actions/checkout@v3 - name: 'Run: Clean, Test, Pack' run: ./build.cmd Clean Test Pack - - name: 'Publish: artifacts' + - name: 'Publish: publish' uses: actions/upload-artifact@v3 with: - name: artifacts - path: artifacts + name: publish + path: publish ubuntu-latest: name: ubuntu-latest runs-on: ubuntu-latest @@ -44,8 +45,8 @@ jobs: - uses: actions/checkout@v3 - name: 'Run: Clean, Test, Pack' run: ./build.cmd Clean Test Pack - - name: 'Publish: artifacts' + - name: 'Publish: publish' uses: actions/upload-artifact@v3 with: - name: artifacts - path: artifacts + name: publish + path: publish diff --git a/.github/workflows/PR.yml b/.github/workflows/PR.yml index 66b1df03b..588cac3ac 100644 --- a/.github/workflows/PR.yml +++ b/.github/workflows/PR.yml @@ -27,11 +27,11 @@ jobs: - uses: actions/checkout@v3 - name: 'Run: Clean, Test, Pack' run: ./build.cmd Clean Test Pack - - name: 'Publish: artifacts' + - name: 'Publish: publish' uses: actions/upload-artifact@v3 with: - name: artifacts - path: artifacts + name: publish + path: publish ubuntu-latest: name: ubuntu-latest runs-on: ubuntu-latest @@ -40,8 +40,8 @@ jobs: - uses: actions/checkout@v3 - name: 'Run: Clean, Test, Pack' run: ./build.cmd Clean Test Pack - - name: 'Publish: artifacts' + - name: 'Publish: publish' uses: actions/upload-artifact@v3 with: - name: artifacts - path: artifacts + name: publish + path: publish diff --git a/build/Build.GitHubAction.cs b/build/Build.GitHubAction.cs index 9f6a04398..de708aaf6 100644 --- a/build/Build.GitHubAction.cs +++ b/build/Build.GitHubAction.cs @@ -1,10 +1,9 @@ -using System; using Nuke.Common.CI.GitHubActions; [GitHubActions("CI", GitHubActionsImage.WindowsLatest, GitHubActionsImage.UbuntuLatest, - OnPushBranches = new[] { "main", "master" }, + OnPushBranches = new[] { "main", "master", "release*" }, InvokedTargets = new[] { nameof(Clean), nameof(Test), nameof(Pack) }, TimeoutMinutes = 20, CacheKeyFiles = new string[0] diff --git a/build/Build.cs b/build/Build.cs index c8b3c855c..f10232190 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -1,11 +1,15 @@ +using System; +using System.Linq; using System.Runtime.InteropServices; using Nuke.Common; using Nuke.Common.CI.GitHubActions; +using Nuke.Common.Git; using Nuke.Common.IO; using Nuke.Common.ProjectModel; using Nuke.Common.Tooling; using Nuke.Common.Tools.DotNet; using Nuke.Common.Utilities.Collections; + using static Nuke.Common.Tools.DotNet.DotNetTasks; partial class Build : NukeBuild @@ -22,10 +26,26 @@ partial class Build : NukeBuild readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release; [Solution] Solution Solution; + [GitRepository] readonly GitRepository GitRepository; static AbsolutePath SourceDirectory => RootDirectory / "src"; - static AbsolutePath ArtifactsDirectory => RootDirectory / "artifacts"; + static AbsolutePath ArtifactsDirectory => RootDirectory / "publish"; + + string TagVersion => GitRepository.Tags.SingleOrDefault(x => x.StartsWith("v"))?[1..]; + + string BranchVersion => GitRepository.Branch?.StartsWith("release") == true + ? GitRepository.Branch[7..] + : null; + + // either from tag or branch + string PublishVersion => TagVersion ?? BranchVersion; + + bool IsPublishBuild => !string.IsNullOrWhiteSpace(PublishVersion); + + string VersionSuffix; + + static bool IsRunningOnWindows => RuntimeInformation.IsOSPlatform(OSPlatform.Windows); [Secret] [Parameter("GitHub API token")] @@ -33,9 +53,20 @@ partial class Build : NukeBuild protected override void OnBuildInitialized() { + VersionSuffix = !IsPublishBuild + ? $"preview-{DateTime.UtcNow:yyyyMMdd-HHmm}" + : ""; + + if (IsLocalBuild) + { + VersionSuffix = $"dev-{DateTime.UtcNow:yyyyMMdd-HHmm}"; + } + Serilog.Log.Information("BUILD SETUP"); Serilog.Log.Information("\tSolution: {Solution}", Solution); Serilog.Log.Information("\tConfiguration: {Configuration}", Configuration); + Serilog.Log.Information("\tVersion suffix: {VersionSuffix}", VersionSuffix); + Serilog.Log.Information("\tPublish version: {PublishVersion}", PublishVersion); Serilog.Log.Information("Build environment:"); Serilog.Log.Information("\tHost: {Host}", Host.GetType()); @@ -120,16 +151,31 @@ static void DeleteCompilationArtifacts() var packTarget = Solution.GetProject("NPOI.Pack"); - DotNetPack(_ =>_ - .SetConfiguration(Configuration) - .SetOutputDirectory(ArtifactsDirectory) - .SetDeterministic(IsServerBuild) - .SetContinuousIntegrationBuild(IsServerBuild) - // obsolete missing XML documentation comment, XML comment on not valid language element, XML comment has badly formed XML, no matching tag in XML comment - // need to use escaped separator in order for this to work - .AddProperty("NoWarn", string.Join("%3B", new [] { 169, 612, 618, 1591, 1587, 1570, 1572, 1573, 1574 })) - .SetProperty("EnablePackageValidation", "false") - .SetProject(packTarget) - ); + DotNetPack(_ => + { + var packSettings = _ + .SetProject(packTarget) + .SetConfiguration(Configuration) + .SetOutputDirectory(ArtifactsDirectory) + .SetDeterministic(IsServerBuild) + .SetContinuousIntegrationBuild(IsServerBuild) + // obsolete missing XML documentation comment, XML comment on not valid language element, XML comment has badly formed XML, no matching tag in XML comment + // need to use escaped separator in order for this to work + .AddProperty("NoWarn", string.Join("%3B", new[] { 169, 612, 618, 1591, 1587, 1570, 1572, 1573, 1574 })) + .SetProperty("EnablePackageValidation", "false"); + + if (IsPublishBuild) + { + // force version from tag/branch + packSettings = packSettings + .SetAssemblyVersion(PublishVersion) + .SetFileVersion(PublishVersion) + .SetInformationalVersion(PublishVersion) + .SetVersionSuffix(VersionSuffix) + .SetVersionPrefix(PublishVersion); + } + + return packSettings; + }); }); }