1+ using System . CodeDom ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
14using Nuke . Common . Git ;
5+ using Nuke . Common . IO ;
6+ using Nuke . Common . Tools . GitHub ;
7+ using static Nuke . Common . Tools . DotNet . DotNetTasks ;
8+ using static Nuke . Common . Tools . NuGet . NuGetTasks ;
29
3- [ GitHubActions ( "pack" , GitHubActionsImage . WindowsLatest ,
10+ [
11+ ShutdownDotNetAfterServerBuild ,
12+ GitHubActions ( "pack" , GitHubActionsImage . WindowsLatest ,
413 InvokedTargets = [ nameof ( Pack ) ] ,
514 AutoGenerate = true ,
615 PublishArtifacts = true ,
716 On =
817 [
918 GitHubActionsTrigger . WorkflowDispatch ,
1019 GitHubActionsTrigger . PullRequest
11- ] ) ]
20+ ] ) ,
21+ GitHubActions ( "deploy" , GitHubActionsImage . WindowsLatest ,
22+ InvokedTargets = [ nameof ( Publish ) ] ,
23+ AutoGenerate = true ,
24+ PublishArtifacts = true ,
25+ ReadPermissions = [ GitHubActionsPermissions . Contents ] ,
26+ WritePermissions = [ GitHubActionsPermissions . Packages ] ,
27+ OnPushTags = [ "v*" ] )
28+ ]
1229class Build : NukeBuild
1330{
1431 [ GitRepository ]
@@ -25,30 +42,69 @@ class Build : NukeBuild
2542 Target Clean => _ => _
2643 . Executes ( ( ) =>
2744 {
28- PackagesDirectory . DeleteDirectory ( ) ;
45+ PackagesDirectory . CreateOrCleanDirectory ( ) ;
2946 } ) ;
3047
48+ [ Parameter ]
49+ string Version ;
50+
3151 Target Pack => _ => _
3252 . DependsOn ( Test )
3353 . Produces ( PackagesDirectory / "*.nupkg" )
3454 . Executes ( ( ) =>
3555 {
36-
37- NuGetTasks . NuGetPack ( options => options
56+ var version = Version
57+ ?? GitRepository . Tags ? . FirstOrDefault ( t => t != null && t . StartsWith ( 'v' ) ) ? . TrimStart ( 'v' ) ;
58+
59+ if ( version == null )
60+ Assert . Fail ( "Could not find a version specified for this release" ) ;
61+
62+ return NuGetPack ( options => options
63+ . SetVersion ( version )
3864 . SetProcessWorkingDirectory ( NugetDirectory )
3965 . SetOutputDirectory ( PackagesDirectory ) ) ;
4066 } ) ;
4167
4268 Target Test => _ => _
4369 . OnlyWhenStatic ( ( ) => IsWin )
70+ . Executes ( ( ) =>
71+ DotNetPublish ( options => options
72+ . SetProject ( Solution . Tests . TestTarget )
73+ . SetRuntime ( DotNetRuntimeIdentifier . win_x64 )
74+ . SetConfiguration ( Configuration ) ) ) ;
75+
76+ [ Secret , Parameter ( "Private Access Token for publishing Nuget packages to GitHub" ) ]
77+ string GithubNugetPAT ;
78+
79+ Target Publish => _ => _
80+ . OnlyWhenStatic ( ( ) => GitRepository . IsOnMainBranch ( ) )
81+ . DependsOn ( Pack )
4482 . Executes ( ( ) =>
4583 {
46- DotNetTasks . DotNetPublish ( options => options
47- . SetProject ( Solution . Tests . TestTarget )
48- . SetRuntime ( DotNetRuntimeIdentifier . win_x64 )
49- . SetConfiguration ( Configuration ) ) ;
50- } ) ;
84+ if ( string . IsNullOrWhiteSpace ( GithubNugetPAT ) )
85+ GithubNugetPAT = GitHubActions . Instance . Token ;
86+ var source = $ "https://nuget.pkg.github.com/{ GitRepository . GetGitHubOwner ( ) } /index.json";
87+
88+ var preExisting = true ;
89+ if ( ! NuGetSourcesList ( ) . Any ( x => x . Text . Contains ( "github" ) ) )
90+ {
91+ preExisting = false ;
92+ NuGetSourcesAdd ( options => options
93+ . SetName ( "github" )
94+ . SetUserName ( GitRepository . GetGitHubOwner ( ) )
95+ . SetPassword ( GithubNugetPAT )
96+ . SetSource ( source ) ) ;
97+ }
5198
99+ NuGetPush ( options => options
100+ . SetApiKey ( GithubNugetPAT )
101+ . SetSource ( source )
102+ . SetTargetPath ( ( PackagesDirectory / "*.nupkg" ) . GlobFiles ( ) . First ( ) ) ) ;
103+
104+ if ( ! preExisting )
105+ NuGetSourcesRemove ( options => options
106+ . SetName ( "github" ) ) ;
107+ } ) ;
52108
53109
54110 // Paths
0 commit comments