Skip to content

Commit 079cdf5

Browse files
committed
Generate CI workflows
1 parent 702c466 commit 079cdf5

File tree

6 files changed

+346
-255
lines changed

6 files changed

+346
-255
lines changed

.github/workflow-gen/Program.cs

Lines changed: 137 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
using Logicality.GitHub.Actions.Workflow;
2-
using System.IO;
2+
3+
Component[] components = [
4+
new("ignore-this",
5+
["IgnoreThis"],
6+
["IgnoreThis.Tests"]),
7+
8+
new("access-token-management",
9+
["AccessTokenManagement", "AccessTokenManagement.OpenIdConnect"],
10+
["AccessTokenManagement.Tests"]),
11+
12+
new("identity-model",
13+
["IdentityModel"],
14+
["IdentityModel.Tests"]),
15+
16+
new("identity-model-oidc-client",
17+
["IdentityModel.OidcClient", "IdentityModel.OidcClient.DPoP", "IdentityModel.OidcClient.IdentityTokenValidator"],
18+
["IdentityModel.OidcClient.Tests", "IdentityModel.OidcClient.DPoP.Tests", "IdentityModel.OidcClient.IdentityTokenValidator.Tests"])
19+
];
320

421
void WriteWorkflow(Workflow workflow, string fileName)
522
{
@@ -8,23 +25,15 @@ void WriteWorkflow(Workflow workflow, string fileName)
825
Console.WriteLine($"Wrote workflow to {filePath}");
926
}
1027

11-
12-
Component[] components = [
13-
new("ignore-this", ["IgnoreThis"], ["IgnoreThis.Tests"]),
14-
];
15-
16-
(string Key, string Value) EnvSecret(string key) => (key, $"${{secrets.{key}}}");
17-
18-
19-
foreach (var component in components)
28+
void GenerateCIWorkflow(Component component)
2029
{
2130
var workflow = new Workflow($"{component.Name}-ci");
22-
var paths = new[] { $".github/workflows/{component.Name}-ci", $"src/{component.Name}/**" };
31+
var paths = new[] { $".github/workflows/{component.Name}-**", $"src/{component.Name}/**" };
2332

2433
workflow.On.WorkflowDispatch();
2534
workflow.On
2635
.Push()
27-
.Branches("main");
36+
.Paths(paths);
2837
workflow.On
2938
.PullRequest()
3039
.Paths(paths);
@@ -37,16 +46,60 @@ void WriteWorkflow(Workflow workflow, string fileName)
3746
.Job("build")
3847
.Name("Build")
3948
.RunsOn(GitHubHostedRunners.UbuntuLatest)
40-
.Defaults().Run("pwsh", component.Name)
49+
.Defaults().Run("bash", component.Name)
4150
.Job;
4251

43-
job.Step().ActionsCheckout();
52+
job.Step()
53+
.ActionsCheckout();
54+
55+
job.Step()
56+
.ActionsSetupDotNet("8.0.x");
57+
58+
foreach (var testProject in component.Tests)
59+
{
60+
job.StepTestAndReport(component.Name, testProject);
61+
}
62+
63+
job.StepInstallCACerts();
64+
65+
job.StepToolRestore();
66+
67+
foreach (var project in component.Projects)
68+
{
69+
job.StepPack(component.Name, project);
70+
}
71+
72+
job.StepSign();
73+
74+
job.StepPush("MyGet", "https://www.myget.org/F/duende_identityserver/api/v2/package", "MYGET");
75+
76+
job.StepPush("GitHub", "https://nuget.pkg.github.com/DuendeSoftware/index.json", "GITHUB_TOKEN")
77+
.Env(
78+
("GITHUB_TOKEN", "${{ secrets.GITHUB_TOKEN }}"),
79+
("NUGET_AUTH_TOKEN", "${{ secrets.GITHUB_TOKEN }}"));
80+
81+
job.StepUploadArtifacts(component.Name);
82+
83+
var fileName = $"{component.Name}-ci";
4484

45-
job.Step().ActionsSetupDotNet("8.0.x");
85+
WriteWorkflow(workflow, fileName);
86+
}
87+
88+
foreach (var component in components)
89+
{
90+
GenerateCIWorkflow(component);
91+
}
92+
93+
record Component(string Name, string[] Projects, string[] Tests);
94+
95+
public static class StepExtensions
96+
{
97+
public static Step IfRefMain(this Step step)
98+
=> step.If("github.ref == 'refs/heads/main'");
4699

47-
foreach(var testProject in component.Tests)
100+
public static void StepTestAndReport(this Job job, string componentName, string testProject)
48101
{
49-
var path = $"{component.Name}/test/{testProject}";
102+
var path = $"test/{testProject}";
50103
var logFileName = "Tests.trx";
51104
var flags = $"--logger \"console;verbosity=normal\" " +
52105
$"--logger \"trx;LogFileName={logFileName}\" " +
@@ -61,25 +114,77 @@ void WriteWorkflow(Workflow workflow, string fileName)
61114
.If("success() || failure()")
62115
.With(
63116
("name", "Test Report"),
64-
("path", $"{path}/TestResults/{logFileName}"),
117+
("path", $"{componentName}/{path}/TestResults/{logFileName}"),
65118
("reporter", "dotnet-trx"),
66119
("fail-on-error", "true"),
67120
("fail-on-empty", "true"));
68121
}
69122

70-
job.Step()
71-
.Name("Install Sectigo CodeSiging CA certificates")
72-
.Run("""
73-
sudo apt-get update
74-
sudo apt-get install -y ca-certificates
75-
sudo cp build/SectigoPublicCodeSigningRootCrossAAA.crt /usr/local/share/ca-certificates/
76-
sudo update-ca-certificates
77-
78-
""");
79-
80-
var fileName = $"{component.Name}-ci-gen";
123+
public static void StepInstallCACerts(this Job job)
124+
=> job.Step()
125+
.Name("Install Sectigo CodeSiging CA certificates")
126+
.IfRefMain()
127+
.Run("""
128+
sudo apt-get update
129+
sudo apt-get install -y ca-certificates
130+
sudo cp build/SectigoPublicCodeSigningRootCrossAAA.crt /usr/local/share/ca-certificates/
131+
sudo update-ca-certificates
132+
""");
133+
134+
public static void StepToolRestore(this Job job)
135+
=> job.Step()
136+
.Name("Tool restore")
137+
.IfRefMain()
138+
.Run("dotnet tool restore");
139+
140+
public static void StepPack(this Job job, string componentName, string project)
141+
{
142+
var path = $"{componentName}/src/{project}";
143+
job.Step()
144+
.Name($"Pack {project}")
145+
.IfRefMain()
146+
.Run($"dotnet pack -c Release {path} --no-build -o artifacts");
147+
}
81148

82-
WriteWorkflow(workflow, fileName);
83-
}
149+
public static void StepSign(this Job job)
150+
{
151+
var flags = "--file-digest sha256 " +
152+
"--timestamp-rfc3161 http://timestamp.digicert.com " +
153+
"--azure-key-vault-url https://duendecodesigning.vault.azure.net/ " +
154+
"--azure-key-vault-client-id 18e3de68-2556-4345-8076-a46fad79e474 " +
155+
"--azure-key-vault-tenant-id ed3089f0-5401-4758-90eb-066124e2d907 " +
156+
"--azure-key-vault-client-secret ${{ secrets.SignClientSecret }} " +
157+
"--azure-key-vault-certificate CodeSigning";
158+
job.Step()
159+
.Name("Sign packages")
160+
.IfRefMain()
161+
.Run($"""
162+
for file in artifacts/*.nupkg; do
163+
dotnet NuGetKeyVaultSignTool sign \"$file\" {flags}
164+
done
165+
""");
166+
}
84167

85-
record Component(string Name, string[] Projects, string[] Tests);
168+
public static Step StepPush(this Job job, string destination, string sourceUrl, string secretName)
169+
{
170+
var apiKey = $"${{ secrets.{secretName} }}";
171+
return job.Step()
172+
.Name($"Push packages to {destination}")
173+
.IfRefMain()
174+
.Run($"dotnet nuget push artifacts/*.nupkg --source {sourceUrl} --api-key {apiKey} --skip-duplicate");
175+
}
176+
177+
public static void StepUploadArtifacts(this Job job, string componentName)
178+
{
179+
var path = $"{componentName}/artifacts/*.nupkg";
180+
job.Step()
181+
.Name("Upload Artifacts")
182+
.IfRefMain()
183+
.Uses("actions/upload-artifact@v4")
184+
.With(
185+
("name", "Upload Artifacts"),
186+
("path", path),
187+
("overwrite", "true"),
188+
("retention-days", "15"));
189+
}
190+
}

.github/workflow-gen/workflow-gen.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Logicality.GitHub.Actions.Workflow" Version="0.4.0" />
13-
<PackageReference Include="Logicality.GitHub.Actions.Workflow.Extensions" Version="0.4.0" />
12+
<PackageReference Include="Logicality.GitHub.Actions.Workflow" Version="0.5.0" />
13+
<PackageReference Include="Logicality.GitHub.Actions.Workflow.Extensions" Version="0.5.0" />
1414
</ItemGroup>
1515

1616
</Project>
Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,84 @@
1-
name: access-token-management\ci
2-
3-
permissions:
4-
contents: read
5-
checks: write
6-
packages: write
1+
# This was generated by tool. Edits will be overwritten.
72

3+
name: access-token-management-ci
84
on:
95
workflow_dispatch:
106
push:
11-
branches:
12-
- main
137
paths:
14-
- .github/workflows/access-token-management-ci.yml
15-
- access-token-management/*
8+
- .github/workflows/access-token-management-**
9+
- src/access-token-management/**
1610
pull_request:
1711
paths:
18-
- .github/workflows/access-token-management-ci.yml
19-
- access-token-management/*
20-
12+
- .github/workflows/access-token-management-**
13+
- src/access-token-management/**
2114
env:
22-
DOTNET_NOLOGO: true
15+
DOTNETT_NOLOGO: true
2316
DOTNET_CLI_TELEMETRY_OPTOUT: true
24-
2517
jobs:
2618
build:
2719
name: Build
2820
runs-on: ubuntu-latest
2921
defaults:
3022
run:
23+
shell: bash
3124
working-directory: access-token-management
32-
shell: pwsh
33-
3425
steps:
35-
- uses: actions/checkout@v4
26+
- name: Checkout
27+
uses: actions/checkout@v4
3628
with:
3729
fetch-depth: 0
38-
39-
- uses: actions/setup-dotnet@v4
30+
- name: Setup Dotnet
31+
uses: actions/setup-dotnet@v4
4032
with:
41-
dotnet-version: |
42-
8.0.x
43-
44-
- name: Build
45-
run: ./build.ps1
46-
47-
- name: Test report
48-
id: test-report
33+
dotnet-version: 8.0.x
34+
- name: Test
35+
run: dotnet test -c Release test/AccessTokenManagement.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=Tests.trx" --collect:"XPlat Code Coverage"
36+
- id: test-report
37+
name: Test report
38+
if: success() || failure()
4939
uses: dorny/test-reporter@v1
50-
if: success() || failure() # run this step even if previous step failed
5140
with:
52-
name: Test results
53-
path: access-token-management/test/AccessTokenManagement.Tests/TestResults/Test.trx
41+
name: Test Report
42+
path: access-token-management/test/AccessTokenManagement.Tests/TestResults/Tests.trx
5443
reporter: dotnet-trx
5544
fail-on-error: true
5645
fail-on-empty: true
57-
5846
- name: Install Sectigo CodeSiging CA certificates
59-
run: |
47+
if: github.ref == 'refs/heads/main'
48+
run: |-
6049
sudo apt-get update
6150
sudo apt-get install -y ca-certificates
6251
sudo cp build/SectigoPublicCodeSigningRootCrossAAA.crt /usr/local/share/ca-certificates/
6352
sudo update-ca-certificates
64-
65-
- name: Sign
66-
if: (github.ref == 'refs/heads/main')
67-
env:
68-
SignClientSecret: ${{ secrets.SignClientSecret }}
69-
run: ./build.ps1 sign
70-
53+
- name: Tool restore
54+
if: github.ref == 'refs/heads/main'
55+
run: dotnet tool restore
56+
- name: Pack AccessTokenManagement
57+
if: github.ref == 'refs/heads/main'
58+
run: dotnet pack -c Release access-token-management/src/AccessTokenManagement --no-build -o artifacts
59+
- name: Pack AccessTokenManagement.OpenIdConnect
60+
if: github.ref == 'refs/heads/main'
61+
run: dotnet pack -c Release access-token-management/src/AccessTokenManagement.OpenIdConnect --no-build -o artifacts
62+
- name: Sign packages
63+
if: github.ref == 'refs/heads/main'
64+
run: |-
65+
for file in artifacts/*.nupkg; do
66+
dotnet NuGetKeyVaultSignTool sign \"$file\" --file-digest sha256 --timestamp-rfc3161 http://timestamp.digicert.com --azure-key-vault-url https://duendecodesigning.vault.azure.net/ --azure-key-vault-client-id 18e3de68-2556-4345-8076-a46fad79e474 --azure-key-vault-tenant-id ed3089f0-5401-4758-90eb-066124e2d907 --azure-key-vault-client-secret ${{ secrets.SignClientSecret }} --azure-key-vault-certificate CodeSigning
67+
done
7168
- name: Push packages to MyGet
72-
if: (github.ref == 'refs/heads/main')
73-
run: dotnet nuget push artifacts\*.nupkg -s https://www.myget.org/F/duende_identityserver/api/v2/package -k ${{ secrets.MYGET }} --skip-duplicate
74-
75-
- name: Push NuGet package to GitHub Packages
76-
if: (github.ref == 'refs/heads/main')
77-
run: dotnet nuget push artifacts\*.nupkg --source https://nuget.pkg.github.com/DuendeSoftware/index.json --api-key ${{ secrets.GITHUB_TOKEN }} --skip-duplicate
69+
if: github.ref == 'refs/heads/main'
70+
run: dotnet nuget push artifacts/*.nupkg --source https://www.myget.org/F/duende_identityserver/api/v2/package --api-key ${ secrets.MYGET } --skip-duplicate
71+
- name: Push packages to GitHub
72+
if: github.ref == 'refs/heads/main'
73+
run: dotnet nuget push artifacts/*.nupkg --source https://nuget.pkg.github.com/DuendeSoftware/index.json --api-key ${ secrets.GITHUB_TOKEN } --skip-duplicate
7874
env:
7975
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8076
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
81-
82-
- name: Upload artifacts
77+
- name: Upload Artifacts
78+
if: github.ref == 'refs/heads/main'
8379
uses: actions/upload-artifact@v4
84-
if: (github.ref == 'refs/heads/main')
8580
with:
81+
name: Upload Artifacts
8682
path: access-token-management/artifacts/*.nupkg
87-
compression-level: 0
8883
overwrite: true
89-
retention-days: 15
84+
retention-days: 15

0 commit comments

Comments
 (0)