Skip to content

Commit 01b0137

Browse files
committed
#2254 GitHub Actions: Cake build job
1 parent 09de4ce commit 01b0137

File tree

2 files changed

+73
-28
lines changed

2 files changed

+73
-28
lines changed

.github/workflows/develop.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
branches: [ "develop" ]
77

88
jobs:
9+
#1 Official required job to run utilizing GitHub Actions infrastructure
910
build:
1011
runs-on: ubuntu-latest
1112
strategy:
@@ -33,3 +34,38 @@ jobs:
3334
run: dotnet test --no-restore --no-build --verbosity minimal --framework net${{ matrix.dotnet-version }} ./test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj
3435
- name: Acceptance Tests
3536
run: dotnet test --no-restore --no-build --verbosity minimal --framework net${{ matrix.dotnet-version }} ./test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj
37+
38+
#2 The build job to run in Docker container utilizing images from https://hub.docker.com/u/ocelot2
39+
# The main runnung script is build.cake, a cake-script which is hard to support because of slow and outdated integrated libs
40+
# docker-build:
41+
# runs-on: ubuntu-latest
42+
# container:
43+
# image: node:18
44+
# env:
45+
# NODE_ENV: development
46+
# ports:
47+
# - 80
48+
# volumes:
49+
# - my_docker_volume:/volume_mount
50+
# options: --cpus 1
51+
# container:
52+
# image: ghcr.io/owner/image
53+
# credentials:
54+
# username: ${{ github.actor }}
55+
# password: ${{ secrets.github_token }}
56+
# steps:
57+
# - name: Check for dockerenv file
58+
# run: (ls /.dockerenv && echo Found dockerenv) || (echo No dockerenv)
59+
build-cake:
60+
runs-on: ubuntu-latest
61+
steps:
62+
- name: .NET version
63+
run: dotnet --version
64+
- name: Checkout
65+
uses: actions/checkout@v4
66+
with:
67+
fetch-depth: 0
68+
- name: Cake build
69+
uses: cake-build/cake-action@v3
70+
with:
71+
target: Build

build.cake

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#tool dotnet:?package=GitVersion.Tool&version=5.12.0 // 6.0.0-beta.7 supports .NET 8, 7, 6
1+
#tool dotnet:?package=GitVersion.Tool&version=6.2.0 // .NET 8-9
22
#tool dotnet:?package=coveralls.net&version=4.0.1
33
#tool nuget:?package=ReportGenerator&version=5.2.4
44
#addin nuget:?package=Newtonsoft.Json&version=13.0.3
@@ -14,6 +14,7 @@ using System.Linq;
1414
using System.Text.RegularExpressions;
1515

1616
const string Release = "Release"; // task name, target, and Release config name
17+
const string PullRequest = "PullRequest"; // task name, target, and PullRequest config name
1718
const string AllFrameworks = "net8.0;net9.0";
1819
const string LatestFramework = "net9.0";
1920

@@ -50,7 +51,7 @@ var releaseNotes = new List<string>();
5051
// internal build variables - don't change these.
5152
string committedVersion = "0.0.0-dev";
5253
GitVersion versioning = null;
53-
bool IsTechnicalRelease = false;
54+
bool IsTechnicalRelease = true;
5455

5556
var target = Argument("target", "Default");
5657
var slnFile = (target == Release) ? $"./Ocelot.{Release}.sln" : "./Ocelot.sln";
@@ -94,7 +95,7 @@ Task("Compile")
9495
{
9596
Configuration = compileConfig,
9697
};
97-
if (target != Release)
98+
if (target == PullRequest)
9899
{
99100
settings.Framework = LatestFramework; // build using .NET 9 SDK only
100101
}
@@ -121,13 +122,20 @@ Task("Version")
121122
.Does(() =>
122123
{
123124
versioning = GetNuGetVersionForCommit();
124-
var nugetVersion = versioning.NuGetVersion;
125-
Information("SemVer version number: " + nugetVersion);
126-
127-
if (IsRunningOnCircleCI())
125+
Information("#########################");
126+
Information("# SemVer Information");
127+
Information("#========================");
128+
Information($"# {nameof(versioning.NuGetVersion)}: {versioning.NuGetVersion}");
129+
Information($"# {nameof(versioning.BranchName)}: {versioning.BranchName}");
130+
Information($"# {nameof(versioning.MajorMinorPatch)}: {versioning.MajorMinorPatch}");
131+
Information($"# {nameof(versioning.SemVer)}: {versioning.SemVer}");
132+
Information($"# {nameof(versioning.InformationalVersion)}: {versioning.InformationalVersion}");
133+
Information("#########################");
134+
135+
if (IsRunningInCICD())
128136
{
129137
Information("Persisting version number...");
130-
PersistVersion(committedVersion, nugetVersion);
138+
PersistVersion(committedVersion, versioning.SemVer);
131139
}
132140
else
133141
{
@@ -139,7 +147,7 @@ Task("GitLogUniqContributors")
139147
.Does(() =>
140148
{
141149
var command = "log --format=\"%aN|%aE\" ";
142-
// command += IsRunningOnCircleCI() ? "| sort | uniq" :
150+
// command += IsRunningInCICD() ? "| sort | uniq" :
143151
// IsRunningInPowershell() ? "| Sort-Object -Unique" : "| sort | uniq";
144152
List<string> output = GitHelper(command);
145153
output.Sort();
@@ -164,7 +172,7 @@ Task("CreateReleaseNotes")
164172
Information($"Generating release notes at {releaseNotesFile}");
165173
var lastReleaseTags = GitHelper("describe --tags --abbrev=0 --exclude net*");
166174
var lastRelease = lastReleaseTags.First(t => !t.StartsWith("net")); // skip 'net*-vX.Y.Z' tag and take 'major.minor.build'
167-
var releaseVersion = versioning.NuGetVersion;
175+
var releaseVersion = versioning.SemVer;
168176

169177
// Read main header from Git file, substitute version in header, and add content further...
170178
Information("{0} New release tag is " + releaseVersion);
@@ -465,7 +473,7 @@ Task("RunUnitTests")
465473
.Append("--verbosity:detailed")
466474
.Append("--consoleLoggerParameters:ErrorsOnly")
467475
};
468-
if (target != Release)
476+
if (target == PullRequest)
469477
{
470478
settings.Framework = LatestFramework; // .NET 9 SDK only
471479
}
@@ -482,7 +490,7 @@ Task("RunUnitTests")
482490

483491
GenerateReport(coverageSummaryFile);
484492

485-
if (IsRunningOnCircleCI() && IsMainOrDevelop())
493+
if (IsRunningInCICD() && IsMainOrDevelop())
486494
{
487495
var repoToken = EnvironmentVariable(coverallsRepoToken);
488496
if (string.IsNullOrEmpty(repoToken))
@@ -512,9 +520,9 @@ Task("RunUnitTests")
512520

513521
Information("Sequence Coverage: " + sequenceCoverage);
514522

515-
if(double.Parse(sequenceCoverage) < minCodeCoverage)
523+
if (double.Parse(sequenceCoverage) < minCodeCoverage)
516524
{
517-
var whereToCheck = !IsRunningOnCircleCI() ? coverallsRepo : artifactsForUnitTestsDir;
525+
var whereToCheck = !IsRunningInCICD() ? coverallsRepo : artifactsForUnitTestsDir;
518526
throw new Exception(string.Format("Code coverage fell below the threshold of {0}%. You can find the code coverage report at {1}", minCodeCoverage, whereToCheck));
519527
};
520528
});
@@ -530,7 +538,7 @@ Task("RunAcceptanceTests")
530538
.Append("--no-restore")
531539
.Append("--no-build")
532540
};
533-
if (target != Release)
541+
if (target == PullRequest)
534542
{
535543
settings.Framework = LatestFramework; // .NET 9 SDK only
536544
}
@@ -551,7 +559,7 @@ Task("RunIntegrationTests")
551559
.Append("--no-restore")
552560
.Append("--no-build")
553561
};
554-
if (target != Release)
562+
if (target == PullRequest)
555563
{
556564
settings.Framework = LatestFramework; // .NET 9 SDK only
557565
}
@@ -602,7 +610,7 @@ Task("PublishGitHubRelease")
602610
.IsDependentOn("CreateArtifacts")
603611
.Does(() =>
604612
{
605-
if (!IsRunningOnCircleCI()) return;
613+
if (!IsRunningInCICD()) return;
606614

607615
dynamic release = CreateGitHubRelease();
608616
var path = packagesDir.ToString() + @"/**/*";
@@ -619,9 +627,9 @@ Task("EnsureStableReleaseRequirements")
619627
{
620628
Information("Check if stable release...");
621629

622-
if (!IsRunningOnCircleCI())
630+
if (!IsRunningInCICD())
623631
{
624-
throw new Exception("Stable release should happen via circleci");
632+
throw new Exception("Stable release should happen via CI/CD");
625633
}
626634

627635
Information("Release is stable...");
@@ -636,7 +644,7 @@ Task("DownloadGitHubReleaseArtifacts")
636644
System.Threading.Thread.Sleep(5000);
637645
EnsureDirectoryExists(packagesDir);
638646

639-
var releaseUrl = "https://api.github.com/repos/ThreeMammals/ocelot/releases/tags/" + versioning.NuGetVersion;
647+
var releaseUrl = "https://api.github.com/repos/ThreeMammals/ocelot/releases/tags/" + versioning.SemVer;
640648
var releaseInfo = await GetResourceAsync(releaseUrl);
641649
var assets_url = Newtonsoft.Json.Linq.JObject.Parse(releaseInfo)
642650
.Value<string>("assets_url");
@@ -665,7 +673,7 @@ Task("PublishToNuget")
665673
return;
666674
}
667675

668-
if (IsRunningOnCircleCI())
676+
if (IsRunningInCICD())
669677
{
670678
// stable releases
671679
var nugetFeedStableKey = EnvironmentVariable("OCELOT_NUGET_API_KEY_3Mammals");
@@ -762,7 +770,7 @@ private void SetupGitHubClient(System.Net.Http.HttpClient client)
762770

763771
private dynamic CreateGitHubRelease()
764772
{
765-
var json = $"{{ \"tag_name\": \"{versioning.NuGetVersion}\", \"target_commitish\": \"main\", \"name\": \"{versioning.NuGetVersion}\", \"body\": \"{ReleaseNotesAsJson()}\", \"draft\": true, \"prerelease\": true, \"generate_release_notes\": false }}";
773+
var json = $"{{ \"tag_name\": \"{versioning.SemVer}\", \"target_commitish\": \"main\", \"name\": \"{versioning.SemVer}\", \"body\": \"{ReleaseNotesAsJson()}\", \"draft\": true, \"prerelease\": true, \"generate_release_notes\": false }}";
766774
var content = new System.Net.Http.StringContent(json, System.Text.Encoding.UTF8, "application/json");
767775

768776
using (var client = new System.Net.Http.HttpClient())
@@ -816,7 +824,7 @@ private void CompleteGitHubRelease(dynamic release)
816824
{
817825
int releaseId = release.id;
818826
string url = release.url.ToString();
819-
var json = $"{{ \"tag_name\": \"{versioning.NuGetVersion}\", \"target_commitish\": \"main\", \"name\": \"{versioning.NuGetVersion}\", \"body\": \"{ReleaseNotesAsJson()}\", \"draft\": false, \"prerelease\": false }}";
827+
var json = $"{{ \"tag_name\": \"{versioning.SemVer}\", \"target_commitish\": \"main\", \"name\": \"{versioning.SemVer}\", \"body\": \"{ReleaseNotesAsJson()}\", \"draft\": false, \"prerelease\": false }}";
820828
var request = new System.Net.Http.HttpRequestMessage(new System.Net.Http.HttpMethod("Patch"), url); // $"https://api.github.com/repos/ThreeMammals/Ocelot/releases/{releaseId}");
821829
request.Content = new System.Net.Http.StringContent(json, System.Text.Encoding.UTF8, "application/json");
822830

@@ -856,14 +864,15 @@ private async Task<string> GetResourceAsync(string url)
856864
}
857865
}
858866

859-
private bool IsRunningOnCircleCI()
860-
{
861-
return !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("CIRCLECI"));
862-
}
867+
private static bool IsRunningInCICD()
868+
=> IsRunningOnCircleCI() || IsRunningInGitHubActions();
869+
private static bool IsRunningOnCircleCI()
870+
=> !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("CIRCLECI"));
871+
private static bool IsRunningInGitHubActions()
872+
=> Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true";
863873

864874
private bool IsMainOrDevelop()
865875
{
866876
var env = Environment.GetEnvironmentVariable("CIRCLE_BRANCH").ToLower();
867-
868877
return env == "main" || env == "develop";
869878
}

0 commit comments

Comments
 (0)