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
2
2
#tool dotnet: ? package = coveralls . net & version = 4.0 .1
3
3
#tool nuget: ? package = ReportGenerator & version = 5.2 .4
4
4
#addin nuget: ? package = Newtonsoft . Json & version = 13.0 .3
@@ -14,8 +14,10 @@ using System.Linq;
14
14
using System. Text. RegularExpressions;
15
15
16
16
const string Release = "Release" ; // task name, target, and Release config name
17
+ const string PullRequest = "PullRequest" ; // task name, target, and PullRequest config name
17
18
const string AllFrameworks = "net8.0;net9.0" ;
18
19
const string LatestFramework = "net9.0" ;
20
+ static string NL = Environment. NewLine;
19
21
20
22
var compileConfig = Argument( "configuration" , Release ) ; // compile
21
23
@@ -50,16 +52,16 @@ var releaseNotes = new List<string>();
50
52
// internal build variables - don't change these.
51
53
string committedVersion = "0.0.0-dev" ;
52
54
GitVersion versioning = null ;
53
- bool IsTechnicalRelease = false ;
55
+ bool IsTechnicalRelease = true ;
54
56
55
57
var target = Argument( "target" , "Default" ) ;
56
58
var slnFile = ( target = = Release ) ? $"./Ocelot.{Release}.sln" : "./Ocelot.sln" ;
57
- Information( " \n Target: " + target ) ;
58
- Information( "Build: " + compileConfig ) ;
59
- Information( "Solution: " + slnFile ) ;
59
+ Information( $ " { NL } Target: { target } " ) ;
60
+ Information( $ "Build: { compileConfig } " ) ;
61
+ Information( $ "Solution: { slnFile } " ) ;
60
62
61
63
TaskTeardown( context => {
62
- AnsiConsole . Markup ( $ "[green]DONE[/] { context . Task . Name } \n " ) ;
64
+ AnsiConsole . Markup ( $ "[green]DONE[/] { context . Task . Name } " + NL ) ;
63
65
} ) ;
64
66
65
67
Task( "Default" )
@@ -88,13 +90,14 @@ Task("Compile")
88
90
. IsDependentOn ( "Version" )
89
91
. Does ( ( ) =>
90
92
{
93
+ Information ( "Branch: " + GetBranchName ( ) ) ;
91
94
Information ( "Build: " + compileConfig ) ;
92
95
Information ( "Solution: " + slnFile ) ;
93
96
var settings = new DotNetBuildSettings
94
97
{
95
98
Configuration = compileConfig ,
96
99
} ;
97
- if ( target != Release )
100
+ if ( target == PullRequest )
98
101
{
99
102
settings . Framework = LatestFramework ; // build using .NET 9 SDK only
100
103
}
@@ -121,13 +124,20 @@ Task("Version")
121
124
. Does ( ( ) =>
122
125
{
123
126
versioning = GetNuGetVersionForCommit ( ) ;
124
- var nugetVersion = versioning . NuGetVersion ;
125
- Information ( "SemVer version number: " + nugetVersion ) ;
126
-
127
- if ( IsRunningOnCircleCI ( ) )
127
+ Information ( "#########################" ) ;
128
+ Information ( "# SemVer Information" ) ;
129
+ Information ( "#========================" ) ;
130
+ Information ( $ "# { nameof ( versioning . NuGetVersion ) } : { versioning . NuGetVersion } ") ;
131
+ Information ( $ "# { nameof ( versioning . BranchName ) } : { versioning . BranchName } ") ;
132
+ Information ( $ "# { nameof ( versioning . MajorMinorPatch ) } : { versioning . MajorMinorPatch } ") ;
133
+ Information ( $ "# { nameof ( versioning . SemVer ) } : { versioning . SemVer } ") ;
134
+ Information ( $ "# { nameof ( versioning . InformationalVersion ) } : { versioning . InformationalVersion } ") ;
135
+ Information ( "#########################" ) ;
136
+
137
+ if ( IsRunningInCICD ( ) )
128
138
{
129
139
Information ( "Persisting version number..." ) ;
130
- PersistVersion ( committedVersion , nugetVersion ) ;
140
+ PersistVersion ( committedVersion , versioning . SemVer ) ;
131
141
}
132
142
else
133
143
{
@@ -139,17 +149,17 @@ Task("GitLogUniqContributors")
139
149
. Does ( ( ) =>
140
150
{
141
151
var command = "log --format=\" %aN|%aE\" " ;
142
- // command += IsRunningOnCircleCI () ? "| sort | uniq" :
152
+ // command += IsRunningInCICD () ? "| sort | uniq" :
143
153
// IsRunningInPowershell() ? "| Sort-Object -Unique" : "| sort | uniq";
144
154
List < string > output = GitHelper ( command ) ;
145
155
output . Sort ( ) ;
146
156
List < string > contributors = output . Distinct ( ) . ToList ( ) ;
147
157
contributors . Sort ( ) ;
148
158
Information ( $ "Detected { contributors . Count } unique contributors:") ;
149
- Information ( string . Join ( Environment . NewLine , contributors ) ) ;
159
+ Information ( string . Join ( NL , contributors ) ) ;
150
160
// TODO Search example in bash: curl -L -H "X-GitHub-Api-Version: 2022-11-28" "https://api.github.com/search/users?q=Chris+Swinchatt"
151
- Information ( Environment . NewLine + "Unicode test: 1) Raynald Messié; 2) 彭伟 pengweiqhca" ) ;
152
- AnsiConsole . Markup ( "Unicode test: 1) Raynald Messié; 2) 彭伟 pengweiqhca" + Environment . NewLine ) ;
161
+ Information ( NL + "Unicode test: 1) Raynald Messié; 2) 彭伟 pengweiqhca" ) ;
162
+ AnsiConsole . Markup ( "Unicode test: 1) Raynald Messié; 2) 彭伟 pengweiqhca" + NL ) ;
153
163
// Powershell life hack: $OutputEncoding = [Console]::InputEncoding = [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding
154
164
// https://stackoverflow.com/questions/40098771/changing-powershells-default-output-encoding-to-utf-8
155
165
// https://stackoverflow.com/questions/49476326/displaying-unicode-in-powershell/49481797#49481797
@@ -164,7 +174,7 @@ Task("CreateReleaseNotes")
164
174
Information ( $ "Generating release notes at { releaseNotesFile } ") ;
165
175
var lastReleaseTags = GitHelper ( "describe --tags --abbrev=0 --exclude net*" ) ;
166
176
var lastRelease = lastReleaseTags . First ( t => ! t . StartsWith ( "net" ) ) ; // skip 'net*-vX.Y.Z' tag and take 'major.minor.build'
167
- var releaseVersion = versioning . NuGetVersion ;
177
+ var releaseVersion = versioning . SemVer ;
168
178
169
179
// Read main header from Git file, substitute version in header, and add content further...
170
180
Information ( "{0} New release tag is " + releaseVersion ) ;
@@ -209,7 +219,7 @@ Task("CreateReleaseNotes")
209
219
{
210
220
starring . Add ( CreateStars ( contributor . Commits , contributor . Author ) ) ;
211
221
}
212
- Information ( string . Join ( Environment . NewLine , starring ) ) ;
222
+ Information ( string . Join ( NL , starring ) ) ;
213
223
214
224
var commitsGrouping = summary
215
225
. GroupBy ( x => x . Commits )
@@ -223,7 +233,7 @@ Task("CreateReleaseNotes")
223
233
byInsertions : ( log , group , fGroup , insGroup ) => CreateStars ( group . Commits , insGroup . Contributors . First ( ) . Contributor ) ,
224
234
byDeletions : ( log , group , fGroup , insGroup , contributor ) => CreateStars ( group . Commits , contributor . Contributor ) ) ;
225
235
Information ( "------==< New Starring >==------" ) ;
226
- Information ( string . Join ( Environment . NewLine , starring ) ) ;
236
+ Information ( string . Join ( NL , starring ) ) ;
227
237
228
238
// Honoring aka Top Contributors
229
239
var coreTeamNames = new List < string > { "Raman Maksimchuk" , "Raynald Messié" , "Guillaume Gnaegi" } ; // Ocelot Core team members should not be in Top 3 Chart
@@ -266,7 +276,7 @@ Task("CreateReleaseNotes")
266
276
return HonorForDeletions ( place , contributor . Contributor , group . Commits , contributor . Files , contributor . Insertions , contributor . Deletions ) ;
267
277
} ) ;
268
278
Information ( "---==< TOP Contributors >==---" ) ;
269
- Information ( string . Join ( Environment . NewLine , topContributors ) ) ;
279
+ Information ( string . Join ( NL , topContributors ) ) ;
270
280
271
281
// local helpers
272
282
static string Place ( int i ) => ++ i == 1 ? "1st" : i == 2 ? "2nd" : i == 3 ? "3rd" : $ "{ i } th";
@@ -436,18 +446,14 @@ private List<string> GitHelper(string command)
436
446
private void WriteReleaseNotes( )
437
447
{
438
448
Information( $ "RUN { nameof ( WriteReleaseNotes ) } ...") ;
439
-
440
449
EnsureDirectoryExists( packagesDir ) ;
441
450
System. IO . File . WriteAllLines ( releaseNotesFile , releaseNotes , Encoding . UTF8 ) ;
442
-
443
451
var content = System. IO . File . ReadAllText ( releaseNotesFile , Encoding . UTF8 ) ;
444
452
if ( string . IsNullOrEmpty ( content ) )
445
453
{
446
454
System. IO . File . WriteAllText ( releaseNotesFile , "No commits since last release" ) ;
447
455
}
448
-
449
- Information( "Release notes are >>>\n {0}<<<" , content ) ;
450
- //Information($"EXITED {nameof(WriteReleaseNotes)}");
456
+ Information( "Release notes are >>>{0}<<<" , NL + content ) ;
451
457
}
452
458
453
459
Task( "RunUnitTests" )
@@ -465,7 +471,7 @@ Task("RunUnitTests")
465
471
. Append ( "--verbosity:detailed" )
466
472
. Append ( "--consoleLoggerParameters:ErrorsOnly" )
467
473
} ;
468
- if ( target != Release )
474
+ if ( target == PullRequest )
469
475
{
470
476
settings . Framework = LatestFramework ; // .NET 9 SDK only
471
477
}
@@ -482,7 +488,7 @@ Task("RunUnitTests")
482
488
483
489
GenerateReport ( coverageSummaryFile ) ;
484
490
485
- if ( IsRunningOnCircleCI ( ) && IsMainOrDevelop ( ) )
491
+ if ( IsRunningInCICD ( ) && IsMainOrDevelop ( ) )
486
492
{
487
493
var repoToken = EnvironmentVariable ( coverallsRepoToken ) ;
488
494
if ( string . IsNullOrEmpty ( repoToken ) )
@@ -512,9 +518,9 @@ Task("RunUnitTests")
512
518
513
519
Information ( "Sequence Coverage: " + sequenceCoverage ) ;
514
520
515
- if ( double . Parse ( sequenceCoverage ) < minCodeCoverage )
521
+ if ( double . Parse ( sequenceCoverage ) < minCodeCoverage )
516
522
{
517
- var whereToCheck = ! IsRunningOnCircleCI ( ) ? coverallsRepo : artifactsForUnitTestsDir ;
523
+ var whereToCheck = ! IsRunningInCICD ( ) ? coverallsRepo : artifactsForUnitTestsDir ;
518
524
throw new Exception ( string . Format ( "Code coverage fell below the threshold of {0}%. You can find the code coverage report at {1}" , minCodeCoverage , whereToCheck ) ) ;
519
525
} ;
520
526
} ) ;
@@ -530,7 +536,7 @@ Task("RunAcceptanceTests")
530
536
. Append ( "--no-restore" )
531
537
. Append ( "--no-build" )
532
538
} ;
533
- if ( target != Release )
539
+ if ( target == PullRequest )
534
540
{
535
541
settings . Framework = LatestFramework ; // .NET 9 SDK only
536
542
}
@@ -551,7 +557,7 @@ Task("RunIntegrationTests")
551
557
. Append ( "--no-restore" )
552
558
. Append ( "--no-build" )
553
559
} ;
554
- if ( target != Release )
560
+ if ( target == PullRequest )
555
561
{
556
562
settings . Framework = LatestFramework ; // .NET 9 SDK only
557
563
}
@@ -602,7 +608,7 @@ Task("PublishGitHubRelease")
602
608
. IsDependentOn ( "CreateArtifacts" )
603
609
. Does ( ( ) =>
604
610
{
605
- if ( ! IsRunningOnCircleCI ( ) ) return ;
611
+ if ( ! IsRunningInCICD ( ) ) return ;
606
612
607
613
dynamic release = CreateGitHubRelease ( ) ;
608
614
var path = packagesDir . ToString ( ) + @"/**/*" ;
@@ -619,9 +625,9 @@ Task("EnsureStableReleaseRequirements")
619
625
{
620
626
Information ( "Check if stable release..." ) ;
621
627
622
- if ( ! IsRunningOnCircleCI ( ) )
628
+ if ( ! IsRunningInCICD ( ) )
623
629
{
624
- throw new Exception ( "Stable release should happen via circleci " ) ;
630
+ throw new Exception ( "Stable release should happen via CI/CD " ) ;
625
631
}
626
632
627
633
Information ( "Release is stable..." ) ;
@@ -636,7 +642,7 @@ Task("DownloadGitHubReleaseArtifacts")
636
642
System . Threading . Thread . Sleep ( 5000 ) ;
637
643
EnsureDirectoryExists ( packagesDir ) ;
638
644
639
- var releaseUrl = "https://api.github.com/repos/ThreeMammals/ocelot/releases/tags/" + versioning . NuGetVersion ;
645
+ var releaseUrl = "https://api.github.com/repos/ThreeMammals/ocelot/releases/tags/" + versioning . SemVer ;
640
646
var releaseInfo = await GetResourceAsync ( releaseUrl ) ;
641
647
var assets_url = Newtonsoft . Json . Linq . JObject . Parse ( releaseInfo )
642
648
. Value < string > ( "assets_url" ) ;
@@ -665,7 +671,7 @@ Task("PublishToNuget")
665
671
return ;
666
672
}
667
673
668
- if ( IsRunningOnCircleCI ( ) )
674
+ if ( IsRunningInCICD ( ) )
669
675
{
670
676
// stable releases
671
677
var nugetFeedStableKey = EnvironmentVariable ( "OCELOT_NUGET_API_KEY_3Mammals" ) ;
@@ -762,7 +768,7 @@ private void SetupGitHubClient(System.Net.Http.HttpClient client)
762
768
763
769
private dynamic CreateGitHubRelease( )
764
770
{
765
- var json = $ "{{ \" tag_name\" : \" { versioning . NuGetVersion } \" , \" target_commitish\" : \" main\" , \" name\" : \" { versioning . NuGetVersion } \" , \" body\" : \" { ReleaseNotesAsJson ( ) } \" , \" draft\" : true, \" prerelease\" : true, \" generate_release_notes\" : false }}";
771
+ var json = $ "{{ \" tag_name\" : \" { versioning . SemVer } \" , \" target_commitish\" : \" main\" , \" name\" : \" { versioning . SemVer } \" , \" body\" : \" { ReleaseNotesAsJson ( ) } \" , \" draft\" : true, \" prerelease\" : true, \" generate_release_notes\" : false }}";
766
772
var content = new System. Net . Http . StringContent ( json , System . Text . Encoding . UTF8 , "application/json" ) ;
767
773
768
774
using ( var client = new System . Net . Http . HttpClient ( ) )
@@ -816,7 +822,7 @@ private void CompleteGitHubRelease(dynamic release)
816
822
{
817
823
int releaseId = release. id ;
818
824
string url = release . url . ToString ( ) ;
819
- var json = $ "{{ \" tag_name\" : \" { versioning . NuGetVersion } \" , \" target_commitish\" : \" main\" , \" name\" : \" { versioning . NuGetVersion } \" , \" body\" : \" { ReleaseNotesAsJson ( ) } \" , \" draft\" : false, \" prerelease\" : false }}";
825
+ var json = $ "{{ \" tag_name\" : \" { versioning . SemVer } \" , \" target_commitish\" : \" main\" , \" name\" : \" { versioning . SemVer } \" , \" body\" : \" { ReleaseNotesAsJson ( ) } \" , \" draft\" : false, \" prerelease\" : false }}";
820
826
var request = new System. Net . Http . HttpRequestMessage ( new System . Net . Http . HttpMethod ( "Patch" ) , url ) ; // $"https://api.github.com/repos/ThreeMammals/Ocelot/releases/{releaseId}");
821
827
request. Content = new System . Net . Http . StringContent ( json , System . Text . Encoding . UTF8 , "application/json" ) ;
822
828
@@ -846,7 +852,7 @@ private async Task<string> GetResourceAsync(string url)
846
852
using var response = await client. GetAsync ( url ) ;
847
853
response. EnsureSuccessStatusCode ( ) ;
848
854
var content = await response. Content . ReadAsStringAsync ( ) ;
849
- Information( "Response is >>>" + Environment . NewLine + content + Environment . NewLine + "<<<" ) ;
855
+ Information( "Response is >>>" + NL + content + NL + "<<<" ) ;
850
856
return content;
851
857
}
852
858
catch( Exception exception )
@@ -856,14 +862,34 @@ private async Task<string> GetResourceAsync(string url)
856
862
}
857
863
}
858
864
865
+ private bool IsRunningInCICD( )
866
+ => IsRunningOnCircleCI( ) || IsRunningInGitHubActions ( ) ;
859
867
private bool IsRunningOnCircleCI ( )
860
- {
861
- return ! string . IsNullOrWhiteSpace ( Environment . GetEnvironmentVariable ( "CIRCLECI" ) ) ;
862
- }
868
+ => ! string . IsNullOrWhiteSpace ( Environment . GetEnvironmentVariable ( "CIRCLECI" ) ) ;
869
+ private bool IsRunningInGitHubActions ( )
870
+ => Environment . GetEnvironmentVariable ( "GITHUB_ACTIONS" ) == "true" ;
863
871
864
872
private bool IsMainOrDevelop ( )
865
873
{
866
- var env = Environment. GetEnvironmentVariable ( "CIRCLE_BRANCH" ) . ToLower ( ) ;
867
-
868
- return env == "main" || env == "develop" ;
874
+ var br = GetBranchName( ) . ToLower( ) ;
875
+ return br = = "main" || br == "develop" ;
876
+ }
877
+ private string GetBranchName ( )
878
+ {
879
+ if ( IsRunningOnCircleCI ( ) )
880
+ {
881
+ return Environment. GetEnvironmentVariable( "CIRCLE_BRANCH");
882
+ }
883
+ else if ( IsRunningInGitHubActions ( ) )
884
+ {
885
+ return GetGitHubBranchName ( ) ;
886
+ }
887
+ return versioning . BranchName ;
888
+ }
889
+ private string GetGitHubBranchName ( )
890
+ {
891
+ string githubRef = Environment . GetEnvironmentVariable ( "GITHUB_REF") ;
892
+ return ( ! string . IsNullOrEmpty ( githubRef ) && githubRef . StartsWith ( "refs/heads/" ) )
893
+ ? githubRef . Substring ( "refs/heads/" . Length )
894
+ : "Unknown Branch" ;
869
895
}
0 commit comments