Skip to content

Commit

Permalink
build - generate test reports in a separate step
Browse files Browse the repository at this point in the history
  • Loading branch information
Erwinvandervalk committed Feb 12, 2025
1 parent 7818534 commit 9a47c08
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 58 deletions.
83 changes: 69 additions & 14 deletions .github/workflow-gen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@
GenerateReleaseWorkflow(component);
}

GenerateUploadTestResultsWorkflow();


void GenerateCiWorkflow(Component component)
{
var workflow = new Workflow($"{component.Name}/ci");
var workflow = new Workflow(component.CiWorkflowName);
var paths = new[]
{
$".github/workflows/{component.Name}-**",
Expand Down Expand Up @@ -76,9 +79,11 @@ void GenerateCiWorkflow(Component component)

foreach (var testProject in component.Tests)
{
job.StepTestAndReport(component.Name, testProject);
job.StepTest(component.Name, testProject);
}

job.StepUploadTestResultsAsArtifact(component);

job.StepToolRestore();

foreach (var project in component.Projects)
Expand All @@ -100,7 +105,7 @@ void GenerateCiWorkflow(Component component)

void GenerateReleaseWorkflow(Component component)
{
var workflow = new Workflow($"{component.Name}/release");
var workflow = new Workflow(component.ReleaseWorkflowName);

workflow.On
.WorkflowDispatch()
Expand Down Expand Up @@ -166,14 +171,49 @@ git tag -a {component.TagPrefix}-{contexts.Event.Input.Version} -m ""Release v{c
WriteWorkflow(workflow, fileName);
}

void GenerateUploadTestResultsWorkflow()
{
var workflow = new Workflow("generate-test-reports");
workflow.On
.WorkflowRun()
.Workflows(components.Select(x => x.CiWorkflowName).ToArray())
.Types("completed");

var job = workflow
.Job("report")
.Name("report")
.RunsOn(GitHubHostedRunners.UbuntuLatest);

job.Permissions(
actions: Permission.Read,
contents: Permission.Read,
checks: Permission.Write,
packages: Permission.Write);

foreach (var component in components)
{
foreach (var testProject in component.Tests)
{
job.StepGenerateReportFromTestArtifact(component, testProject);
}
}

var fileName = $"generate-test-reports";
WriteWorkflow(workflow, fileName);
}

void WriteWorkflow(Workflow workflow, string fileName)
{
var filePath = $"../workflows/{fileName}.yml";
workflow.WriteYaml(filePath);
Console.WriteLine($"Wrote workflow to {filePath}");
}

record Component(string Name, string[] Projects, string[] Tests, string TagPrefix);
record Component(string Name, string[] Projects, string[] Tests, string TagPrefix)
{
public string CiWorkflowName => $"{Name}/ci";
public string ReleaseWorkflowName => $"{Name}/release";
}

public static class StepExtensions
{
Expand All @@ -190,28 +230,43 @@ public static void StepSetupDotNet(this Job job)
public static Step IfRefMain(this Step step)
=> step.If("github.ref == 'refs/heads/main'");

public static Job RunEitherOnBranchOrAsPR(this Job job)
=> job.If(
"(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) || (github.event_name == 'push')");

public static void StepTestAndReport(this Job job, string componentName, string testProject)
public static void StepTest(this Job job, string componentName, string testProject)
{
var path = $"test/{testProject}";
var logFileName = "Tests.trx";
var logFileName = $"{testProject}.trx";
var flags = $"--logger \"console;verbosity=normal\" " +
$"--logger \"trx;LogFileName={logFileName}\" " +
$"--collect:\"XPlat Code Coverage\"";
job.Step()
.Name($"Test - {testProject}")
.Run($"dotnet test -c Release {path} {flags}");

}

internal static void StepUploadTestResultsAsArtifact(this Job job, Component component)
{
job.Step()
.Name($"Test report")
.If("success() || failure()")
.Uses("actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882") // 4.4.3
.With(
("name", "test-results"),
("path", string.Join(Environment.NewLine, component.Tests
.Select(testProject => $"{component.Name}/test/{testProject}/TestResults/{testProject}.trx"))),
("retention-days", "5"));
}

internal static void StepGenerateReportFromTestArtifact(this Job job, Component component, string testProject)
{
var path = $"test/{testProject}";
job.Step()
.Name($"Test report - {testProject}")
.Name($"Test report - {component.Name} - {testProject}")
.Uses("dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5") // v1.9.1
.If("github.event_name == 'push' && (success() || failure())")
.If($"event.workflow.name == '{component.CiWorkflowName}'")
.With(
("artifact", "test-results"),
("name", $"Test Report - {testProject}"),
("path", $"{componentName}/{path}/TestResults/{logFileName}"),
("path", $"{testProject}.trx"),
("reporter", "dotnet-trx"),
("fail-on-error", "true"),
("fail-on-empty", "true"));
Expand Down Expand Up @@ -272,7 +327,7 @@ public static void StepUploadArtifacts(this Job job, string componentName)
var path = $"{componentName}/artifacts/*.nupkg";
job.Step()
.Name("Upload Artifacts")
.IfRefMain()
.IfGithubEventIsPush()
.Uses("actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882") // 4.4.3
.With(
("name", "artifacts"),
Expand Down
18 changes: 8 additions & 10 deletions .github/workflows/access-token-management-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@ jobs:
8.0.x
9.0.x
- name: Test - AccessTokenManagement.Tests
run: dotnet test -c Release test/AccessTokenManagement.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=Tests.trx" --collect:"XPlat Code Coverage"
- name: Test report - AccessTokenManagement.Tests
if: github.event_name == 'push' && (success() || failure())
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
run: dotnet test -c Release test/AccessTokenManagement.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=AccessTokenManagement.Tests.trx" --collect:"XPlat Code Coverage"
- name: Test report
if: success() || failure()
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
with:
name: Test Report - AccessTokenManagement.Tests
path: access-token-management/test/AccessTokenManagement.Tests/TestResults/Tests.trx
reporter: dotnet-trx
fail-on-error: true
fail-on-empty: true
name: test-results
path: access-token-management/test/AccessTokenManagement.Tests/TestResults/AccessTokenManagement.Tests.trx
retention-days: 5
- name: Tool restore
run: dotnet tool restore
- name: Pack AccessTokenManagement
Expand All @@ -72,7 +70,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload Artifacts
if: github.ref == 'refs/heads/main'
if: github.event_name == 'push'
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
with:
name: artifacts
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/access-token-management-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload Artifacts
if: github.ref == 'refs/heads/main'
if: github.event_name == 'push'
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
with:
name: artifacts
Expand Down
62 changes: 62 additions & 0 deletions .github/workflows/generate-test-reports.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# This was generated by tool. Edits will be overwritten.

name: generate-test-reports
on:
workflow_run:
workflows:
- 'ignore-this/ci'
- 'access-token-management/ci'
- 'identity-model/ci'
- 'identity-model-oidc-client/ci'
types:
- completed
jobs:
report:
name: report
runs-on: ubuntu-latest
permissions:
actions: read
checks: write
contents: read
packages: write
steps:
- name: Test report - ignore-this - IgnoreThis.Tests
if: event.workflow.name == 'ignore-this/ci'
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
with:
artifact: test-results
name: Test Report - IgnoreThis.Tests
path: IgnoreThis.Tests.trx
reporter: dotnet-trx
fail-on-error: true
fail-on-empty: true
- name: Test report - access-token-management - AccessTokenManagement.Tests
if: event.workflow.name == 'access-token-management/ci'
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
with:
artifact: test-results
name: Test Report - AccessTokenManagement.Tests
path: AccessTokenManagement.Tests.trx
reporter: dotnet-trx
fail-on-error: true
fail-on-empty: true
- name: Test report - identity-model - IdentityModel.Tests
if: event.workflow.name == 'identity-model/ci'
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
with:
artifact: test-results
name: Test Report - IdentityModel.Tests
path: IdentityModel.Tests.trx
reporter: dotnet-trx
fail-on-error: true
fail-on-empty: true
- name: Test report - identity-model-oidc-client - IdentityModel.OidcClient.Tests
if: event.workflow.name == 'identity-model-oidc-client/ci'
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
with:
artifact: test-results
name: Test Report - IdentityModel.OidcClient.Tests
path: IdentityModel.OidcClient.Tests.trx
reporter: dotnet-trx
fail-on-error: true
fail-on-empty: true
18 changes: 8 additions & 10 deletions .github/workflows/identity-model-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@ jobs:
8.0.x
9.0.x
- name: Test - IdentityModel.Tests
run: dotnet test -c Release test/IdentityModel.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=Tests.trx" --collect:"XPlat Code Coverage"
- name: Test report - IdentityModel.Tests
if: github.event_name == 'push' && (success() || failure())
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
run: dotnet test -c Release test/IdentityModel.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=IdentityModel.Tests.trx" --collect:"XPlat Code Coverage"
- name: Test report
if: success() || failure()
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
with:
name: Test Report - IdentityModel.Tests
path: identity-model/test/IdentityModel.Tests/TestResults/Tests.trx
reporter: dotnet-trx
fail-on-error: true
fail-on-empty: true
name: test-results
path: identity-model/test/IdentityModel.Tests/TestResults/IdentityModel.Tests.trx
retention-days: 5
- name: Tool restore
run: dotnet tool restore
- name: Pack IdentityModel
Expand All @@ -70,7 +68,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload Artifacts
if: github.ref == 'refs/heads/main'
if: github.event_name == 'push'
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
with:
name: artifacts
Expand Down
18 changes: 8 additions & 10 deletions .github/workflows/identity-model-oidc-client-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@ jobs:
8.0.x
9.0.x
- name: Test - IdentityModel.OidcClient.Tests
run: dotnet test -c Release test/IdentityModel.OidcClient.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=Tests.trx" --collect:"XPlat Code Coverage"
- name: Test report - IdentityModel.OidcClient.Tests
if: github.event_name == 'push' && (success() || failure())
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
run: dotnet test -c Release test/IdentityModel.OidcClient.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=IdentityModel.OidcClient.Tests.trx" --collect:"XPlat Code Coverage"
- name: Test report
if: success() || failure()
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
with:
name: Test Report - IdentityModel.OidcClient.Tests
path: identity-model-oidc-client/test/IdentityModel.OidcClient.Tests/TestResults/Tests.trx
reporter: dotnet-trx
fail-on-error: true
fail-on-empty: true
name: test-results
path: identity-model-oidc-client/test/IdentityModel.OidcClient.Tests/TestResults/IdentityModel.OidcClient.Tests.trx
retention-days: 5
- name: Tool restore
run: dotnet tool restore
- name: Pack IdentityModel.OidcClient
Expand All @@ -72,7 +70,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload Artifacts
if: github.ref == 'refs/heads/main'
if: github.event_name == 'push'
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
with:
name: artifacts
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/identity-model-oidc-client-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload Artifacts
if: github.ref == 'refs/heads/main'
if: github.event_name == 'push'
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
with:
name: artifacts
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/identity-model-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload Artifacts
if: github.ref == 'refs/heads/main'
if: github.event_name == 'push'
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
with:
name: artifacts
Expand Down
18 changes: 8 additions & 10 deletions .github/workflows/ignore-this-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@ jobs:
8.0.x
9.0.x
- name: Test - IgnoreThis.Tests
run: dotnet test -c Release test/IgnoreThis.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=Tests.trx" --collect:"XPlat Code Coverage"
- name: Test report - IgnoreThis.Tests
if: github.event_name == 'push' && (success() || failure())
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
run: dotnet test -c Release test/IgnoreThis.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=IgnoreThis.Tests.trx" --collect:"XPlat Code Coverage"
- name: Test report
if: success() || failure()
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
with:
name: Test Report - IgnoreThis.Tests
path: ignore-this/test/IgnoreThis.Tests/TestResults/Tests.trx
reporter: dotnet-trx
fail-on-error: true
fail-on-empty: true
name: test-results
path: ignore-this/test/IgnoreThis.Tests/TestResults/IgnoreThis.Tests.trx
retention-days: 5
- name: Tool restore
run: dotnet tool restore
- name: Pack IgnoreThis
Expand All @@ -70,7 +68,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload Artifacts
if: github.ref == 'refs/heads/main'
if: github.event_name == 'push'
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
with:
name: artifacts
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ignore-this-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload Artifacts
if: github.ref == 'refs/heads/main'
if: github.event_name == 'push'
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
with:
name: artifacts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<ProjectConfiguration>
<Settings />
</ProjectConfiguration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<ProjectConfiguration>
<Settings />
</ProjectConfiguration>

0 comments on commit 9a47c08

Please sign in to comment.