22
22
using static Nuke . Common . Tools . Git . GitTasks ;
23
23
using Nuke . Common . ChangeLog ;
24
24
using System . Collections . Generic ;
25
+ using Octokit ;
25
26
26
27
[ CheckBuildProjectConfigurations ]
27
28
[ DotNetVerbosityMapping ]
@@ -45,8 +46,6 @@ partial class Build : NukeBuild
45
46
[ GitRepository ] readonly GitRepository GitRepository ;
46
47
[ Required ] [ GitVersion ( Framework = "net6.0" ) ] readonly GitVersion GitVersion ;
47
48
48
- readonly string _githubContext = EnvironmentInfo . GetVariable < string > ( "GITHUB_CONTEXT" ) ;
49
-
50
49
[ Parameter ] string NugetApiUrl = "https://api.nuget.org/v3/index.json" ;
51
50
52
51
[ Parameter ] [ Secret ] string NuGetApiKey ;
@@ -62,7 +61,7 @@ partial class Build : NukeBuild
62
61
AbsolutePath Output => RootDirectory / "output" ;
63
62
AbsolutePath ArtifactsDirectory => RootDirectory / "artifacts" ;
64
63
AbsolutePath TestSourceDirectory => RootDirectory / "AvroSchemaGenerator.Tests" ;
65
-
64
+ GitHubClient GitHubClient ;
66
65
public ChangeLog Changelog => ReadChangelog ( ChangelogFile ) ;
67
66
68
67
public ReleaseNotes LatestVersion => Changelog . ReleaseNotes . OrderByDescending ( s => s . Version ) . FirstOrDefault ( ) ?? throw new ArgumentException ( "Bad Changelog File. Version Should Exist" ) ;
@@ -175,6 +174,7 @@ internal static class AssemblyVersionInformation {
175
174
. Requires ( ( ) => NugetApiUrl )
176
175
. Requires ( ( ) => ! NuGetApiKey . IsNullOrEmpty ( ) )
177
176
. Requires ( ( ) => Configuration . Equals ( Configuration . Release ) )
177
+ . Triggers ( GitHubRelease )
178
178
. Executes ( ( ) =>
179
179
{
180
180
GlobFiles ( OutputNuget , "*.nupkg" )
@@ -189,6 +189,65 @@ internal static class AssemblyVersionInformation {
189
189
) ;
190
190
} ) ;
191
191
} ) ;
192
+
193
+ Target AuthenticatedGitHubClient => _ => _
194
+ . Unlisted ( )
195
+ . OnlyWhenDynamic ( ( ) => ! string . IsNullOrWhiteSpace ( GitHubActions . Token ) )
196
+ . Executes ( ( ) =>
197
+ {
198
+ GitHubClient = new GitHubClient ( new ProductHeaderValue ( "nuke-build" ) )
199
+ {
200
+ Credentials = new Credentials ( GitHubActions . Token , AuthenticationType . Bearer )
201
+ } ;
202
+ } ) ;
203
+ Target GitHubRelease => _ => _
204
+ . Unlisted ( )
205
+ . Description ( "Creates a GitHub release (or amends existing) and uploads the artifact" )
206
+ . OnlyWhenDynamic ( ( ) => ! string . IsNullOrWhiteSpace ( GitHubActions . Token ) )
207
+ . DependsOn ( AuthenticatedGitHubClient )
208
+ . Executes ( async ( ) =>
209
+ {
210
+ var version = GitVersion . NuGetVersionV2 ;
211
+ var releaseNotes = GetNuGetReleaseNotes ( ChangelogFile ) ;
212
+ Release release ;
213
+
214
+
215
+ var identifier = GitRepository . Identifier . Split ( "/" ) ;
216
+ var ( gitHubOwner , repoName ) = ( identifier [ 0 ] , identifier [ 1 ] ) ;
217
+ try
218
+ {
219
+ release = await GitHubClient . Repository . Release . Get ( gitHubOwner , repoName , version ) ;
220
+ }
221
+ catch ( NotFoundException )
222
+ {
223
+ var newRelease = new NewRelease ( version )
224
+ {
225
+ Body = releaseNotes ,
226
+ Name = version ,
227
+ Draft = false ,
228
+ Prerelease = GitRepository . IsOnReleaseBranch ( )
229
+ } ;
230
+ release = await GitHubClient . Repository . Release . Create ( gitHubOwner , repoName , newRelease ) ;
231
+ }
232
+
233
+ foreach ( var existingAsset in release . Assets )
234
+ {
235
+ await GitHubClient . Repository . Release . DeleteAsset ( gitHubOwner , repoName , existingAsset . Id ) ;
236
+ }
237
+
238
+ Information ( $ "GitHub Release { version } ") ;
239
+ var packages = OutputNuget . GlobFiles ( "*.nupkg" , "*.symbols.nupkg" ) . NotNull ( ) ;
240
+ foreach ( var artifact in packages )
241
+ {
242
+ var releaseAssetUpload = new ReleaseAssetUpload ( artifact . Name , "application/zip" , File . OpenRead ( artifact ) , null ) ;
243
+ var releaseAsset = await GitHubClient . Repository . Release . UploadAsset ( release , releaseAssetUpload ) ;
244
+ Information ( $ " { releaseAsset . BrowserDownloadUrl } ") ;
245
+ }
246
+ } ) ;
247
+ string ParseReleaseNote ( )
248
+ {
249
+ return XmlTasks . XmlPeek ( RootDirectory / "Directory.Build.props" , "//Project/PropertyGroup/PackageReleaseNotes" ) . FirstOrDefault ( ) ;
250
+ }
192
251
static void Information ( string info )
193
252
{
194
253
Serilog . Log . Information ( info ) ;
0 commit comments