Skip to content

Commit

Permalink
[CI] Improve versioning and allow upload to nuget.org (MonoGame#8447)
Browse files Browse the repository at this point in the history
* [CI] Improve versioning and allow upload to nuget.org

* [CI] Take president with using tag as version

* [CI] Quick tag check fix

* [CI] Skip picking up build number and fully use the tag version
  • Loading branch information
harry-cpp authored Aug 20, 2024
1 parent aa2f088 commit 39313ed
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 7 deletions.
16 changes: 14 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: "nugets/*.nupkg"
token: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions build/Build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<ItemGroup>
<PackageReference Include="Cake.FileHelpers" Version="7.0.0" />
<PackageReference Include="Cake.Frosting" Version="4.0.0" />
<PackageReference Include="Cake.Git" Version="4.0.0" />
<PackageReference Include="NuGet.Packaging" Version="6.10.1" />
<PackageReference Include="System.Formats.Asn1" Version="8.0.1" />
</ItemGroup>
Expand Down
35 changes: 30 additions & 5 deletions build/BuildContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using Cake.Git;
using Microsoft.VisualBasic;
using System.Text.RegularExpressions;

namespace BuildScripts;

Expand All @@ -13,35 +16,57 @@ 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.Repository != "MonoGame/MonoGame")
{
Version = $"{DefaultBaseVersion}.{workflow.RunNumber}-{workflow.RepositoryOwner}";
Version = $"{VersionBase}.{workflow.RunNumber}-{workflow.RepositoryOwner}";
}
else if (workflow.RefType == GitHubActionsRefType.Tag)
{
var baseVersion = workflow.RefName.Split('/')[^1];
if (!VersionRegex.IsMatch(baseVersion))
throw new Exception($"Invalid tag: {baseVersion}");

VersionBase = baseVersion[1..];
Version = VersionBase;
}
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);
Expand Down
31 changes: 31 additions & 0 deletions build/DeployTasks/DeployNuGetsToNuGetOrgTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

namespace BuildScripts;

[TaskName("DeployNuGetsToNuGetOrgTask")]
[IsDependentOn(typeof(DownloadArtifactsTask))]
public sealed class DeployNuGetsToNuGetOrgTask : FrostingTask<BuildContext>
{
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"
});
}
}
5 changes: 5 additions & 0 deletions build/Tasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public sealed class BuildTemplatesTask : FrostingTask<BuildContext> { }
[IsDependentOn(typeof(BuildTemplatesTask))]
public sealed class BuildAllTask : FrostingTask<BuildContext> { }

[TaskName("Deploy")]
[IsDependentOn(typeof(DeployNuGetsToGitHubTask))]
[IsDependentOn(typeof(DeployNuGetsToNuGetOrgTask))]
public sealed class DeployTask : FrostingTask<BuildContext> { }

[TaskName("Default")]
[IsDependentOn(typeof(BuildAllTask))]
public sealed class DefaultTask : FrostingTask<BuildContext> { }

0 comments on commit 39313ed

Please sign in to comment.