Skip to content

Commit

Permalink
Merge pull request #822 from AristurtleDev/feature/automated-packaging
Browse files Browse the repository at this point in the history
Update GitHub Workflow to Automate Build, Test, and Package Deployment
  • Loading branch information
lithiumtoast authored Nov 19, 2023
2 parents 20fb26d + f4b2942 commit 17c45f5
Show file tree
Hide file tree
Showing 25 changed files with 350 additions and 148 deletions.
138 changes: 36 additions & 102 deletions .github/workflows/build-test-deploy.yml
Original file line number Diff line number Diff line change
@@ -1,115 +1,49 @@
name: "Build test deploy"

env:
DOTNET_CLI_TELEMETRY_OPTOUT: 1
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
DOTNET_CORE_SDK_VERSION: 6.0.302
MYGET_ACCESS_TOKEN: ${{ secrets.MYGET_ACCESS_TOKEN }}
MYGET_SOURCE_URL: 'https://www.myget.org/F/lithiumtoast/api/v3/index.json'

on:
push:
branches: [develop]
branches: [develop, test/feature]
tags: [v*]
pull_request:
branches: [develop]
pull_request_target:
branches: [develop]

jobs:

version-job:
name: "Version"
runs-on: ubuntu-latest

steps:
- name: "Checkout Git repository"
uses: actions/checkout@v2

- name: "Fetch all history for all tags and branches"
run: git fetch --prune --unshallow

- name: "Install GitVersion"
uses: gittools/actions/gitversion/setup@v0.9.6
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true # workaround for https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/ until the execute action is updated
with:
versionSpec: '5.x'

- name: "Use GitVersion"
uses: gittools/actions/gitversion/execute@v0.9.6

- run: echo "$GitVersion_NuGetVersionV2" >> version.txt

- name: "Upload NuGetVersion version artifact"
uses: actions/upload-artifact@v2
with:
name: version
path: version.txt

build-test-pack-job:
name: "Build"
needs: [version-job]
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, macos-latest, ubuntu-latest]

name: "Build-Test-Pack"
runs-on: ubuntu-latest
steps:

- name: "Download version artifact"
uses: actions/download-artifact@v2
with:
name: version

- name: "Read Version"
id: version
shell: bash
run: |
echo "VERSION=$(cat version.txt)" >> $GITHUB_ENV
- name: "Print Version"
shell: bash
run: |
echo $VERSION
- name: "Checkout repository"
uses: actions/checkout@master
with:
submodules: true
lfs: true

- name: "Setup .NET Core CLI"
uses: actions/setup-dotnet@v1
with:
dotnet-version: '${{ env.DOTNET_CORE_SDK_VERSION }}'

- name: "Install fonts (Ubuntu)"
if: matrix.os == 'ubuntu-latest'
run: |
echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | sudo debconf-set-selections
sudo apt-get install -y ttf-mscorefonts-installer
sudo apt-get install -y fontconfig
sudo fc-cache -f -v
sudo fc-match Arial
- name: "Download NuGet packages"
run: dotnet restore --verbosity quiet

- name: "Build solution"
run: dotnet build --nologo --verbosity minimal --configuration Release --no-restore /p:Version='${{ env.VERSION }}'

- name: "Test solution"
run: dotnet test --nologo --verbosity normal --configuration Release --no-build

- name: "Pack solution"
if: matrix.os == 'ubuntu-latest' && github.event_name == 'push'
run: dotnet pack --nologo --output "./nuget-packages" --verbosity minimal --configuration Release --no-build -p:PackageVersion='${{ env.VERSION }}'

- name: "Add Packages Source: MyGet"
if: matrix.os == 'ubuntu-latest' && github.event_name == 'push'
run: dotnet nuget add source $MYGET_SOURCE_URL --name "MyGet"

- name: "Upload Packages: MyGet"
if: matrix.os == 'ubuntu-latest' && github.event_name == 'push'
run: dotnet nuget push "./**/*.nupkg" --source "MyGet" --skip-duplicate --api-key $MYGET_ACCESS_TOKEN
- name: "Clone Repository"
uses: actions/checkout@v4
- name: "CAKE (Build -> Test -> Package)"
run: dotnet run --project ./build/Build.csproj -- --target=Default
- name: "Upload Artifacts For Deploy"
if: github.event_name != 'pull_request'
uses: actions/upload-artifact@main
with:
name: MonoGame.Extended
path: artifacts/*

deploy-job:
name: "Deploy Nugets"
runs-on: ubuntu-latest
permissions:
packages: write
contents: write
needs: [build-test-pack-job]
if: ${{ github.event_name == 'push' }}
steps:
- name: "Clone Repository"
uses: actions/checkout@v4
- name: "Download Artifacts For Deploy"
uses: actions/download-artifact@v3
with:
name: MonoGame.Extended
path: artifacts
- name: "Push Package"
run: dotnet run --project ./build/Build.csproj -- --target=Deploy
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
MYGET_ACCESS_TOKEN: ${{ secrets.MYGET_ACCESS_TOKEN }}
NUGET_ACCESS_TOKEN: ${{ secrets.NUGET_ACCESS_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
# macOS
.DS_Store
*.user

# cake build output
artifacts
20 changes: 20 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project>
<PropertyGroup>
<Version>3.9.0</Version>
<Authors>craftworkgames</Authors>
<PackageProjectUrl>https://github.com/craftworkgames/MonoGame.Extended</PackageProjectUrl>
<RepositoryUrl>https://github.com/craftworkgames/MonoGame.Extended</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<RepositoryBranch>develop</RepositoryBranch>
<NeutralLanguage>en</NeutralLanguage>
<PackageIcon>logo-nuget-128.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>

<ItemGroup>
<!-- path must be relative to the individual csproj's not this .targets file -->
<None Include="../../../Logos/logo-nuget-128.png" Pack="true" PackagePath="" />
<None Include="../../../README.md" Pack="true" PackagePath="" />
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions build/Build.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Cake.Frosting" Version="3.1.0" />
</ItemGroup>
</Project>
41 changes: 41 additions & 0 deletions build/BuildContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Linq;
using Cake.Common;
using Cake.Common.Build;
using Cake.Common.Xml;
using Cake.Core;
using Cake.Frosting;

namespace BuildScripts;

public sealed class BuildContext : FrostingContext
{
public string ArtifactsDirectory { get; }
public string Version { get; }
public string SolutionPath { get; }
public string? RepositoryOwner { get; }
public string? RepositoryUrl { get; }
public bool IsTag { get; }
public bool IsRunningOnGitHubActions { get; }
public string? GitHubToken { get; }
public string? MyGetAccessToken { get; }
public string? NuGetAccessToken { get; }

public BuildContext(ICakeContext context) : base(context)
{
ArtifactsDirectory = context.Argument(nameof(ArtifactsDirectory), "artifacts");
Version = context.XmlPeek("Directory.Build.targets", "/Project/PropertyGroup/Version");
SolutionPath = "./MonoGame.Extended.sln";
IsRunningOnGitHubActions = context.BuildSystem().IsRunningOnGitHubActions;

if (IsRunningOnGitHubActions)
{
Version += "." + context.EnvironmentVariable("GITHUB_RUN_NUMBER");
RepositoryOwner = context.EnvironmentVariable("GITHUB_REPOSITORY_OWNER");
RepositoryUrl = $"https://github.com/{context.EnvironmentVariable("GITHUB_REPOSITORY")}";
IsTag = context.EnvironmentVariable("GITHUB_REF_TYPE") == "tag";
GitHubToken = context.EnvironmentVariable("GITHUB_TOKEN");
MyGetAccessToken = context.EnvironmentVariable("MYGET_ACCESS_TOKEN");
NuGetAccessToken = context.EnvironmentVariable("NUGET_ACCESS_TOKEN");
}
}
}
29 changes: 29 additions & 0 deletions build/BuildTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Build;
using Cake.Common.Tools.DotNet.MSBuild;
using Cake.Frosting;

namespace BuildScripts;

[TaskName(nameof(BuildTask))]
public sealed class BuildTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
DotNetMSBuildSettings msBuildSettings = new DotNetMSBuildSettings();
msBuildSettings.WithProperty("Version", context.Version);
msBuildSettings.WithProperty("NoWarn", "CS1591");


DotNetBuildSettings buildSettings = new DotNetBuildSettings()
{
MSBuildSettings = msBuildSettings,
Configuration = "Release",
Verbosity = DotNetVerbosity.Minimal,
NoRestore = true,
NoLogo = true
};

context.DotNetBuild(context.SolutionPath, buildSettings);
}
}
25 changes: 25 additions & 0 deletions build/DeployToGithubTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.NuGet.Push;
using Cake.Frosting;

namespace BuildScripts;

[TaskName(nameof(DeployToGitHubTask))]
public sealed class DeployToGitHubTask : FrostingTask<BuildContext>
{
public override bool ShouldRun(BuildContext context)
{
return context.IsRunningOnGitHubActions;
}

public override void Run(BuildContext context)
{
DotNetNuGetPushSettings pushSettings = new DotNetNuGetPushSettings()
{
Source = $"https://nuget.pkg.github.com/{context.RepositoryOwner}/index.json",
ApiKey = context.GitHubToken
};

context.DotNetNuGetPush("artifacts/*.nupkg", pushSettings);
}
}
25 changes: 25 additions & 0 deletions build/DeployToMyGetTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.NuGet.Push;
using Cake.Frosting;

namespace BuildScripts;

[TaskName(nameof(DeployToMyGetTask))]
public sealed class DeployToMyGetTask : FrostingTask<BuildContext>
{
public override bool ShouldRun(BuildContext context)
{
return context.IsRunningOnGitHubActions;
}

public override void Run(BuildContext context)
{
DotNetNuGetPushSettings pushSettings = new DotNetNuGetPushSettings()
{
Source = $"https://www.myget.org/F/lithiumtoast/api/v3/index.json",
ApiKey = context.MyGetAccessToken
};

context.DotNetNuGetPush("artifacts/*.nupkg", pushSettings);
}
}
27 changes: 27 additions & 0 deletions build/DeployToNuGetTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.NuGet.Push;
using Cake.Frosting;

namespace BuildScripts;

[TaskName(nameof(DeployToNuGetTask))]
public sealed class DeployToNuGetTask : FrostingTask<BuildContext>
{
public override bool ShouldRun(BuildContext context)
{
return context.IsRunningOnGitHubActions &&
context.IsTag &&
context.RepositoryOwner == "craftworksgames";
}

public override void Run(BuildContext context)
{
DotNetNuGetPushSettings pushSettings = new DotNetNuGetPushSettings()
{
Source = $"https://api.nuget.org/v3/index.json",
ApiKey = context.NuGetAccessToken
};

context.DotNetNuGetPush("artifacts/*.nupkg", pushSettings);
}
}
40 changes: 40 additions & 0 deletions build/PackageTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Cake.Common.Build;
using Cake.Common.IO;
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.MSBuild;
using Cake.Common.Tools.DotNet.Pack;
using Cake.Core.IO;
using Cake.Frosting;

namespace BuildScripts;

[TaskName(nameof(PackageTask))]
public sealed class PackageTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.CleanDirectories(context.ArtifactsDirectory);
context.CreateDirectory(context.ArtifactsDirectory);

DotNetMSBuildSettings msBuildSettings = new DotNetMSBuildSettings();
msBuildSettings.WithProperty("Version", context.Version);

// Ignore warnings about adding duplicate items added to package
msBuildSettings.WithProperty("NoWarn", "NU5118");

DotNetPackSettings packSettings = new DotNetPackSettings()
{
MSBuildSettings = msBuildSettings,
Configuration = "Release",
Verbosity = DotNetVerbosity.Minimal,
NoLogo = true,
OutputDirectory = context.ArtifactsDirectory,
};

FilePathCollection files = context.GetFiles("./src/cs/MonoGame.Extended*/**/*.csproj");
foreach(FilePath file in files)
{
context.DotNetPack(file.FullPath, packSettings);
}
}
}
Loading

0 comments on commit 17c45f5

Please sign in to comment.