From a2a51329c61d405189334a4be95d40a3f8ae5865 Mon Sep 17 00:00:00 2001 From: Ebere Abanonu Date: Fri, 18 Mar 2022 13:35:16 +0100 Subject: [PATCH] Ported over changes in the Petabridge.Library template (#15) --- .github/workflows/Windows_release.yml | 5 ++ .github/workflows/pr_validation.yml | 16 +++- .nuke/build.schema.json | 10 ++- build/Build.CI.GitHubActions.cs | 35 ++++++-- build/Build.cs | 122 ++++++++++++++++++-------- src/Directory.Build.props | 4 +- 6 files changed, 139 insertions(+), 53 deletions(-) diff --git a/.github/workflows/Windows_release.yml b/.github/workflows/Windows_release.yml index a54b34e9..17581bf3 100644 --- a/.github/workflows/Windows_release.yml +++ b/.github/workflows/Windows_release.yml @@ -27,6 +27,10 @@ jobs: runs-on: windows-latest steps: - uses: actions/checkout@v1 + - name: Make build.sh executable + run: chmod +x ./build.sh + - name: Make build.cmd executable + run: chmod +x ./build.cmd - uses: actions/setup-dotnet@v1 with: dotnet-version: 5.0.* @@ -44,4 +48,5 @@ jobs: run: ./build.cmd NuGet env: Nuget_Key: ${{ secrets.NUGET_KEY }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_CONTEXT: ${{ toJSON(github) }} diff --git a/.github/workflows/pr_validation.yml b/.github/workflows/pr_validation.yml index c3632f14..0cd782e1 100644 --- a/.github/workflows/pr_validation.yml +++ b/.github/workflows/pr_validation.yml @@ -32,6 +32,10 @@ jobs: runs-on: windows-latest steps: - uses: actions/checkout@v1 + - name: Make build.sh executable + run: chmod +x ./build.sh + - name: Make build.cmd executable + run: chmod +x ./build.cmd - uses: actions/setup-dotnet@v1 with: dotnet-version: 5.0.* @@ -45,8 +49,8 @@ jobs: .nuke/temp ~/.nuget/packages key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }} - - name: Run './build.cmd RunTests' - run: ./build.cmd RunTests + - name: Run './build.cmd All' + run: ./build.cmd All env: GITHUB_CONTEXT: ${{ toJSON(github) }} ubuntu-latest: @@ -54,6 +58,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 + - name: Make build.sh executable + run: chmod +x ./build.sh + - name: Make build.cmd executable + run: chmod +x ./build.cmd - uses: actions/setup-dotnet@v1 with: dotnet-version: 5.0.* @@ -67,7 +75,7 @@ jobs: .nuke/temp ~/.nuget/packages key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }} - - name: Run './build.cmd RunTests' - run: ./build.cmd RunTests + - name: Run './build.cmd All' + run: ./build.cmd All env: GITHUB_CONTEXT: ${{ toJSON(github) }} diff --git a/.nuke/build.schema.json b/.nuke/build.schema.json index 044f7630..83216e1d 100644 --- a/.nuke/build.schema.json +++ b/.nuke/build.schema.json @@ -18,6 +18,10 @@ "type": "boolean", "description": "Indicates to continue a previously failed build attempt" }, + "GitHubToken": { + "type": "string", + "default": "Secrets must be entered via 'nuke :secret [profile]'" + }, "Help": { "type": "boolean", "description": "Shows the help text for this build assembly" @@ -103,6 +107,7 @@ "enum": [ "All", "AssemblyInfo", + "AuthenticatedGitHubClient", "BuildRelease", "Clean", "Compile", @@ -110,6 +115,7 @@ "DocFx", "DocsInit", "DocsMetadata", + "GitHubRelease", "Install", "NBench", "Nuget", @@ -118,7 +124,6 @@ "Restore", "RunTests", "ServeDocs", - "SetFilePermission", "SignClient" ] } @@ -134,6 +139,7 @@ "enum": [ "All", "AssemblyInfo", + "AuthenticatedGitHubClient", "BuildRelease", "Clean", "Compile", @@ -141,6 +147,7 @@ "DocFx", "DocsInit", "DocsMetadata", + "GitHubRelease", "Install", "NBench", "Nuget", @@ -149,7 +156,6 @@ "Restore", "RunTests", "ServeDocs", - "SetFilePermission", "SignClient" ] } diff --git a/build/Build.CI.GitHubActions.cs b/build/Build.CI.GitHubActions.cs index 217d2a29..2a2aa9da 100644 --- a/build/Build.CI.GitHubActions.cs +++ b/build/Build.CI.GitHubActions.cs @@ -12,20 +12,20 @@ [CustomGitHubActions("pr_validation", GitHubActionsImage.WindowsLatest, GitHubActionsImage.UbuntuLatest, - AutoGenerate = true, + AutoGenerate = false, OnPushBranches = new[] { "master", "dev" }, OnPullRequestBranches = new[] { "master", "dev" }, - InvokedTargets = new[] { nameof(RunTests) }, - PublishArtifacts = false, + InvokedTargets = new[] { nameof(All) }, + PublishArtifacts = true, EnableGitHubContext = true) ] [CustomGitHubActions("Windows_release", GitHubActionsImage.WindowsLatest, - AutoGenerate = true, - OnPushBranches = new[] { "refs/tags/*" }, + OnPushTags = new[] { "*" }, + AutoGenerate = false, InvokedTargets = new[] { nameof(NuGet) }, - ImportSecrets = new[] { "Nuget_Key" }, + ImportSecrets = new[] { "Nuget_Key", "GITHUB_TOKEN" }, EnableGitHubContext = true, PublishArtifacts = true) ] @@ -50,7 +50,14 @@ protected override GitHubActionsJob GetJobs(GitHubActionsImage image, IReadOnlyC Version = version }); } - + newSteps.Insert(1, new GitHubActionsSetupChmod + { + File = "build.cmd" + }); + newSteps.Insert(1, new GitHubActionsSetupChmod + { + File = "build.sh" + }); job.Steps = newSteps.ToArray(); return job; } @@ -73,4 +80,18 @@ public override void Write(CustomFileWriter writer) } } } +} + +class GitHubActionsSetupChmod : GitHubActionsStep +{ + public string File { get; init; } + + public override void Write(CustomFileWriter writer) + { + writer.WriteLine($"- name: Make {File} executable"); + using (writer.Indent()) + { + writer.WriteLine($"run: chmod +x ./{File}"); + } + } } \ No newline at end of file diff --git a/build/Build.cs b/build/Build.cs index 8a3b15c0..171ba12c 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -23,6 +23,8 @@ using static Nuke.Common.Tools.SignClient.SignClientTasks; using Nuke.Common.Tools.SignClient; using static Nuke.Common.Tools.Git.GitTasks; +using Octokit; +using Nuke.Common.Utilities; [CheckBuildProjectConfigurations] [ShutdownDotNetAfterServerBuild] @@ -42,7 +44,9 @@ partial class Build : NukeBuild [GitRepository] readonly GitRepository GitRepository; [Parameter] string NugetPublishUrl = "https://api.nuget.org/v3/index.json"; - [Parameter] [Secret] string NugetKey; + + [Parameter][Secret] string GitHubToken; + [Parameter][Secret] string NugetKey; [Parameter] int Port = 8090; @@ -57,8 +61,8 @@ partial class Build : NukeBuild [Parameter] string SigningDescription = "My REALLY COOL Library"; [Parameter] string SigningUrl = "https://signing.is.cool/"; - [Parameter] [Secret] string SignClientSecret; - [Parameter] [Secret] string SignClientUser; + [Parameter][Secret] string SignClientSecret; + [Parameter][Secret] string SignClientUser; // Directories AbsolutePath ToolsDir => RootDirectory / "tools"; AbsolutePath Output => RootDirectory / "bin"; @@ -88,7 +92,7 @@ partial class Build : NukeBuild private string VersionFromReleaseNotes => ReleaseNotes.Version.IsPrerelease ? ReleaseNotes.Version.OriginalVersion : ""; private string VersionSuffix => NugetPrerelease == "dev" ? PreReleaseVersionSuffix : NugetPrerelease == "" ? VersionFromReleaseNotes : NugetPrerelease; public string ReleaseVersion => ReleaseNotes.Version?.ToString() ?? throw new ArgumentException("Bad Changelog File. Define at least one version"); - + GitHubClient GitHubClient; Target Clean => _ => _ .Description("Cleans all the output directories") .Before(Restore) @@ -133,8 +137,6 @@ partial class Build : NukeBuild .SetVersionPrefix(version) .SetVersionSuffix(VersionSuffix) .SetPackageReleaseNotes(releaseNotes) - .SetDescription("YOUR_DESCRIPTION_HERE") - .SetPackageProjectUrl("YOUR_PACKAGE_URL_HERE") .SetOutputDirectory(OutputNuget)); } }); @@ -144,35 +146,88 @@ partial class Build : NukeBuild .After(CreateNuget, SignClient) .OnlyWhenDynamic(() => !NugetPublishUrl.IsNullOrEmpty()) .OnlyWhenDynamic(() => !NugetKey.IsNullOrEmpty()) + .Triggers(GitHubRelease) .Executes(() => { var packages = OutputNuget.GlobFiles("*.nupkg", "*.symbols.nupkg").NotNull(); var shouldPublishSymbolsPackages = !string.IsNullOrWhiteSpace(SymbolsPublishUrl); - if (!string.IsNullOrWhiteSpace(NugetPublishUrl)) + foreach (var package in packages) { - foreach (var package in packages) + if (shouldPublishSymbolsPackages) { - if (shouldPublishSymbolsPackages) - { - DotNetNuGetPush(s => s - .SetTimeout(TimeSpan.FromMinutes(10).Minutes) - .SetTargetPath(package) - .SetSource(NugetPublishUrl) - .SetSymbolSource(SymbolsPublishUrl) - .SetApiKey(NugetKey)); - } - else - { - DotNetNuGetPush(s => s - .SetTimeout(TimeSpan.FromMinutes(10).Minutes) - .SetTargetPath(package) - .SetSource(NugetPublishUrl) - .SetApiKey(NugetKey) - ); - } + DotNetNuGetPush(s => s + .SetTimeout(TimeSpan.FromMinutes(10).Minutes) + .SetTargetPath(package) + .SetSource(NugetPublishUrl) + .SetSymbolSource(SymbolsPublishUrl) + .SetApiKey(NugetKey)); + } + else + { + DotNetNuGetPush(s => s + .SetTimeout(TimeSpan.FromMinutes(10).Minutes) + .SetTargetPath(package) + .SetSource(NugetPublishUrl) + .SetApiKey(NugetKey)); } + } }); + Target AuthenticatedGitHubClient => _ => _ + .Unlisted() + .OnlyWhenDynamic(() => !string.IsNullOrWhiteSpace(GitHubToken)) + .Executes(() => + { + GitHubClient = new GitHubClient(new ProductHeaderValue("nuke-build")) + { + Credentials = new Credentials(GitHubToken, AuthenticationType.Bearer) + }; + }); + Target GitHubRelease => _ => _ + .Unlisted() + .Description("Creates a GitHub release (or amends existing) and uploads the artifact") + .OnlyWhenDynamic(() => !string.IsNullOrWhiteSpace(GitHubToken)) + .DependsOn(AuthenticatedGitHubClient) + .Executes(async () => + { + var version = ReleaseNotes.Version.ToString(); + var releaseNotes = GetNuGetReleaseNotes(ChangelogFile); + Release release; + var releaseName = $"{version}"; + if (!VersionSuffix.IsNullOrWhiteSpace()) + releaseName = $"{version}-{VersionSuffix}"; + var identifier = GitRepository.Identifier.Split("/"); + var (gitHubOwner, repoName) = (identifier[0], identifier[1]); + try + { + release = await GitHubClient.Repository.Release.Get(gitHubOwner, repoName, releaseName); + } + catch (NotFoundException) + { + var newRelease = new NewRelease(releaseName) + { + Body = releaseNotes, + Name = releaseName, + Draft = false, + Prerelease = GitRepository.IsOnReleaseBranch() + }; + release = await GitHubClient.Repository.Release.Create(gitHubOwner, repoName, newRelease); + } + + foreach (var existingAsset in release.Assets) + { + await GitHubClient.Repository.Release.DeleteAsset(gitHubOwner, repoName, existingAsset.Id); + } + + Information($"GitHub Release {releaseName}"); + var packages = OutputNuget.GlobFiles("*.nupkg", "*.symbols.nupkg").NotNull(); + foreach (var artifact in packages) + { + var releaseAssetUpload = new ReleaseAssetUpload(artifact.Name, "application/zip", File.OpenRead(artifact), null); + var releaseAsset = await GitHubClient.Repository.Release.UploadAsset(release, releaseAssetUpload); + Information($" {releaseAsset.BrowserDownloadUrl}"); + } + }); Target RunTests => _ => _ .Description("Runs all the unit tests") .DependsOn(Compile) @@ -306,23 +361,21 @@ private AbsolutePath[] GetDockerProjects() Target Compile => _ => _ .Description("Builds all the projects in the solution") - .DependsOn(Restore) + .DependsOn(AssemblyInfo, Restore) .Executes(() => { var version = ReleaseNotes.Version.ToString(); DotNetBuild(s => s .SetProjectFile(Solution) .SetConfiguration(Configuration) - .SetAssemblyVersion(version) - .SetFileVersion(version) - .SetVersion(version) .EnableNoRestore()); }); Target BuildRelease => _ => _ - .DependsOn(Clean, AssemblyInfo, Compile); + .DependsOn(Compile); Target AssemblyInfo => _ => _ + .After(Restore) .Executes(() => { XmlTasks.XmlPoke(SourceDirectory / "Directory.Build.props", "//Project/PropertyGroup/PackageReleaseNotes", GetNuGetReleaseNotes(ChangelogFile)); @@ -330,13 +383,6 @@ private AbsolutePath[] GetDockerProjects() }); - Target SetFilePermission => _ => _ - .Description("User may experience PERMISSION issues - this target be used to fix that!") - .Executes(() => - { - Git($"update-index --chmod=+x {RootDirectory}/build.cmd"); - Git($"update-index --chmod=+x {RootDirectory}/build.sh"); - }); Target Install => _ => _ .Description("Install `Nuke.GlobalTool` and SignClient") .Executes(() => diff --git a/src/Directory.Build.props b/src/Directory.Build.props index b38b15c3..9975e2da 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,8 +2,8 @@ Copyright © 2013-2022 Akka.NET Team Akka.NET Team - 0.3.0 - • Fixed bugs + 0.1.0 + • Initial release of Akka.Hosting%2C Akka.Remote.Hosting%2C Akka.Cluster.Hosting%2C and Akka.Persistence.SqlServer.Hosting