From 8e59754f842634fc10e89e4671fa657b3b9c9d5f Mon Sep 17 00:00:00 2001 From: JT Date: Tue, 18 Apr 2023 21:01:12 +0800 Subject: [PATCH] Use auto-generated pipeline in Nuke (#10) * Use auto-generation of pipeline in Nuke * Only run builds against main --- .github/workflows/Build_&_Test.yml | 45 +++++++++++++++++++++++++ .github/workflows/Manual_Nuget_Push.yml | 37 ++++++++++++++++++++ .github/workflows/build.yml | 27 --------------- .github/workflows/nuget-push.yml | 24 ------------- .nuke/build.schema.json | 16 +++++++++ build/Build.cs | 42 +++++++++++++++++++---- build/_build.csproj | 2 +- 7 files changed, 135 insertions(+), 58 deletions(-) create mode 100644 .github/workflows/Build_&_Test.yml create mode 100644 .github/workflows/Manual_Nuget_Push.yml delete mode 100644 .github/workflows/build.yml delete mode 100644 .github/workflows/nuget-push.yml diff --git a/.github/workflows/Build_&_Test.yml b/.github/workflows/Build_&_Test.yml new file mode 100644 index 0000000..6e49e01 --- /dev/null +++ b/.github/workflows/Build_&_Test.yml @@ -0,0 +1,45 @@ +# ------------------------------------------------------------------------------ +# +# +# This code was generated. +# +# - To turn off auto-generation set: +# +# [GitHubActions (AutoGenerate = false)] +# +# - To trigger manual generation invoke: +# +# nuke --generate-configuration GitHubActions_Build_&_Test --host GitHubActions +# +# +# ------------------------------------------------------------------------------ + +name: Build_&_Test + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + ubuntu-latest: + name: ubuntu-latest + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Cache .nuke/temp, ~/.nuget/packages + uses: actions/cache@v3 + with: + path: | + .nuke/temp + ~/.nuget/packages + key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }} + - name: Run './build.cmd Test' + run: ./build.cmd Test + env: + FgaStoreId: ${{ secrets.FGA_STORE_ID }} + FgaClientId: ${{ secrets.FGA_CLIENT_ID }} + FgaClientSecret: ${{ secrets.FGA_CLIENT_SECRET }} diff --git a/.github/workflows/Manual_Nuget_Push.yml b/.github/workflows/Manual_Nuget_Push.yml new file mode 100644 index 0000000..5d17b54 --- /dev/null +++ b/.github/workflows/Manual_Nuget_Push.yml @@ -0,0 +1,37 @@ +# ------------------------------------------------------------------------------ +# +# +# This code was generated. +# +# - To turn off auto-generation set: +# +# [GitHubActions (AutoGenerate = false)] +# +# - To trigger manual generation invoke: +# +# nuke --generate-configuration GitHubActions_Manual_Nuget_Push --host GitHubActions +# +# +# ------------------------------------------------------------------------------ + +name: Manual_Nuget_Push + +on: [workflow_dispatch] + +jobs: + ubuntu-latest: + name: ubuntu-latest + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Cache .nuke/temp, ~/.nuget/packages + uses: actions/cache@v3 + with: + path: | + .nuke/temp + ~/.nuget/packages + key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }} + - name: Run './build.cmd NugetPush' + run: ./build.cmd NugetPush + env: + NugetApiKey: ${{ secrets.NUGET_API_KEY }} diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 7c0d0f3..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Build & Test - -on: - push: - branches: [ main ] - pull_request: - branches: [ main ] - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - - name: Setup .NET - uses: actions/setup-dotnet@v3 - with: - dotnet-version: '7.0.x' - - - name: Run build & test - run: ./build.sh Test - shell: bash - env: - AUTH0FGA__STOREID: ${{ secrets.FGA_STOREID }} - AUTH0FGA__CLIENTID: ${{ secrets.FGA_CLIENTID }} - AUTH0FGA__CLIENTSECRET: ${{ secrets.FGA_CLIENTSECRET }} diff --git a/.github/workflows/nuget-push.yml b/.github/workflows/nuget-push.yml deleted file mode 100644 index ebe1d47..0000000 --- a/.github/workflows/nuget-push.yml +++ /dev/null @@ -1,24 +0,0 @@ -name: NuGet Manual Push - -on: [workflow_dispatch] - -jobs: - push: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v3 - - - name: Setup .NET Core - uses: actions/setup-dotnet@v3 - with: - dotnet-version: '7.0.x' - - - name: Run Nuget Pack - run: ./build.sh NugetPack - shell: bash - - - name: Run Nuget Push - run: ./build.sh NugetPush -NugetApiKey ${{secrets.NUGET_API_KEY}} - shell: bash \ No newline at end of file diff --git a/.nuke/build.schema.json b/.nuke/build.schema.json index cb8c21b..d65e543 100644 --- a/.nuke/build.schema.json +++ b/.nuke/build.schema.json @@ -10,6 +10,21 @@ "type": "boolean", "description": "Indicates to continue a previously failed build attempt" }, + "FgaClientId": { + "type": "string", + "description": "FGA Client ID", + "default": "Secrets must be entered via 'nuke :secrets [profile]'" + }, + "FgaClientSecret": { + "type": "string", + "description": "FGA Client Secret", + "default": "Secrets must be entered via 'nuke :secrets [profile]'" + }, + "FgaStoreId": { + "type": "string", + "description": "FGA Store ID", + "default": "Secrets must be entered via 'nuke :secrets [profile]'" + }, "Help": { "type": "boolean", "description": "Shows the help text for this build assembly" @@ -21,6 +36,7 @@ "AppVeyor", "AzurePipelines", "Bamboo", + "Bitbucket", "Bitrise", "GitHubActions", "GitLab", diff --git a/build/Build.cs b/build/Build.cs index 0515dcf..9516af1 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -1,12 +1,27 @@ using Nuke.Common; using Nuke.Common.CI; +using Nuke.Common.CI.GitHubActions; using Nuke.Common.IO; using Nuke.Common.ProjectModel; +using Nuke.Common.Tooling; using Nuke.Common.Tools.DotNet; using static Nuke.Common.IO.FileSystemTasks; using static Nuke.Common.Tools.DotNet.DotNetTasks; [ShutdownDotNetAfterServerBuild] +[GitHubActions( + "Build & Test", + GitHubActionsImage.UbuntuLatest, + OnPushBranches = new []{ "main" }, + OnPullRequestBranches = new []{ "main" }, + InvokedTargets = new[] { nameof(Test) }, + ImportSecrets = new []{ nameof(FgaStoreId), nameof(FgaClientId), nameof(FgaClientSecret) })] +[GitHubActions( + "Manual Nuget Push", + GitHubActionsImage.UbuntuLatest, + On = new[] { GitHubActionsTrigger.WorkflowDispatch }, + InvokedTargets = new[] { nameof(NugetPush) }, + ImportSecrets = new[] { nameof(NugetApiKey) })] class Build : NukeBuild { /// Support plugins are available for: @@ -19,7 +34,6 @@ class Build : NukeBuild [Solution(GenerateProjects = true)] readonly Solution Solution; - AbsolutePath SourceDirectory => RootDirectory / "src"; AbsolutePath ArtifactsDirectory => RootDirectory / "artifacts"; Target Clean => _ => _ @@ -47,12 +61,28 @@ class Build : NukeBuild .EnableNoRestore()); }); + [Parameter("FGA Store ID")][Secret] readonly string FgaStoreId; + [Parameter("FGA Client ID")][Secret] readonly string FgaClientId; + [Parameter("FGA Client Secret")][Secret] readonly string FgaClientSecret; + Target Test => _ => _ .DependsOn(Compile) .Executes(() => { - DotNetTest(s => s - .SetProjectFile(Solution.Fga_Net_Tests)); + DotNetTest(s => + { + var config = s + .SetProjectFile(Solution.Fga_Net_Tests); + if (!string.IsNullOrEmpty(FgaStoreId)) + { + return config.AddProcessEnvironmentVariable("AUTH0FGA__STOREID", FgaStoreId) + .AddProcessEnvironmentVariable("AUTH0FGA__CLIENTID", FgaClientId) + .AddProcessEnvironmentVariable("AUTH0FGA__CLIENTSECRET", FgaClientSecret); + } + + return config; + + }); }); Target NugetPack => _ => _ @@ -72,10 +102,10 @@ class Build : NukeBuild .SetOutputDirectory(ArtifactsDirectory)); }); - [Parameter("Nuget Api Key")] - readonly string NugetApiKey = string.Empty; + [Parameter("Nuget Api Key")] [Secret] readonly string NugetApiKey; + Target NugetPush => _ => _ - .After(NugetPack) + .DependsOn(NugetPack) .Requires(() => !string.IsNullOrEmpty(NugetApiKey)) .Executes(() => { diff --git a/build/_build.csproj b/build/_build.csproj index a748288..ee1f14c 100644 --- a/build/_build.csproj +++ b/build/_build.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net7.0 CS0649;CS0169 ..