From caec2eb96099d0d43a81dfc2564c63cd48e8bf5e Mon Sep 17 00:00:00 2001 From: Damian Hickey <57436+damianh@users.noreply.github.com> Date: Mon, 11 Nov 2024 21:51:39 +0100 Subject: [PATCH] Fix release workflows tag prefixes Also nicer way to manage of contexts --- .github/workflow-gen/Program.cs | 75 ++++++++++++++----- .../access-token-management-release.yml | 8 +- .../identity-model-oidc-client-release.yml | 8 +- .github/workflows/identity-model-release.yml | 8 +- .github/workflows/ignore-this-release.yml | 4 +- 5 files changed, 75 insertions(+), 28 deletions(-) diff --git a/.github/workflow-gen/Program.cs b/.github/workflow-gen/Program.cs index cdcbe3db..49071181 100644 --- a/.github/workflow-gen/Program.cs +++ b/.github/workflow-gen/Program.cs @@ -2,23 +2,30 @@ // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. using Logicality.GitHub.Actions.Workflow; +using static GitHubContexts; + +var contexts = Instance; Component[] components = [ new("ignore-this", ["IgnoreThis"], - ["IgnoreThis.Tests"]), + ["IgnoreThis.Tests"], + "it"), new("access-token-management", ["AccessTokenManagement", "AccessTokenManagement.OpenIdConnect"], - ["AccessTokenManagement.Tests"]), + ["AccessTokenManagement.Tests"], + "atm"), new("identity-model", ["IdentityModel"], - ["IdentityModel.Tests"]), + ["IdentityModel.Tests"], + "im"), new("identity-model-oidc-client", ["IdentityModel.OidcClient", "IdentityModel.OidcClient.Extensions"], - ["IdentityModel.OidcClient.Tests"]) + ["IdentityModel.OidcClient.Tests"], + "imoc") ]; foreach (var component in components) @@ -76,8 +83,8 @@ void GenerateCiWorkflow(Component component) job.StepPush("MyGet", "https://www.myget.org/F/duende_identityserver/api/v2/package", "MYGET"); job.StepPush("GitHub", "https://nuget.pkg.github.com/DuendeSoftware/index.json", "GITHUB_TOKEN") - .Env(("GITHUB_TOKEN", "${{ secrets.GITHUB_TOKEN }}"), - ("NUGET_AUTH_TOKEN", "${{ secrets.GITHUB_TOKEN }}")); + .Env(("GITHUB_TOKEN", contexts.Secrets.GitHubToken), + ("NUGET_AUTH_TOKEN", contexts.Secrets.GitHubToken)); job.StepUploadArtifacts(component.Name); @@ -100,8 +107,7 @@ void GenerateReleaseWorkflow(Component component) .Name("Tag and Pack") .RunsOn(GitHubHostedRunners.UbuntuLatest) .Permissions(contents: Permission.Write, packages: Permission.Write) - .Defaults().Run("pwsh", component.Name) - .Job; + .Defaults().Run("bash", component.Name).Job; tagJob.Step() .ActionsCheckout(); @@ -110,12 +116,10 @@ void GenerateReleaseWorkflow(Component component) tagJob.Step() .Name("Git tag") - .Run(""" - git config --global user.email "github-bot@duendesoftware.com" - git config --global user.name "Duende Software GitHub Bot" - git tag -a it-${{ github.event.inputs.version }} -m "Release v${{ github.event.inputs.version }}" - git push origin it-${{ github.event.inputs.version }} - """); + .Run($@"git config --global user.email ""github-bot@duendesoftware.com"" +git config --global user.name ""Duende Software GitHub Bot"" +git tag -a {component.TagPrefix}-{contexts.Event.Input.Version} -m ""Release v{contexts.Event.Input.Version}"" +git push origin {component.TagPrefix}-{contexts.Event.Input.Version}"); tagJob.StepInstallCACerts(); @@ -124,13 +128,15 @@ git push origin it-${{ github.event.inputs.version }} tagJob.StepPack(project); } + tagJob.StepToolRestore(); + tagJob.StepSign(); tagJob.StepPush("MyGet", "https://www.myget.org/F/duende_identityserver/api/v2/package", "MYGET"); tagJob.StepPush("GitHub", "https://nuget.pkg.github.com/DuendeSoftware/index.json", "GITHUB_TOKEN") - .Env(("GITHUB_TOKEN", "${{ secrets.GITHUB_TOKEN }}"), - ("NUGET_AUTH_TOKEN", "${{ secrets.GITHUB_TOKEN }}")); + .Env(("GITHUB_TOKEN", contexts.Secrets.GitHubToken), + ("NUGET_AUTH_TOKEN", contexts.Secrets.GitHubToken)); tagJob.StepUploadArtifacts(component.Name); @@ -164,7 +170,7 @@ void WriteWorkflow(Workflow workflow, string fileName) Console.WriteLine($"Wrote workflow to {filePath}"); } -record Component(string Name, string[] Projects, string[] Tests); +record Component(string Name, string[] Projects, string[] Tests, string TagPrefix); public static class StepExtensions { @@ -219,7 +225,6 @@ sudo update-ca-certificates public static void StepToolRestore(this Job job) => job.Step() .Name("Tool restore") - //.IfRefMain() .Run("dotnet tool restore"); public static void StepPack(this Job job, string project) @@ -271,3 +276,37 @@ public static void StepUploadArtifacts(this Job job, string componentName) ("retention-days", "15")); } } + +public class GitHubContexts +{ + public static GitHubContexts Instance { get; } = new(); + public virtual GitHubContext GitHub { get; } = new(); + public virtual SecretsContext Secrets { get; } = new(); + public virtual EventContext Event { get; } = new(); + + public abstract class Context(string name) + { + protected string Name => name; + + protected string Expression(string s) => "${{ " + s + " }}"; + } + + public class GitHubContext() : Context("github") + { + } + + public class SecretsContext() : Context("secrets") + { + public string GitHubToken => Expression($"{Name}.GITHUB_TOKEN"); + } + + public class EventContext() : Context("github.event") + { + public EventsInputContext Input { get; } = new (); + } + + public class EventsInputContext() : Context("github.event.inputs") + { + public string Version => Expression($"{Name}.version"); + } +} diff --git a/.github/workflows/access-token-management-release.yml b/.github/workflows/access-token-management-release.yml index 9ec13c63..e3531463 100644 --- a/.github/workflows/access-token-management-release.yml +++ b/.github/workflows/access-token-management-release.yml @@ -21,7 +21,7 @@ jobs: packages: write defaults: run: - shell: pwsh + shell: bash working-directory: access-token-management steps: - name: Checkout @@ -36,8 +36,8 @@ jobs: run: |- git config --global user.email "github-bot@duendesoftware.com" git config --global user.name "Duende Software GitHub Bot" - git tag -a it-${{ github.event.inputs.version }} -m "Release v${{ github.event.inputs.version }}" - git push origin it-${{ github.event.inputs.version }} + git tag -a atm-${{ github.event.inputs.version }} -m "Release v${{ github.event.inputs.version }}" + git push origin atm-${{ github.event.inputs.version }} - name: Install Sectigo CodeSiging CA certificates run: |- sudo apt-get update @@ -49,6 +49,8 @@ jobs: run: dotnet pack -c Release src/AccessTokenManagement -o artifacts - name: Pack AccessTokenManagement.OpenIdConnect run: dotnet pack -c Release src/AccessTokenManagement.OpenIdConnect -o artifacts + - name: Tool restore + run: dotnet tool restore - name: Sign packages run: |- for file in artifacts/*.nupkg; do diff --git a/.github/workflows/identity-model-oidc-client-release.yml b/.github/workflows/identity-model-oidc-client-release.yml index 76b4fffe..71471224 100644 --- a/.github/workflows/identity-model-oidc-client-release.yml +++ b/.github/workflows/identity-model-oidc-client-release.yml @@ -21,7 +21,7 @@ jobs: packages: write defaults: run: - shell: pwsh + shell: bash working-directory: identity-model-oidc-client steps: - name: Checkout @@ -36,8 +36,8 @@ jobs: run: |- git config --global user.email "github-bot@duendesoftware.com" git config --global user.name "Duende Software GitHub Bot" - git tag -a it-${{ github.event.inputs.version }} -m "Release v${{ github.event.inputs.version }}" - git push origin it-${{ github.event.inputs.version }} + git tag -a imoc-${{ github.event.inputs.version }} -m "Release v${{ github.event.inputs.version }}" + git push origin imoc-${{ github.event.inputs.version }} - name: Install Sectigo CodeSiging CA certificates run: |- sudo apt-get update @@ -49,6 +49,8 @@ jobs: run: dotnet pack -c Release src/IdentityModel.OidcClient -o artifacts - name: Pack IdentityModel.OidcClient.Extensions run: dotnet pack -c Release src/IdentityModel.OidcClient.Extensions -o artifacts + - name: Tool restore + run: dotnet tool restore - name: Sign packages run: |- for file in artifacts/*.nupkg; do diff --git a/.github/workflows/identity-model-release.yml b/.github/workflows/identity-model-release.yml index eb060d75..3fac6e44 100644 --- a/.github/workflows/identity-model-release.yml +++ b/.github/workflows/identity-model-release.yml @@ -21,7 +21,7 @@ jobs: packages: write defaults: run: - shell: pwsh + shell: bash working-directory: identity-model steps: - name: Checkout @@ -36,8 +36,8 @@ jobs: run: |- git config --global user.email "github-bot@duendesoftware.com" git config --global user.name "Duende Software GitHub Bot" - git tag -a it-${{ github.event.inputs.version }} -m "Release v${{ github.event.inputs.version }}" - git push origin it-${{ github.event.inputs.version }} + git tag -a im-${{ github.event.inputs.version }} -m "Release v${{ github.event.inputs.version }}" + git push origin im-${{ github.event.inputs.version }} - name: Install Sectigo CodeSiging CA certificates run: |- sudo apt-get update @@ -47,6 +47,8 @@ jobs: working-directory: .github/workflows - name: Pack IdentityModel run: dotnet pack -c Release src/IdentityModel -o artifacts + - name: Tool restore + run: dotnet tool restore - name: Sign packages run: |- for file in artifacts/*.nupkg; do diff --git a/.github/workflows/ignore-this-release.yml b/.github/workflows/ignore-this-release.yml index 67fc7f81..57fe7fe4 100644 --- a/.github/workflows/ignore-this-release.yml +++ b/.github/workflows/ignore-this-release.yml @@ -21,7 +21,7 @@ jobs: packages: write defaults: run: - shell: pwsh + shell: bash working-directory: ignore-this steps: - name: Checkout @@ -47,6 +47,8 @@ jobs: working-directory: .github/workflows - name: Pack IgnoreThis run: dotnet pack -c Release src/IgnoreThis -o artifacts + - name: Tool restore + run: dotnet tool restore - name: Sign packages run: |- for file in artifacts/*.nupkg; do