From 70803eb3e321829eeef29c94074ae9f9bf162ce7 Mon Sep 17 00:00:00 2001 From: "harry.cpp" Date: Mon, 19 Aug 2024 16:29:27 +0200 Subject: [PATCH] [CI] Improve versioning and allow upload to nuget.org --- .github/workflows/main.yml | 16 +++++++- build/Build.csproj | 1 + build/BuildContext.cs | 39 ++++++++++++++++--- .../DeployTasks/DeployNuGetsToNuGetOrgTask.cs | 31 +++++++++++++++ build/Tasks.cs | 5 +++ 5 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 build/DeployTasks/DeployNuGetsToNuGetOrgTask.cs diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d55f74a4421..3dc9f9688e5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -72,7 +72,19 @@ jobs: - name: Expose GitHub Runtime uses: crazy-max/ghaction-github-runtime@v3 - - name: Push GitHub Nugets - run: dotnet run --project build/Build.csproj -- --target=DeployNuGetsToGithub + - name: Push Nugets + run: dotnet run --project build/Build.csproj -- --target=Deploy env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }} + + - name: Make a Release + if: github.ref_type == 'tag' + uses: ncipollo/release-action@v1 + with: + name: 'MonoGame ${{ github.ref_name }}' + tag: ${{ github.ref_name }} + allowUpdates: true + removeArtifacts: true + artifacts: "artifacts/**/*.nupkg" + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/build/Build.csproj b/build/Build.csproj index 94bc6d75ddf..c421510786d 100644 --- a/build/Build.csproj +++ b/build/Build.csproj @@ -32,6 +32,7 @@ + diff --git a/build/BuildContext.cs b/build/BuildContext.cs index 0c6d46b8e3e..0156804889a 100644 --- a/build/BuildContext.cs +++ b/build/BuildContext.cs @@ -1,3 +1,6 @@ +using Cake.Git; +using Microsoft.VisualBasic; +using System.Text.RegularExpressions; namespace BuildScripts; @@ -13,35 +16,61 @@ public enum ProjectType public class BuildContext : FrostingContext { + public static string VersionBase = "1.0.0"; + public static readonly Regex VersionRegex = new(@"^v\d+.\d+.\d+", RegexOptions.Compiled | RegexOptions.IgnoreCase); public static readonly string DefaultRepositoryUrl = "https://github.com/MonoGame/MonoGame"; - public static readonly string DefaultBaseVersion = "3.8.2"; public BuildContext(ICakeContext context) : base(context) { var repositoryUrl = context.Argument("build-repository", DefaultRepositoryUrl); var buildConfiguration = context.Argument("build-configuration", "Release"); BuildOutput = context.Argument("build-output", "artifacts"); - Version = context.Argument("build-version", DefaultBaseVersion + ".1-develop"); NuGetsDirectory = $"{BuildOutput}/NuGet/"; + var tags = GitAliases.GitTags(context, "."); + foreach (var tag in tags) + { + if (VersionRegex.IsMatch(tag.FriendlyName)) + { + VersionBase = tag.FriendlyName[1..]; + } + } + if (context.BuildSystem().IsRunningOnGitHubActions) { var workflow = context.BuildSystem().GitHubActions.Environment.Workflow; repositoryUrl = $"https://github.com/{workflow.Repository}"; + if (workflow.RefType == GitHubActionsRefType.Tag) + { + var baseVersion = workflow.RefName; + if (Regex.IsMatch(baseVersion, @"v\d+.\d+.\d+")) + { + VersionBase = baseVersion[1..]; + } + else + { + throw new Exception($"Invalid tag: {baseVersion}"); + } + } + if (workflow.Repository != "MonoGame/MonoGame") { - Version = $"{DefaultBaseVersion}.{workflow.RunNumber}-{workflow.RepositoryOwner}"; + Version = $"{VersionBase}.{workflow.RunNumber}-{workflow.RepositoryOwner}"; } else if (workflow.RefType == GitHubActionsRefType.Branch && workflow.RefName != "refs/heads/master") { - Version = $"{DefaultBaseVersion}.{workflow.RunNumber}-develop"; + Version = $"{VersionBase}.{workflow.RunNumber}-develop"; } else { - Version = $"{DefaultBaseVersion}.{workflow.RunNumber}"; + Version = $"{VersionBase}.{workflow.RunNumber}"; } } + else + { + Version = context.Argument("build-version", VersionBase + ".1-develop"); + } DotNetMSBuildSettings = new DotNetMSBuildSettings(); DotNetMSBuildSettings.WithProperty("Version", Version); diff --git a/build/DeployTasks/DeployNuGetsToNuGetOrgTask.cs b/build/DeployTasks/DeployNuGetsToNuGetOrgTask.cs new file mode 100644 index 00000000000..e45de9d8e31 --- /dev/null +++ b/build/DeployTasks/DeployNuGetsToNuGetOrgTask.cs @@ -0,0 +1,31 @@ + +namespace BuildScripts; + +[TaskName("DeployNuGetsToNuGetOrgTask")] +[IsDependentOn(typeof(DownloadArtifactsTask))] +public sealed class DeployNuGetsToNuGetOrgTask : FrostingTask +{ + public override bool ShouldRun(BuildContext context) + { + if (context.BuildSystem().IsRunningOnGitHubActions) + { + var workflow = context.BuildSystem().GitHubActions.Environment.Workflow; + if (workflow.RefType == GitHubActionsRefType.Tag && + string.IsNullOrWhiteSpace(context.EnvironmentVariable("NUGET_API_KEY"))) + { + return true; + } + } + + return false; + } + + public override void Run(BuildContext context) + { + context.DotNetNuGetPush($"nugets/*.nupkg", new() + { + ApiKey = context.EnvironmentVariable("NUGET_API_KEY"), + Source = $"https://api.nuget.org/v3/index.json" + }); + } +} diff --git a/build/Tasks.cs b/build/Tasks.cs index 1efd62f8c0a..fc74aa211a3 100644 --- a/build/Tasks.cs +++ b/build/Tasks.cs @@ -29,6 +29,11 @@ public sealed class BuildTemplatesTask : FrostingTask { } [IsDependentOn(typeof(BuildTemplatesTask))] public sealed class BuildAllTask : FrostingTask { } +[TaskName("Deploy")] +[IsDependentOn(typeof(DeployNuGetsToGitHubTask))] +[IsDependentOn(typeof(DeployNuGetsToNuGetOrgTask))] +public sealed class DeployTask : FrostingTask { } + [TaskName("Default")] [IsDependentOn(typeof(BuildAllTask))] public sealed class DefaultTask : FrostingTask { }