Skip to content

Commit d6cd6f6

Browse files
committed
[CI] Improve versioning and allow upload to nuget.org
1 parent aa2f088 commit d6cd6f6

File tree

5 files changed

+85
-7
lines changed

5 files changed

+85
-7
lines changed

.github/workflows/main.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,19 @@ jobs:
7272
- name: Expose GitHub Runtime
7373
uses: crazy-max/ghaction-github-runtime@v3
7474

75-
- name: Push GitHub Nugets
76-
run: dotnet run --project build/Build.csproj -- --target=DeployNuGetsToGithub
75+
- name: Push Nugets
76+
run: dotnet run --project build/Build.csproj -- --target=Deploy
7777
env:
7878
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
79+
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
80+
81+
- name: Make a Release
82+
if: github.ref_type == 'tag'
83+
uses: ncipollo/release-action@v1
84+
with:
85+
name: 'MonoGame ${{ github.ref_name }}'
86+
tag: ${{ github.ref_name }}
87+
allowUpdates: true
88+
removeArtifacts: true
89+
artifacts: "artifacts/**/*.nupkg"
90+
token: ${{ secrets.GITHUB_TOKEN }}

build/Build.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<ItemGroup>
3333
<PackageReference Include="Cake.FileHelpers" Version="7.0.0" />
3434
<PackageReference Include="Cake.Frosting" Version="4.0.0" />
35+
<PackageReference Include="Cake.Git" Version="4.0.0" />
3536
<PackageReference Include="NuGet.Packaging" Version="6.10.1" />
3637
<PackageReference Include="System.Formats.Asn1" Version="8.0.1" />
3738
</ItemGroup>

build/BuildContext.cs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using Cake.Git;
2+
using Microsoft.VisualBasic;
3+
using System.Text.RegularExpressions;
14

25
namespace BuildScripts;
36

@@ -13,35 +16,61 @@ public enum ProjectType
1316

1417
public class BuildContext : FrostingContext
1518
{
19+
public static string VersionBase = "1.0.0";
20+
public static readonly Regex VersionRegex = new(@"^v\d+.\d+.\d+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
1621
public static readonly string DefaultRepositoryUrl = "https://github.com/MonoGame/MonoGame";
17-
public static readonly string DefaultBaseVersion = "3.8.2";
1822

1923
public BuildContext(ICakeContext context) : base(context)
2024
{
2125
var repositoryUrl = context.Argument("build-repository", DefaultRepositoryUrl);
2226
var buildConfiguration = context.Argument("build-configuration", "Release");
2327
BuildOutput = context.Argument("build-output", "artifacts");
24-
Version = context.Argument("build-version", DefaultBaseVersion + ".1-develop");
2528
NuGetsDirectory = $"{BuildOutput}/NuGet/";
2629

30+
var tags = GitAliases.GitTags(context, ".");
31+
foreach (var tag in tags)
32+
{
33+
if (VersionRegex.IsMatch(tag.FriendlyName))
34+
{
35+
VersionBase = tag.FriendlyName[1..];
36+
}
37+
}
38+
2739
if (context.BuildSystem().IsRunningOnGitHubActions)
2840
{
2941
var workflow = context.BuildSystem().GitHubActions.Environment.Workflow;
3042
repositoryUrl = $"https://github.com/{workflow.Repository}";
3143

44+
if (workflow.RefType == GitHubActionsRefType.Tag)
45+
{
46+
var baseVersion = workflow.RefName;
47+
if (Regex.IsMatch(baseVersion, @"v\d+.\d+.\d+"))
48+
{
49+
VersionBase = baseVersion[1..];
50+
}
51+
else
52+
{
53+
throw new Exception($"Invalid tag: {baseVersion}");
54+
}
55+
}
56+
3257
if (workflow.Repository != "MonoGame/MonoGame")
3358
{
34-
Version = $"{DefaultBaseVersion}.{workflow.RunNumber}-{workflow.RepositoryOwner}";
59+
Version = $"{VersionBase}.{workflow.RunNumber}-{workflow.RepositoryOwner}";
3560
}
3661
else if (workflow.RefType == GitHubActionsRefType.Branch && workflow.RefName != "refs/heads/master")
3762
{
38-
Version = $"{DefaultBaseVersion}.{workflow.RunNumber}-develop";
63+
Version = $"{VersionBase}.{workflow.RunNumber}-develop";
3964
}
4065
else
4166
{
42-
Version = $"{DefaultBaseVersion}.{workflow.RunNumber}";
67+
Version = $"{VersionBase}.{workflow.RunNumber}";
4368
}
4469
}
70+
else
71+
{
72+
Version = context.Argument("build-version", VersionBase + ".1-develop");
73+
}
4574

4675
DotNetMSBuildSettings = new DotNetMSBuildSettings();
4776
DotNetMSBuildSettings.WithProperty("Version", Version);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
namespace BuildScripts;
3+
4+
[TaskName("DeployNuGetsToNuGetOrgTask")]
5+
[IsDependentOn(typeof(DownloadArtifactsTask))]
6+
public sealed class DeployNuGetsToNuGetOrgTask : FrostingTask<BuildContext>
7+
{
8+
public override bool ShouldRun(BuildContext context)
9+
{
10+
if (context.BuildSystem().IsRunningOnGitHubActions)
11+
{
12+
var workflow = context.BuildSystem().GitHubActions.Environment.Workflow;
13+
if (workflow.RefType == GitHubActionsRefType.Tag &&
14+
!string.IsNullOrWhiteSpace(context.EnvironmentVariable("NUGET_API_KEY")))
15+
{
16+
return true;
17+
}
18+
}
19+
20+
return false;
21+
}
22+
23+
public override void Run(BuildContext context)
24+
{
25+
context.DotNetNuGetPush($"nugets/*.nupkg", new()
26+
{
27+
ApiKey = context.EnvironmentVariable("NUGET_API_KEY"),
28+
Source = $"https://api.nuget.org/v3/index.json"
29+
});
30+
}
31+
}

build/Tasks.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public sealed class BuildTemplatesTask : FrostingTask<BuildContext> { }
2929
[IsDependentOn(typeof(BuildTemplatesTask))]
3030
public sealed class BuildAllTask : FrostingTask<BuildContext> { }
3131

32+
[TaskName("Deploy")]
33+
[IsDependentOn(typeof(DeployNuGetsToGitHubTask))]
34+
[IsDependentOn(typeof(DeployNuGetsToNuGetOrgTask))]
35+
public sealed class DeployTask : FrostingTask<BuildContext> { }
36+
3237
[TaskName("Default")]
3338
[IsDependentOn(typeof(BuildAllTask))]
3439
public sealed class DefaultTask : FrostingTask<BuildContext> { }

0 commit comments

Comments
 (0)