Skip to content

Commit f6739b1

Browse files
committed
Added github actions build
1 parent 452dc9f commit f6739b1

File tree

5 files changed

+254
-0
lines changed

5 files changed

+254
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Build/Publish Preview Nuget
2+
3+
on:
4+
push:
5+
branches: [main]
6+
workflow_dispatch:
7+
inputs:
8+
push_nuget:
9+
description: "Push to nuget.org"
10+
required: true
11+
type: boolean
12+
default: false
13+
nuget_version_override:
14+
description: "Nuget Version Override"
15+
required: false
16+
type: string
17+
18+
jobs:
19+
build:
20+
runs-on: ubuntu-latest
21+
env:
22+
DOTNET_VERSION: 8.0.x
23+
SRC_DIRECTORY_PATH_LIB: ${{ github.workspace }}/src
24+
PROJECT_NAME_LIB: PublicInterfaceGenerator
25+
NUGET_VERSION: ${{ github.event.inputs.nuget_version_override || '0.1.0-preview' }}.${{ github.run_number }}
26+
PUSH_NUGET: ${{ github.event.inputs.push_nuget || false }}
27+
28+
steps:
29+
- uses: actions/checkout@v2
30+
- name: Setup .NET
31+
uses: actions/setup-dotnet@v1
32+
with:
33+
dotnet-version: ${{ env.DOTNET_VERSION }}
34+
- name: Cake Frosting Build and Push NuGet
35+
run: dotnet run --project ${{ github.workspace }}/build/Build.csproj -- --srcDirectoryPath=${{ env.SRC_DIRECTORY_PATH_LIB }} --nugetVersion=${{ env.NUGET_VERSION }} --pushNuget=${{ env.PUSH_NUGET }} --nugetPushToken=${{ secrets.NUGET_PUSH_TOKEN }}
36+
37+
# - name: Publish NuGet - Lib
38+
# run: dotnet nuget push "${{ env.SRC_DIRECTORY_PATH_LIB }}/${{ env.PROJECT_NAME_LIB }}/bin/${{ env.CONFIGURATION }}/cake-build-output/*${{ env.NUGET_VERSION }}.nupkg" --source "https://api.nuget.org/v3/index.json" --api-key ${{ secrets.NUGET_PUSH_TOKEN }}

build/ApplicationBuildConfigs.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Cake.Common;
2+
using Cake.Core;
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
public record ProjectPaths(
11+
string ProjectName,
12+
string PathToSln,
13+
string ProjectFolder,
14+
string CsprojFile,
15+
string UnitTestProj,
16+
string OutDir,
17+
string NuGetFilePath)
18+
{
19+
public static ProjectPaths LoadFromContext(ICakeContext context, string buildConfiguration, string srcDirectory, string nugetVersion)
20+
{
21+
var projectName = "CodeUpdater";
22+
var pathToSln = srcDirectory + $"/{projectName}.sln";
23+
var projectDir = srcDirectory + $"/{projectName}";
24+
var csProjFile = projectDir + $"/{projectName}.csproj";
25+
var unitTestsProj = srcDirectory + $"/UnitTests/UnitTests.csproj";
26+
var outDir = projectDir + $"/bin/{buildConfiguration}/cake-build-output";
27+
var nugetFilePath = outDir + $"/*{nugetVersion}.nupkg";
28+
29+
return new ProjectPaths(
30+
projectName,
31+
pathToSln,
32+
projectDir,
33+
csProjFile,
34+
unitTestsProj,
35+
outDir,
36+
nugetFilePath);
37+
}
38+
};

build/Build.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
7+
<LangVersion>preview</LangVersion>
8+
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
9+
</PropertyGroup>
10+
<ItemGroup>
11+
<PackageReference Include="Cake.Frosting" Version="4.0.0" />
12+
</ItemGroup>
13+
</Project>

build/Build.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31912.275
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Build", "Build.csproj", "{0C92FB17-0701-445C-8DA8-4065442B48C5}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{0C92FB17-0701-445C-8DA8-4065442B48C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{0C92FB17-0701-445C-8DA8-4065442B48C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{0C92FB17-0701-445C-8DA8-4065442B48C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{0C92FB17-0701-445C-8DA8-4065442B48C5}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {4B8CCF56-5733-4E25-A930-E35CF2132F0F}
24+
EndGlobalSection
25+
EndGlobal

build/Program.cs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Cake.Core;
4+
using Cake.Core.Diagnostics;
5+
using Cake.Frosting;
6+
using Cake.Common;
7+
using Cake.Common.IO;
8+
using Cake.Common.Tools.DotNet;
9+
using Cake.Common.Tools.DotNet.Restore;
10+
using System.Threading;
11+
using System.Diagnostics;
12+
using Cake.Common.Tools.DotNet.Test;
13+
using Cake.Common.Tools.DotNet.Build;
14+
15+
public static class Program
16+
{
17+
public static int Main(string[] args)
18+
{
19+
return new CakeHost()
20+
.UseContext<BuildContext>()
21+
.Run(args);
22+
}
23+
}
24+
25+
public class BuildContext : FrostingContext
26+
{
27+
public const string BuildConfiguration = "Release";
28+
29+
public string Target { get; }
30+
public string SrcDirectoryPath { get; }
31+
public string NugetVersion { get; }
32+
public bool PushNuget { get; }
33+
public string NuGetPushToken { get; }
34+
public ProjectPaths ProjectPaths { get; }
35+
36+
public BuildContext(ICakeContext context)
37+
: base(context)
38+
{
39+
Target = context.Argument("target", "Default");
40+
SrcDirectoryPath = LoadParameter(context, "srcDirectoryPath");
41+
NugetVersion = LoadParameter(context, "nugetVersion");
42+
NuGetPushToken = LoadParameter(context, "nuGetPushToken");
43+
PushNuget = context.Argument<bool>("pushNuget", false);
44+
45+
ProjectPaths = ProjectPaths.LoadFromContext(context, BuildConfiguration, SrcDirectoryPath, NugetVersion);
46+
}
47+
48+
private string LoadParameter(ICakeContext context, string parameterName)
49+
{
50+
return context.Arguments.GetArgument(parameterName) ?? throw new Exception($"Missing parameter '{parameterName}'");
51+
}
52+
}
53+
54+
[TaskName(nameof(OutputParametersTask))]
55+
public sealed class OutputParametersTask : FrostingTask<BuildContext>
56+
{
57+
public override void Run(BuildContext context)
58+
{
59+
context.Log.Information($"INFO: Current Working Directory: {context.Environment.WorkingDirectory}");
60+
61+
context.Log.Information($"INFO: {nameof(context.SrcDirectoryPath)}: {context.SrcDirectoryPath}");
62+
context.Log.Information($"INFO: {nameof(context.ProjectPaths)}.{nameof(context.ProjectPaths.ProjectName)}: {context.ProjectPaths.ProjectName}");
63+
}
64+
}
65+
66+
[IsDependentOn(typeof(OutputParametersTask))]
67+
[TaskName(nameof(BuildTask))]
68+
public sealed class BuildTask : FrostingTask<BuildContext>
69+
{
70+
public override void Run(BuildContext context)
71+
{
72+
context.CleanDirectory(context.ProjectPaths.OutDir);
73+
74+
BuildDotnetApp(context, context.ProjectPaths.PathToSln);
75+
TestDotnetApp(context, context.ProjectPaths.UnitTestProj);
76+
PackNugetPackage(context, context.ProjectPaths.OutDir, context.ProjectPaths.CsprojFile);
77+
}
78+
79+
private void BuildDotnetApp(BuildContext context, string pathToSln)
80+
{
81+
context.DotNetRestore(pathToSln, new DotNetRestoreSettings { });
82+
83+
context.DotNetBuild(pathToSln, new DotNetBuildSettings
84+
{
85+
NoRestore = true,
86+
Configuration = BuildContext.BuildConfiguration
87+
});
88+
}
89+
90+
private void TestDotnetApp(BuildContext context, string pathToUnitTestProj)
91+
{
92+
var testSettings = new DotNetTestSettings()
93+
{
94+
Configuration = BuildContext.BuildConfiguration,
95+
NoBuild = true,
96+
ArgumentCustomization = (args) => args.Append("/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --logger trx")
97+
};
98+
99+
context.DotNetTest(pathToUnitTestProj, testSettings);
100+
}
101+
102+
private void PackNugetPackage(BuildContext context, string outDir, string csprojFile)
103+
{
104+
context.DotNetPack(csprojFile, new Cake.Common.Tools.DotNet.Pack.DotNetPackSettings
105+
{
106+
IncludeSymbols = false,
107+
IncludeSource = false,
108+
NoBuild = true,
109+
Configuration = BuildContext.BuildConfiguration,
110+
OutputDirectory = outDir,
111+
VersionSuffix = context.NugetVersion,
112+
ArgumentCustomization = (args) => args.Append($"-p:PackageVersion={context.NugetVersion}")
113+
});
114+
}
115+
}
116+
117+
[IsDependentOn(typeof(BuildTask))]
118+
[TaskName(nameof(NugetPushTask))]
119+
public sealed class NugetPushTask : FrostingTask<BuildContext>
120+
{
121+
public override void Run(BuildContext context)
122+
{
123+
if (!context.PushNuget)
124+
{
125+
return;
126+
}
127+
128+
context.DotNetNuGetPush(context.ProjectPaths.NuGetFilePath, new Cake.Common.Tools.DotNet.NuGet.Push.DotNetNuGetPushSettings
129+
{
130+
Source = "https://api.nuget.org/v3/index.json",
131+
ApiKey = context.NuGetPushToken
132+
});
133+
}
134+
}
135+
136+
[TaskName("Default")]
137+
[IsDependentOn(typeof(NugetPushTask))]
138+
public class DefaultTask : FrostingTask
139+
{
140+
}

0 commit comments

Comments
 (0)