Skip to content

Commit 4330bf7

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

File tree

2 files changed

+102
-29
lines changed

2 files changed

+102
-29
lines changed

.github/workflows/develop.yml

+44
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,46 @@ 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: Setup .NET 9
69+
uses: actions/setup-dotnet@v3
70+
with:
71+
dotnet-version: 9.0.x
72+
- name: .NET version
73+
run: dotnet --version
74+
- name: Get branch name
75+
run: echo "Branch name is ${{ github.ref_name }}"
76+
- name: Cake build
77+
uses: cake-build/cake-action@v3
78+
with:
79+
target: Build

build.cake

+58-29
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";
@@ -88,13 +89,14 @@ Task("Compile")
8889
.IsDependentOn("Version")
8990
.Does(() =>
9091
{
92+
Information("Branch: " + GetBranchName());
9193
Information("Build: " + compileConfig);
9294
Information("Solution: " + slnFile);
9395
var settings = new DotNetBuildSettings
9496
{
9597
Configuration = compileConfig,
9698
};
97-
if (target != Release)
99+
if (target == PullRequest)
98100
{
99101
settings.Framework = LatestFramework; // build using .NET 9 SDK only
100102
}
@@ -121,13 +123,20 @@ Task("Version")
121123
.Does(() =>
122124
{
123125
versioning = GetNuGetVersionForCommit();
124-
var nugetVersion = versioning.NuGetVersion;
125-
Information("SemVer version number: " + nugetVersion);
126-
127-
if (IsRunningOnCircleCI())
126+
Information("#########################");
127+
Information("# SemVer Information");
128+
Information("#========================");
129+
Information($"# {nameof(versioning.NuGetVersion)}: {versioning.NuGetVersion}");
130+
Information($"# {nameof(versioning.BranchName)}: {versioning.BranchName}");
131+
Information($"# {nameof(versioning.MajorMinorPatch)}: {versioning.MajorMinorPatch}");
132+
Information($"# {nameof(versioning.SemVer)}: {versioning.SemVer}");
133+
Information($"# {nameof(versioning.InformationalVersion)}: {versioning.InformationalVersion}");
134+
Information("#########################");
135+
136+
if (IsRunningInCICD())
128137
{
129138
Information("Persisting version number...");
130-
PersistVersion(committedVersion, nugetVersion);
139+
PersistVersion(committedVersion, versioning.SemVer);
131140
}
132141
else
133142
{
@@ -139,7 +148,7 @@ Task("GitLogUniqContributors")
139148
.Does(() =>
140149
{
141150
var command = "log --format=\"%aN|%aE\" ";
142-
// command += IsRunningOnCircleCI() ? "| sort | uniq" :
151+
// command += IsRunningInCICD() ? "| sort | uniq" :
143152
// IsRunningInPowershell() ? "| Sort-Object -Unique" : "| sort | uniq";
144153
List<string> output = GitHelper(command);
145154
output.Sort();
@@ -164,7 +173,7 @@ Task("CreateReleaseNotes")
164173
Information($"Generating release notes at {releaseNotesFile}");
165174
var lastReleaseTags = GitHelper("describe --tags --abbrev=0 --exclude net*");
166175
var lastRelease = lastReleaseTags.First(t => !t.StartsWith("net")); // skip 'net*-vX.Y.Z' tag and take 'major.minor.build'
167-
var releaseVersion = versioning.NuGetVersion;
176+
var releaseVersion = versioning.SemVer;
168177

169178
// Read main header from Git file, substitute version in header, and add content further...
170179
Information("{0} New release tag is " + releaseVersion);
@@ -465,7 +474,7 @@ Task("RunUnitTests")
465474
.Append("--verbosity:detailed")
466475
.Append("--consoleLoggerParameters:ErrorsOnly")
467476
};
468-
if (target != Release)
477+
if (target == PullRequest)
469478
{
470479
settings.Framework = LatestFramework; // .NET 9 SDK only
471480
}
@@ -482,7 +491,7 @@ Task("RunUnitTests")
482491

483492
GenerateReport(coverageSummaryFile);
484493

485-
if (IsRunningOnCircleCI() && IsMainOrDevelop())
494+
if (IsRunningInCICD() && IsMainOrDevelop())
486495
{
487496
var repoToken = EnvironmentVariable(coverallsRepoToken);
488497
if (string.IsNullOrEmpty(repoToken))
@@ -512,9 +521,9 @@ Task("RunUnitTests")
512521

513522
Information("Sequence Coverage: " + sequenceCoverage);
514523

515-
if(double.Parse(sequenceCoverage) < minCodeCoverage)
524+
if (double.Parse(sequenceCoverage) < minCodeCoverage)
516525
{
517-
var whereToCheck = !IsRunningOnCircleCI() ? coverallsRepo : artifactsForUnitTestsDir;
526+
var whereToCheck = !IsRunningInCICD() ? coverallsRepo : artifactsForUnitTestsDir;
518527
throw new Exception(string.Format("Code coverage fell below the threshold of {0}%. You can find the code coverage report at {1}", minCodeCoverage, whereToCheck));
519528
};
520529
});
@@ -530,7 +539,7 @@ Task("RunAcceptanceTests")
530539
.Append("--no-restore")
531540
.Append("--no-build")
532541
};
533-
if (target != Release)
542+
if (target == PullRequest)
534543
{
535544
settings.Framework = LatestFramework; // .NET 9 SDK only
536545
}
@@ -551,7 +560,7 @@ Task("RunIntegrationTests")
551560
.Append("--no-restore")
552561
.Append("--no-build")
553562
};
554-
if (target != Release)
563+
if (target == PullRequest)
555564
{
556565
settings.Framework = LatestFramework; // .NET 9 SDK only
557566
}
@@ -602,7 +611,7 @@ Task("PublishGitHubRelease")
602611
.IsDependentOn("CreateArtifacts")
603612
.Does(() =>
604613
{
605-
if (!IsRunningOnCircleCI()) return;
614+
if (!IsRunningInCICD()) return;
606615

607616
dynamic release = CreateGitHubRelease();
608617
var path = packagesDir.ToString() + @"/**/*";
@@ -619,9 +628,9 @@ Task("EnsureStableReleaseRequirements")
619628
{
620629
Information("Check if stable release...");
621630

622-
if (!IsRunningOnCircleCI())
631+
if (!IsRunningInCICD())
623632
{
624-
throw new Exception("Stable release should happen via circleci");
633+
throw new Exception("Stable release should happen via CI/CD");
625634
}
626635

627636
Information("Release is stable...");
@@ -636,7 +645,7 @@ Task("DownloadGitHubReleaseArtifacts")
636645
System.Threading.Thread.Sleep(5000);
637646
EnsureDirectoryExists(packagesDir);
638647

639-
var releaseUrl = "https://api.github.com/repos/ThreeMammals/ocelot/releases/tags/" + versioning.NuGetVersion;
648+
var releaseUrl = "https://api.github.com/repos/ThreeMammals/ocelot/releases/tags/" + versioning.SemVer;
640649
var releaseInfo = await GetResourceAsync(releaseUrl);
641650
var assets_url = Newtonsoft.Json.Linq.JObject.Parse(releaseInfo)
642651
.Value<string>("assets_url");
@@ -665,7 +674,7 @@ Task("PublishToNuget")
665674
return;
666675
}
667676

668-
if (IsRunningOnCircleCI())
677+
if (IsRunningInCICD())
669678
{
670679
// stable releases
671680
var nugetFeedStableKey = EnvironmentVariable("OCELOT_NUGET_API_KEY_3Mammals");
@@ -762,7 +771,7 @@ private void SetupGitHubClient(System.Net.Http.HttpClient client)
762771

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

768777
using (var client = new System.Net.Http.HttpClient())
@@ -816,7 +825,7 @@ private void CompleteGitHubRelease(dynamic release)
816825
{
817826
int releaseId = release.id;
818827
string url = release.url.ToString();
819-
var json = $"{{ \"tag_name\": \"{versioning.NuGetVersion}\", \"target_commitish\": \"main\", \"name\": \"{versioning.NuGetVersion}\", \"body\": \"{ReleaseNotesAsJson()}\", \"draft\": false, \"prerelease\": false }}";
828+
var json = $"{{ \"tag_name\": \"{versioning.SemVer}\", \"target_commitish\": \"main\", \"name\": \"{versioning.SemVer}\", \"body\": \"{ReleaseNotesAsJson()}\", \"draft\": false, \"prerelease\": false }}";
820829
var request = new System.Net.Http.HttpRequestMessage(new System.Net.Http.HttpMethod("Patch"), url); // $"https://api.github.com/repos/ThreeMammals/Ocelot/releases/{releaseId}");
821830
request.Content = new System.Net.Http.StringContent(json, System.Text.Encoding.UTF8, "application/json");
822831

@@ -856,14 +865,34 @@ private async Task<string> GetResourceAsync(string url)
856865
}
857866
}
858867

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

864875
private bool IsMainOrDevelop()
865876
{
866-
var env = Environment.GetEnvironmentVariable("CIRCLE_BRANCH").ToLower();
867-
868-
return env == "main" || env == "develop";
877+
var br = GetBranchName().ToLower();
878+
return br == "main" || br == "develop";
879+
}
880+
private string GetBranchName()
881+
{
882+
if (IsRunningOnCircleCI())
883+
{
884+
return Environment.GetEnvironmentVariable("CIRCLE_BRANCH");
885+
}
886+
else if (IsRunningInGitHubActions())
887+
{
888+
return GetGitHubBranchName();
889+
}
890+
return versioning.BranchName;
891+
}
892+
private string GetGitHubBranchName()
893+
{
894+
string githubRef = Environment.GetEnvironmentVariable("GITHUB_REF");
895+
return (!string.IsNullOrEmpty(githubRef) && githubRef.StartsWith("refs/heads/"))
896+
? githubRef.Substring("refs/heads/".Length)
897+
: "Unknown Branch";
869898
}

0 commit comments

Comments
 (0)