Merge pull request #56 from skrysmanski/fix/test-report #461
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# GitHub Actions workflow: Builds and tests the code in this repository. | |
# | |
# For more details on workflows, see README.md. | |
# | |
# IMPORTANT: When changing this name, also change the name in the trigger section in "test-report.yaml". | |
name: CI | |
# When to run this workflow | |
# | |
# See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows | |
# See: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#on | |
# | |
# TIP: Don't use "schedule" triggers as this will cause the workflow to be disabled after 60 days of inactivity | |
# (and afterward the workflow must be manually reenabled). | |
on: | |
# Trigger the workflow on push to the main branch. | |
push: | |
branches: | |
- main | |
# Trigger the workflow for any pull requests. | |
pull_request: | |
# Allow manual run of this workflow (https://docs.github.com/en/actions/using-workflows/manually-running-a-workflow) | |
workflow_dispatch: | |
# Permissions for GITHUB_TOKEN for this workflow. | |
# | |
# See: https://docs.github.com/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token | |
# | |
# NOTE: Because we run with minimal permissions, we use "@vX" (instead of "@hash") for non-GitHub steps below. | |
# Usually you would use "@hash" as a security measure to pin a specific version. However, since we run with | |
# minimal permissions here, malicious code can't do much harm (most likely). For more details, see: | |
# https://blog.gitguardian.com/github-actions-security-cheat-sheet/#use-specific-action-version-tags | |
permissions: | |
contents: read | |
env: | |
DOTNET_VERSION: '8.0' | |
# NOTE: Jobs run in parallel by default. | |
# https://docs.github.com/en/actions/using-jobs/using-jobs-in-a-workflow | |
jobs: | |
# | |
# Build & Test job | |
# | |
build-and-test: | |
# Name of the job | |
name: Build & Test | |
# Set the type of machine to run on | |
# See: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on | |
runs-on: ubuntu-latest | |
steps: | |
########################################################################### | |
# | |
# Setup Steps | |
# | |
########################################################################### | |
# See: https://github.com/marketplace/actions/checkout | |
- name: Clone Git repository | |
uses: actions/checkout@v4 | |
with: | |
lfs: true | |
submodules: true | |
# This creates ${{ steps.short-sha.outputs.sha }} to be used below. | |
# See: https://github.com/marketplace/actions/short-sha | |
- name: Determine Git short commit hash | |
id: short-sha | |
uses: benjlevesque/short-sha@v3.0 | |
# See: https://github.com/marketplace/actions/setup-net-core-sdk | |
- name: Setup .NET build environment | |
uses: actions/setup-dotnet@v4 | |
with: | |
# NOTE: Apparently only the 3rd component can be "x"; i.e. "5.x" is not supported. | |
dotnet-version: '${{ env.DOTNET_VERSION }}.x' | |
########################################################################### | |
# | |
# Build Steps | |
# | |
########################################################################### | |
# See: https://docs.microsoft.com/de-de/dotnet/core/tools/dotnet-build | |
# NOTE: Without specifying a solution file, "dotnet build" searches for a .sln file in the current directory. | |
- name: Build code | |
run: dotnet build --configuration Release | |
# See: https://docs.microsoft.com/de-de/dotnet/core/tools/dotnet-test | |
# NOTES: | |
# * Without specifying a solution file, "dotnet test" searches for a .sln file in the current directory. | |
# * There seems to be no way to name the .trx file as '<project>.trx'. If no 'LogFileName' is specified, | |
# the .trx files will be named something like "_fv-az278-737_2021-08-15_03_50_33.trx". | |
# * The Option 'TreatNoTestsAsError' is documented here: https://learn.microsoft.com/en-us/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file?view=vs-2022#runconfiguration-element | |
- name: Run tests | |
id: run_tests | |
run: dotnet test --configuration Release --no-restore --no-build --logger "trx;LogFileName=${{ runner.os }}.trx" --nologo -- RunConfiguration.TreatNoTestsAsError=true | |
env: | |
# Tells tests that they're running in a (potentially slow) CI environment. | |
RUNS_IN_CI: true | |
########################################################################### | |
# | |
# Archive Steps | |
# | |
########################################################################### | |
# See: https://github.com/marketplace/actions/upload-a-build-artifact | |
- name: Upload test results | |
uses: actions/upload-artifact@v4 | |
# Run this step even if "run_tests" has failed (but not if any other previous step has failed - which would | |
# be "failure()" - because in this case the tests have not run and thus no .trx files have been generated). | |
# See: https://docs.github.com/en/actions/learn-github-actions/expressions#failure | |
if: success() || steps.run_tests.conclusion == 'failure' | |
with: | |
# NOTE: To make the downloads of the test results easier to use (i.e. when downloading test results | |
# from different runs), we'll add an id to the name. | |
# | |
# We don't just use the sha because this workflow also runs on a schedule - which means that different | |
# runs would again create files with the same name (e.g. two consecutive scheduled runs while the | |
# repo hasn't changed in the meantime). | |
# | |
# Instead we use 'github.run_number' because this gives us the same number that's also shown in the | |
# ui - like 27 for run #27 ('github.run_id' on the other hand gives us some "random" big number like | |
# 1152888876 - which is less useful). For more details, see: | |
# https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context | |
# | |
# NOTE: We put the "run_number" first so that the result zip file can be sorted by name. | |
name: 'test-results-#${{ github.run_number }}-${{ steps.short-sha.outputs.sha }}-${{ runner.os }}' | |
path: '**/*.trx' | |
if-no-files-found: error | |
# | |
# Create action test report | |
# | |
test-report: | |
# Name of the job | |
name: Test Report | |
needs: build-and-test | |
# Set the type of machine to run on | |
# See: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on | |
runs-on: ubuntu-latest | |
permissions: | |
actions: read | |
checks: write | |
steps: | |
# See: https://github.com/marketplace/actions/download-a-build-artifact | |
# | |
# The dorny/test-reporter@v1.8.0 action doesn't support actions/upload-artifact@v4 yet. | |
# We therefore download the artifact manually and feed it to test-reporter as local files. | |
# See: https://github.com/dorny/test-reporter/issues/363 | |
- name: Download test results | |
uses: actions/download-artifact@v4 | |
with: | |
pattern: test-results-* | |
path: test-results | |
# See: https://github.com/marketplace/actions/test-reporter | |
- name: Create test report | |
# For pinned versions, see: https://blog.gitguardian.com/github-actions-security-cheat-sheet/#use-specific-action-version-tags | |
uses: dorny/test-reporter@eaa763f6ffc21c7a37837f56cd5f9737f27fc6c8 # version 1.8.0 | |
with: | |
# NOTE: We add the 'github.run_number' to the name so that we can easier identify the | |
# test report if they pile up due to bug https://github.com/dorny/test-reporter/issues/67. | |
# See top of this file for more details. | |
name: 'Test Report #${{ github.run_number }}' | |
# Path to test results (downloaded in previous step) | |
path: 'test-results/**/*.trx' | |
# Format of test results | |
reporter: dotnet-trx | |
# Don't mark the test report generated as failed if there's a failed test. | |
# Only mark it as failed if something with the workflow has actually gone wrong. | |
fail-on-error: false | |
# Workaround for error 'fatal: not a git repository' caused by a call to 'git ls-files' | |
# See: https://github.com/dorny/test-reporter/issues/169#issuecomment-1583560458 | |
max-annotations: 0 | |
# | |
# CodeQL job | |
# | |
codeql: | |
# Name of the job | |
name: CodeQL | |
# Set the type of machine to run on | |
# See: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on | |
runs-on: ubuntu-latest | |
permissions: | |
actions: read | |
security-events: write | |
steps: | |
########################################################################### | |
# | |
# Setup Steps | |
# | |
########################################################################### | |
# See: https://github.com/marketplace/actions/checkout | |
- name: Clone Git repository | |
uses: actions/checkout@v4 | |
with: | |
lfs: true | |
submodules: true | |
# Initializes the CodeQL tools for scanning. | |
# See: https://github.com/github/codeql-action | |
- name: Initialize CodeQL | |
uses: github/codeql-action/init@v3 | |
with: | |
languages: csharp | |
# See: https://github.com/marketplace/actions/setup-net-core-sdk | |
- name: Setup .NET build environment | |
uses: actions/setup-dotnet@v4 | |
with: | |
# NOTE: Apparently only the 3rd component can be "x"; i.e. "5.x" is not supported. | |
dotnet-version: '${{ env.DOTNET_VERSION }}.x' | |
########################################################################### | |
# | |
# Build Steps | |
# | |
########################################################################### | |
# See: https://docs.microsoft.com/de-de/dotnet/core/tools/dotnet-build | |
# NOTE: Without specifying a solution file, "dotnet build" searches for a .sln file in the current directory. | |
- name: Build code | |
run: dotnet build --configuration CodeQL_Release | |
# See: https://github.com/github/codeql-action | |
- name: Perform CodeQL Analysis | |
uses: github/codeql-action/analyze@v3 |