Skip to content

Commit a7a4916

Browse files
committed
Generate CI workflows
1 parent 702c466 commit a7a4916

File tree

3 files changed

+164
-82
lines changed

3 files changed

+164
-82
lines changed

.github/workflow-gen/Program.cs

Lines changed: 122 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using Logicality.GitHub.Actions.Workflow;
2-
using System.IO;
2+
3+
Component[] components = [
4+
new("ignore-this", ["IgnoreThis"], ["IgnoreThis.Tests"]),
5+
];
36

47
void WriteWorkflow(Workflow workflow, string fileName)
58
{
@@ -8,23 +11,15 @@ void WriteWorkflow(Workflow workflow, string fileName)
811
Console.WriteLine($"Wrote workflow to {filePath}");
912
}
1013

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)
14+
void GenerateCIWorkflow(Component component)
2015
{
2116
var workflow = new Workflow($"{component.Name}-ci");
22-
var paths = new[] { $".github/workflows/{component.Name}-ci", $"src/{component.Name}/**" };
17+
var paths = new[] { $".github/workflows/{component.Name}-**", $"src/{component.Name}/**" };
2318

2419
workflow.On.WorkflowDispatch();
2520
workflow.On
2621
.Push()
27-
.Branches("main");
22+
.Paths(paths);
2823
workflow.On
2924
.PullRequest()
3025
.Paths(paths);
@@ -37,16 +32,61 @@ void WriteWorkflow(Workflow workflow, string fileName)
3732
.Job("build")
3833
.Name("Build")
3934
.RunsOn(GitHubHostedRunners.UbuntuLatest)
40-
.Defaults().Run("pwsh", component.Name)
35+
.Defaults().Run("bash", component.Name)
4136
.Job;
4237

43-
job.Step().ActionsCheckout();
38+
job.Step()
39+
.ActionsCheckout();
40+
41+
job.Step()
42+
.ActionsSetupDotNet("8.0.x");
43+
44+
foreach (var testProject in component.Tests)
45+
{
46+
job.StepTestAndReport(testProject);
47+
}
48+
49+
job.StepInstallCACerts();
50+
51+
job.StepToolRestore();
52+
53+
foreach (var project in component.Projects)
54+
{
55+
job.StepPack(component.Name, project);
56+
}
57+
58+
job.StepSign();
59+
60+
job.StepPush("MyGet", "https://www.myget.org/F/duende_identityserver/api/v2/package", "MYGET");
61+
62+
job.StepPush("GitHub", "https://nuget.pkg.github.com/DuendeSoftware/index.json", "GITHUB_TOKEN")
63+
.Env(
64+
("GITHUB_TOKEN", "${{ secrets.GITHUB_TOKEN }}"),
65+
("NUGET_AUTH_TOKEN", "${{ secrets.GITHUB_TOKEN }}"));
66+
67+
job.StepUploadArtifacts(component.Name);
68+
69+
var fileName = $"{component.Name}-ci";
70+
71+
WriteWorkflow(workflow, fileName);
72+
}
73+
74+
foreach (var component in components)
75+
{
76+
GenerateCIWorkflow(component);
77+
}
78+
79+
record Component(string Name, string[] Projects, string[] Tests);
80+
4481

45-
job.Step().ActionsSetupDotNet("8.0.x");
82+
public static class StepExtensions
83+
{
84+
public static Step IfRefMain(this Step step)
85+
=> step.If("github.ref == 'refs/heads/main'");
4686

47-
foreach(var testProject in component.Tests)
87+
public static void StepTestAndReport(this Job job, string testProject)
4888
{
49-
var path = $"{component.Name}/test/{testProject}";
89+
var path = $"test/{testProject}";
5090
var logFileName = "Tests.trx";
5191
var flags = $"--logger \"console;verbosity=normal\" " +
5292
$"--logger \"trx;LogFileName={logFileName}\" " +
@@ -67,19 +107,70 @@ void WriteWorkflow(Workflow workflow, string fileName)
67107
("fail-on-empty", "true"));
68108
}
69109

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";
110+
public static void StepInstallCACerts(this Job job)
111+
=> job.Step()
112+
.Name("Install Sectigo CodeSiging CA certificates")
113+
.IfRefMain()
114+
.Run("""
115+
sudo apt-get update
116+
sudo apt-get install -y ca-certificates
117+
sudo cp build/SectigoPublicCodeSigningRootCrossAAA.crt /usr/local/share/ca-certificates/
118+
sudo update-ca-certificates
119+
""");
120+
121+
public static void StepToolRestore(this Job job)
122+
=> job.Step()
123+
.Name("Tool restore")
124+
.IfRefMain()
125+
.Run("dotnet tool restore");
126+
127+
public static void StepPack(this Job job, string componentName, string project)
128+
{
129+
var path = $"{componentName}/src/{project}";
130+
job.Step()
131+
.Name($"Pack {project}")
132+
.IfRefMain()
133+
.Run($"dotnet pack -c Release {path} --no-build -o artifacts");
134+
}
81135

82-
WriteWorkflow(workflow, fileName);
83-
}
136+
public static void StepSign(this Job job)
137+
{
138+
var flags = "--file-digest sha256 " +
139+
"--timestamp-rfc3161 http://timestamp.digicert.com " +
140+
"--azure-key-vault-url https://duendecodesigning.vault.azure.net/ " +
141+
"--azure-key-vault-client-id 18e3de68-2556-4345-8076-a46fad79e474 " +
142+
"--azure-key-vault-tenant-id ed3089f0-5401-4758-90eb-066124e2d907 " +
143+
"--azure-key-vault-client-secret ${{ secrets.SignClientSecret }} " +
144+
"--azure-key-vault-certificate CodeSigning";
145+
job.Step()
146+
.Name("Sign packages")
147+
.IfRefMain()
148+
.Run($"""
149+
for file in artifacts/*.nupkg; do
150+
dotnet NuGetKeyVaultSignTool sign \"$file\" {flags}
151+
done
152+
""");
153+
}
84154

85-
record Component(string Name, string[] Projects, string[] Tests);
155+
public static Step StepPush(this Job job, string destination, string sourceUrl, string secretName)
156+
{
157+
var apiKey = $"${{ secrets.{secretName} }}";
158+
return job.Step()
159+
.Name($"Push packages to {destination}")
160+
.Run($"dotnet nuget push artifacts/*.nupkg --source {sourceUrl} --api-key {apiKey} --skip-duplicate");
161+
}
162+
163+
public static void StepUploadArtifacts(this Job job, string componentName)
164+
{
165+
var path = $"{componentName}/artifacts/*.nupkg";
166+
job.Step()
167+
.Name("Upload Artifacts")
168+
.IfRefMain()
169+
.Uses("actions/upload-artifact@v4")
170+
.With(
171+
("name", "Upload Artifacts"),
172+
("path", path),
173+
("overwrite", "true"),
174+
("retention-days", "15"));
175+
}
176+
}

.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>

.github/workflows/ignore-this-ci.yml

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,79 @@
1-
name: ignore-this\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: ignore-this-ci
84
on:
95
workflow_dispatch:
106
push:
11-
branches:
12-
- main
137
paths:
14-
- .github/workflows/ignore-this-ci.yml
15-
- ignore-this/*
8+
- .github/workflows/ignore-this-**
9+
- src/ignore-this/**
1610
pull_request:
1711
paths:
18-
- .github/workflows/ignore-this-ci.yml
19-
- ignore-this/*
20-
12+
- .github/workflows/ignore-this-**
13+
- src/ignore-this/**
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: ignore-this
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/IgnoreThis.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: ignore-this/test/IgnoreThis.Tests/TestResults/Test.trx
41+
name: Test Report
42+
path: test/IgnoreThis.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
47+
if: github.ref == 'refs/heads/main'
5948
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 IgnoreThis
57+
if: github.ref == 'refs/heads/main'
58+
run: dotnet pack -c Release ignore-this/src/IgnoreThis --no-build -o artifacts
59+
- name: Sign packages
60+
if: github.ref == 'refs/heads/main'
61+
run: |-
62+
for file in artifacts/*.nupkg; do
63+
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
64+
done
7165
- 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-
run: dotnet nuget push artifacts\*.nupkg --source https://nuget.pkg.github.com/DuendeSoftware/index.json --api-key ${{ secrets.GITHUB_TOKEN }} --skip-duplicate
66+
run: dotnet nuget push artifacts/*.nupkg --source https://www.myget.org/F/duende_identityserver/api/v2/package --api-key ${ secrets.MYGET } --skip-duplicate
67+
- name: Push packages to GitHub
68+
run: dotnet nuget push artifacts/*.nupkg --source https://nuget.pkg.github.com/DuendeSoftware/index.json --api-key ${ secrets.GITHUB_TOKEN } --skip-duplicate
7769
env:
7870
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7971
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
80-
81-
- name: Upload artifacts
72+
- name: Upload Artifacts
73+
if: github.ref == 'refs/heads/main'
8274
uses: actions/upload-artifact@v4
83-
if: (github.ref == 'refs/heads/main')
8475
with:
76+
name: Upload Artifacts
8577
path: ignore-this/artifacts/*.nupkg
86-
compression-level: 0
8778
overwrite: true
88-
retention-days: 15
79+
retention-days: 15

0 commit comments

Comments
 (0)