|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using Nuke.Common; |
| 4 | +using Nuke.Common.CI; |
| 5 | +using Nuke.Common.CI.GitHubActions; |
| 6 | +using Nuke.Common.Execution; |
| 7 | +using Nuke.Common.Git; |
| 8 | +using Nuke.Common.IO; |
| 9 | +using Nuke.Common.ProjectModel; |
| 10 | +using Nuke.Common.Tooling; |
| 11 | +using Nuke.Common.Tools.DotNet; |
| 12 | +using Nuke.Common.Tools.GitHub; |
| 13 | +using Nuke.Common.Utilities.Collections; |
| 14 | +using Serilog; |
| 15 | +using static Nuke.Common.EnvironmentInfo; |
| 16 | +using static Nuke.Common.IO.FileSystemTasks; |
| 17 | +using static Nuke.Common.IO.PathConstruction; |
| 18 | +using static Nuke.Common.Tools.DotNet.DotNetTasks; |
| 19 | + |
| 20 | +[GitHubActions( |
| 21 | + "continuous", |
| 22 | + GitHubActionsImage.UbuntuLatest, |
| 23 | + FetchDepth = 0, |
| 24 | + On = new[] { GitHubActionsTrigger.Push }, |
| 25 | + PublishArtifacts = true, |
| 26 | + InvokedTargets = new[] { nameof(Compile), nameof(Pack) })] |
| 27 | +[GitHubActions( |
| 28 | + "release", |
| 29 | + GitHubActionsImage.UbuntuLatest, |
| 30 | + FetchDepth = 0, |
| 31 | + OnPushTags = new[] { @"\d+\.\d+\.\d+" }, |
| 32 | + PublishArtifacts = true, |
| 33 | + InvokedTargets = new[] { nameof(Push), nameof(PushGithubNuget) }, |
| 34 | + ImportSecrets = new[] { nameof(NuGetApiKey), nameof(PersonalAccessToken) })] |
| 35 | +class Build : NukeBuild |
| 36 | +{ |
| 37 | + /// Support plugins are available for: |
| 38 | + /// - JetBrains ReSharper https://nuke.build/resharper |
| 39 | + /// - JetBrains Rider https://nuke.build/rider |
| 40 | + /// - Microsoft VisualStudio https://nuke.build/visualstudio |
| 41 | + /// - Microsoft VSCode https://nuke.build/vscode |
| 42 | + |
| 43 | + public static int Main () => Execute<Build>(x => x.Compile); |
| 44 | + |
| 45 | + [Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")] |
| 46 | + readonly Configuration Configuration = Configuration.Release; |
| 47 | + |
| 48 | + |
| 49 | + [Parameter] string NugetApiUrl = "https://api.nuget.org/v3/index.json"; //default |
| 50 | + |
| 51 | + bool IsTag => GitHubActions.Instance?.Ref?.StartsWith("refs/tags/") ?? false; |
| 52 | + |
| 53 | + [Parameter][Secret] readonly string NuGetApiKey; |
| 54 | + [Parameter][Secret] readonly string PersonalAccessToken; |
| 55 | + |
| 56 | + [Solution] readonly Solution Solution; |
| 57 | + [GitRepository] readonly GitRepository GitRepository; |
| 58 | + |
| 59 | + AbsolutePath SourceDirectory => RootDirectory / "src"; |
| 60 | + AbsolutePath TestsDirectory => RootDirectory / "tests"; |
| 61 | + AbsolutePath ArtifactsDirectory => RootDirectory / "artifacts"; |
| 62 | + AbsolutePath PackagesDirectory => ArtifactsDirectory / "packages"; |
| 63 | + |
| 64 | + Target Clean => _ => _ |
| 65 | + .Before(Restore) |
| 66 | + .Executes(() => |
| 67 | + { |
| 68 | + SourceDirectory.GlobDirectories("*/bin", "*/obj").DeleteDirectories(); |
| 69 | + TestsDirectory.GlobDirectories("*/bin", "*/obj").DeleteDirectories(); |
| 70 | + ArtifactsDirectory.CreateOrCleanDirectory(); |
| 71 | + }); |
| 72 | + |
| 73 | + Target Restore => _ => _ |
| 74 | + .DependsOn(Clean) |
| 75 | + .Executes(() => |
| 76 | + { |
| 77 | + DotNetRestore(s => s |
| 78 | + .SetProjectFile(Solution)); |
| 79 | + }); |
| 80 | + |
| 81 | + Target Compile => _ => _ |
| 82 | + .DependsOn(Restore) |
| 83 | + .Executes(() => |
| 84 | + { |
| 85 | + DotNetBuild(s => s |
| 86 | + .SetProjectFile(Solution) |
| 87 | + .SetConfiguration(Configuration) |
| 88 | + .EnableNoRestore()); |
| 89 | + }); |
| 90 | + |
| 91 | + Target Pack => _ => _ |
| 92 | + .After(Compile) |
| 93 | + .Produces(PackagesDirectory / "*.nupkg") |
| 94 | + .Produces(PackagesDirectory / "*.snupkg") |
| 95 | + .Requires(() => Configuration.Equals(Configuration.Release)) |
| 96 | + .Executes(() => { |
| 97 | + DotNetPack(_ => _ |
| 98 | + .Apply(PackSettings)); |
| 99 | + |
| 100 | + ReportSummary(_ => _ |
| 101 | + .AddPair("Packages", PackagesDirectory.GlobFiles("*.nupkg").Count.ToString())); |
| 102 | + }); |
| 103 | + |
| 104 | + Target Push => _ => _ |
| 105 | + .DependsOn(Pack) |
| 106 | + .OnlyWhenStatic(() => IsTag && IsServerBuild) |
| 107 | + .Requires(() => NuGetApiKey) |
| 108 | + .Requires(() => Configuration.Equals(Configuration.Release)) |
| 109 | + .Executes(() => |
| 110 | + { |
| 111 | + Log.Information("Running push to packages directory."); |
| 112 | + |
| 113 | + Assert.True(!string.IsNullOrEmpty(NuGetApiKey)); |
| 114 | + |
| 115 | + PackagesDirectory.GlobFiles("*.nupkg") |
| 116 | + .ForEach(x => |
| 117 | + { |
| 118 | + x.NotNull(); |
| 119 | + |
| 120 | + DotNetNuGetPush(s => s |
| 121 | + .SetTargetPath(x) |
| 122 | + .SetSource(NugetApiUrl) |
| 123 | + .SetApiKey(NuGetApiKey) |
| 124 | + .EnableSkipDuplicate() |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + }); |
| 129 | + |
| 130 | + Target PushGithubNuget => _ => _ |
| 131 | + .DependsOn(Pack) |
| 132 | + .OnlyWhenStatic(() => IsTag && IsServerBuild) |
| 133 | + .Requires(() => PersonalAccessToken) |
| 134 | + .Requires(() => Configuration.Equals(Configuration.Release)) |
| 135 | + .Executes(() => |
| 136 | + { |
| 137 | + Log.Information("Running push to packages directory."); |
| 138 | + |
| 139 | + PackagesDirectory.GlobFiles("*.nupkg") |
| 140 | + .ForEach(x => |
| 141 | + { |
| 142 | + x.NotNull(); |
| 143 | + |
| 144 | + Assert.True(!string.IsNullOrEmpty(PersonalAccessToken)); |
| 145 | + |
| 146 | + DotNetNuGetPush(s => s |
| 147 | + .SetTargetPath(x) |
| 148 | + .SetSource($"https://nuget.pkg.github.com/{GitHubActions.Instance.RepositoryOwner}/index.json") |
| 149 | + .SetApiKey(PersonalAccessToken) |
| 150 | + .EnableSkipDuplicate() |
| 151 | + ); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + Configure<DotNetPackSettings> PackSettings => _ => _ |
| 156 | + .SetProject(Solution) |
| 157 | + .SetConfiguration(Configuration) |
| 158 | + .SetNoBuild(SucceededTargets.Contains(Compile)) |
| 159 | + .SetOutputDirectory(PackagesDirectory); |
| 160 | + |
| 161 | +} |
0 commit comments