Skip to content

Commit 3e20bbb

Browse files
first attempt
1 parent 28971fa commit 3e20bbb

File tree

7 files changed

+119
-38
lines changed

7 files changed

+119
-38
lines changed

.github/workflow-gen/Program.cs

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

44
using Logicality.GitHub.Actions.Workflow;
5+
using System.ComponentModel;
6+
using System.IO;
57
using static GitHubContexts;
68

79
var contexts = Instance;
@@ -31,11 +33,12 @@
3133
{
3234
GenerateCiWorkflow(component);
3335
GenerateReleaseWorkflow(component);
36+
GenerateUploadTestResultsWorkflow(component);
3437
}
3538

3639
void GenerateCiWorkflow(Component component)
3740
{
38-
var workflow = new Workflow($"{component.Name}/ci");
41+
var workflow = new Workflow(component.CiWorkflowName);
3942
var paths = new[]
4043
{
4144
$".github/workflows/{component.Name}-**",
@@ -57,6 +60,7 @@ void GenerateCiWorkflow(Component component)
5760
var job = workflow
5861
.Job("build")
5962
.Name("Build")
63+
.RunEitherOnBranchOrAsPR()
6064
.RunsOn(GitHubHostedRunners.UbuntuLatest)
6165
.Defaults().Run("bash", component.Name)
6266
.Job;
@@ -76,9 +80,11 @@ void GenerateCiWorkflow(Component component)
7680

7781
foreach (var testProject in component.Tests)
7882
{
79-
job.StepTestAndReport(component.Name, testProject);
83+
job.StepTest(component.Name, testProject);
8084
}
8185

86+
job.StepUploadTestResultsAsArtifact(component.Tests);
87+
8288
job.StepToolRestore();
8389

8490
foreach (var project in component.Projects)
@@ -102,7 +108,7 @@ void GenerateCiWorkflow(Component component)
102108

103109
void GenerateReleaseWorkflow(Component component)
104110
{
105-
var workflow = new Workflow($"{component.Name}/release");
111+
var workflow = new Workflow(component.ReleaseWorkflowName);
106112

107113
workflow.On
108114
.WorkflowDispatch()
@@ -169,17 +175,69 @@ git tag -a {component.TagPrefix}-{contexts.Event.Input.Version} -m ""Release v{c
169175
WriteWorkflow(workflow, fileName);
170176
}
171177

178+
void GenerateUploadTestResultsWorkflow(Component component)
179+
{
180+
var workflow = new Workflow(component.TestResultWorkflowName);
181+
workflow.On
182+
.WorkflowRun()
183+
.Workflows(component.TestResultWorkflowName)
184+
.Types("completed");
185+
186+
var job = workflow
187+
.Job("report")
188+
.Name("report")
189+
.RunsOn(GitHubHostedRunners.UbuntuLatest);
190+
191+
job.Permissions(
192+
actions: Permission.Read,
193+
contents: Permission.Read,
194+
checks: Permission.Write,
195+
packages: Permission.Write);
196+
197+
foreach (var testProject in component.Tests)
198+
{
199+
job.StepGenerateReportFromTestArtifact(component.Name, testProject);
200+
}
201+
202+
}
203+
172204
void WriteWorkflow(Workflow workflow, string fileName)
173205
{
174206
var filePath = $"../workflows/{fileName}.yml";
175207
workflow.WriteYaml(filePath);
176208
Console.WriteLine($"Wrote workflow to {filePath}");
177209
}
178210

179-
record Component(string Name, string[] Projects, string[] Tests, string TagPrefix);
211+
record Component(string Name, string[] Projects, string[] Tests, string TagPrefix)
212+
{
213+
public string CiWorkflowName => $"{Name}/ci";
214+
public string ReleaseWorkflowName => $"{Name}/release";
215+
public string TestResultWorkflowName => $"{Name}/test-results";
216+
}
180217

181218
public static class StepExtensions
182219
{
220+
221+
/// <summary>
222+
/// The build triggers both on branch AND on pull_request.
223+
///
224+
/// Only (trusted) contributors can open branches in the main repo, so these builds can run with a higher trust level.
225+
/// So, they are running with trigger 'push'. These builds have access to the secrets and thus they can do things like
226+
/// sign, push the packages, etc..
227+
///
228+
/// External contributors can only create branches on external repo's. These builds run with a lower trust level.
229+
/// So, they are running with trigger 'pull_request'. These builds do not have access to the secrets and thus they can't
230+
/// sign, push the packages, etc..
231+
///
232+
/// Now, if a trusted contributor creates a branch in the main repo, then creates a PR, we don't want to run the build twice.
233+
/// This prevents that. The build will only run once, on the branch with the higher trust level.
234+
///
235+
/// </summary>
236+
public static Job RunEitherOnBranchOrAsPR(this Job job)
237+
=> job.If(
238+
"(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) || (github.event_name == 'push')");
239+
240+
183241
public static void EnvDefaults(this Workflow workflow)
184242
=> workflow.Env(
185243
("DOTNET_NOLOGO", "true"),
@@ -193,7 +251,7 @@ public static void StepSetupDotNet(this Job job)
193251
public static Step IfRefMain(this Step step)
194252
=> step.If("github.ref == 'refs/heads/main'");
195253

196-
public static void StepTestAndReport(this Job job, string componentName, string testProject)
254+
public static void StepTest(this Job job, string componentName, string testProject)
197255
{
198256
var path = $"test/{testProject}";
199257
var logFileName = "Tests.trx";
@@ -204,13 +262,34 @@ public static void StepTestAndReport(this Job job, string componentName, string
204262
.Name($"Test - {testProject}")
205263
.Run($"dotnet test -c Release {path} {flags}");
206264

265+
}
266+
267+
private static readonly string LogFileName = "Tests.trx";
268+
269+
public static void StepUploadTestResultsAsArtifact(this Job job, string[] testProjects)
270+
{
271+
job.Step()
272+
.Name("Upload test artifact")
273+
.If("success() || failure()")
274+
.Uses("actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882") // 4.4.3
275+
.With(
276+
("name", "test-results"),
277+
("path", string.Join(Environment.NewLine, testProjects
278+
.Select(testProject => $"test/{testProject}/TestResults/{LogFileName}"))),
279+
("retention-days", "5"));
280+
}
281+
282+
public static void StepGenerateReportFromTestArtifact(this Job job, string componentName, string testProject)
283+
{
284+
var path = $"test/{testProject}";
207285
job.Step()
208286
.Name($"Test report - {testProject}")
209287
.Uses("dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5") // v1.9.1
210288
.If("github.event == 'push' && (success() || failure())")
211289
.With(
290+
("artifact", "test-results"),
212291
("name", $"Test Report - {testProject}"),
213-
("path", $"{componentName}/{path}/TestResults/{logFileName}"),
292+
("path", $"{componentName}/{path}/TestResults/{LogFileName}"),
214293
("reporter", "dotnet-trx"),
215294
("fail-on-error", "true"),
216295
("fail-on-empty", "true"));

.github/workflows/access-token-management-ci.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ env:
1919
jobs:
2020
build:
2121
name: Build
22+
if: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) || (github.event_name == 'push')
2223
runs-on: ubuntu-latest
2324
permissions:
2425
actions: read
@@ -44,15 +45,13 @@ jobs:
4445
9.0.x
4546
- name: Test - AccessTokenManagement.Tests
4647
run: dotnet test -c Release test/AccessTokenManagement.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=Tests.trx" --collect:"XPlat Code Coverage"
47-
- name: Test report - AccessTokenManagement.Tests
48-
if: github.event == 'push' && (success() || failure())
49-
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
48+
- name: Upload test artifact
49+
if: success() || failure()
50+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
5051
with:
51-
name: Test Report - AccessTokenManagement.Tests
52-
path: access-token-management/test/AccessTokenManagement.Tests/TestResults/Tests.trx
53-
reporter: dotnet-trx
54-
fail-on-error: true
55-
fail-on-empty: true
52+
name: test-results
53+
path: test/AccessTokenManagement.Tests/TestResults/Tests.trx
54+
retention-days: 5
5655
- name: Tool restore
5756
run: dotnet tool restore
5857
- name: Pack AccessTokenManagement

.github/workflows/identity-model-ci.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ env:
1919
jobs:
2020
build:
2121
name: Build
22+
if: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) || (github.event_name == 'push')
2223
runs-on: ubuntu-latest
2324
permissions:
2425
actions: read
@@ -44,15 +45,13 @@ jobs:
4445
9.0.x
4546
- name: Test - IdentityModel.Tests
4647
run: dotnet test -c Release test/IdentityModel.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=Tests.trx" --collect:"XPlat Code Coverage"
47-
- name: Test report - IdentityModel.Tests
48-
if: github.event == 'push' && (success() || failure())
49-
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
48+
- name: Upload test artifact
49+
if: success() || failure()
50+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
5051
with:
51-
name: Test Report - IdentityModel.Tests
52-
path: identity-model/test/IdentityModel.Tests/TestResults/Tests.trx
53-
reporter: dotnet-trx
54-
fail-on-error: true
55-
fail-on-empty: true
52+
name: test-results
53+
path: test/IdentityModel.Tests/TestResults/Tests.trx
54+
retention-days: 5
5655
- name: Tool restore
5756
run: dotnet tool restore
5857
- name: Pack IdentityModel

.github/workflows/identity-model-oidc-client-ci.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ env:
1919
jobs:
2020
build:
2121
name: Build
22+
if: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) || (github.event_name == 'push')
2223
runs-on: ubuntu-latest
2324
permissions:
2425
actions: read
@@ -44,15 +45,13 @@ jobs:
4445
9.0.x
4546
- name: Test - IdentityModel.OidcClient.Tests
4647
run: dotnet test -c Release test/IdentityModel.OidcClient.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=Tests.trx" --collect:"XPlat Code Coverage"
47-
- name: Test report - IdentityModel.OidcClient.Tests
48-
if: github.event == 'push' && (success() || failure())
49-
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
48+
- name: Upload test artifact
49+
if: success() || failure()
50+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
5051
with:
51-
name: Test Report - IdentityModel.OidcClient.Tests
52-
path: identity-model-oidc-client/test/IdentityModel.OidcClient.Tests/TestResults/Tests.trx
53-
reporter: dotnet-trx
54-
fail-on-error: true
55-
fail-on-empty: true
52+
name: test-results
53+
path: test/IdentityModel.OidcClient.Tests/TestResults/Tests.trx
54+
retention-days: 5
5655
- name: Tool restore
5756
run: dotnet tool restore
5857
- name: Pack IdentityModel.OidcClient

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ env:
1919
jobs:
2020
build:
2121
name: Build
22+
if: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) || (github.event_name == 'push')
2223
runs-on: ubuntu-latest
2324
permissions:
2425
actions: read
@@ -44,15 +45,13 @@ jobs:
4445
9.0.x
4546
- name: Test - IgnoreThis.Tests
4647
run: dotnet test -c Release test/IgnoreThis.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=Tests.trx" --collect:"XPlat Code Coverage"
47-
- name: Test report - IgnoreThis.Tests
48-
if: github.event == 'push' && (success() || failure())
49-
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
48+
- name: Upload test artifact
49+
if: success() || failure()
50+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
5051
with:
51-
name: Test Report - IgnoreThis.Tests
52-
path: ignore-this/test/IgnoreThis.Tests/TestResults/Tests.trx
53-
reporter: dotnet-trx
54-
fail-on-error: true
55-
fail-on-empty: true
52+
name: test-results
53+
path: test/IgnoreThis.Tests/TestResults/Tests.trx
54+
retention-days: 5
5655
- name: Tool restore
5756
run: dotnet tool restore
5857
- name: Pack IgnoreThis
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<ProjectConfiguration>
2+
<Settings />
3+
</ProjectConfiguration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<ProjectConfiguration>
2+
<Settings />
3+
</ProjectConfiguration>

0 commit comments

Comments
 (0)