CI #46
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
# name of the workflow. Link to the documentation - https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#name | |
name: CI | |
# running on push to main and develop branches or on pull reuqests or on manual trigger | |
on: | |
# manual trigger | |
workflow_dispatch: | |
inputs: | |
ssh_debug_enabled: | |
type: boolean | |
description: 'Run the build/test with ssh debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)' | |
required: false | |
default: false | |
debug_deployment: | |
type: boolean | |
description: 'Run the pipeline with debug deployment enabled' | |
required: false | |
default: false | |
# runnnig on push to main and develop branches | |
push: | |
branches: | |
- main | |
- develop | |
paths-ignore: | |
- '**/README.md' | |
- '.devcontainer/**' | |
# running on pull requests to main and develop branches | |
pull_request: | |
branches: | |
- main | |
- develop | |
paths-ignore: | |
- '**/README.md' | |
- '.devcontainer/**' | |
# defining global environment variables for all jobs | |
env: | |
# define runner indexes for tests splitting and parallel execution | |
total-runners: 2 | |
# defining GitHub registry for docker images | |
REGISTRY: ghcr.io | |
# github.repository as <account>/<repo> | |
IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
build: | |
runs-on: ${{ matrix.os }} | |
name: Build ${{ matrix.os }} | |
permissions: | |
actions: read | |
contents: read | |
packages: write | |
id-token: write | |
security-events: write | |
strategy: | |
matrix: | |
# os: [ubuntu-latest, windows-latest, macos-latest] | |
os: [ubuntu-latest] | |
language: [csharp] | |
steps: | |
- name: Checkout code | |
uses: actions/[email protected] | |
- name: Setup .NET | |
uses: actions/[email protected] | |
with: | |
dotnet-version: '6.0.x' | |
- name: Cache NuGet packages | |
uses: actions/[email protected] | |
with: | |
path: ~/.nuget/packages | |
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj', '**/global.json') }} | |
restore-keys: | | |
${{ runner.os }}-nuget- | |
- name: Restore dependencies | |
run: dotnet restore RazorPagesMovie.csproj | |
- name: Build App project | |
run: dotnet build RazorPagesMovie.csproj --configuration Release --no-restore | |
- name: Publish | |
run: dotnet publish RazorPagesMovie.csproj --configuration Release --output ./publish --self-contained --runtime ${{ matrix.os == 'ubuntu-latest' && 'linux-x64' || matrix.os == 'windows-latest' && 'win-x64' || 'osx-x64' }} | |
- name: Upload published app | |
uses: actions/[email protected] | |
with: | |
name: RazorPagesMovie-${{ matrix.os }} | |
path: ./publish | |
test: | |
runs-on: ${{ matrix.os }} | |
needs: | |
- build | |
- runner-indexes | |
permissions: | |
contents: read # read access to the repository contents | |
packages: write # write access to the repository packages | |
id-token: write # write access to the repository id token | |
strategy: | |
matrix: | |
# os: [ubuntu-latest, windows-latest, macos-latest] | |
os: [ubuntu-latest] | |
index: ${{ fromJson(needs.runner-indexes.outputs.json) }} | |
steps: | |
- name: Checkout code | |
uses: actions/[email protected] | |
- name: Setup .NET | |
uses: actions/[email protected] | |
with: | |
dotnet-version: '6.0.x' | |
- name: Cache NuGet packages | |
uses: actions/[email protected] | |
with: | |
path: ~/.nuget/packages | |
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj', '**/global.json') }} | |
restore-keys: | | |
${{ runner.os }}-nuget- | |
- name: Setup tmate session | |
uses: mxschmitt/action-tmate@v3 | |
if: ${{ github.event_name == 'workflow_dispatch' && inputs.ssh_debug_enabled }} | |
- name: Split Tests | |
id: split-test | |
uses: chaosaffe/[email protected] | |
with: | |
glob: RazorPagesMovie.Tests/**/*Tests.cs | |
split-total: ${{ env.total-runners }} | |
split-index: ${{ matrix.index }} | |
line-count: true | |
- name: Restore dependencies | |
run: dotnet restore RazorPagesMovie.Tests/RazorPagesMovie.Tests.csproj | |
- name: Convert Test File Path to Fully Qualified Name | |
id: convert-path | |
run: | | |
test_suite="${{ steps.split-test.outputs.test-suite }}" | |
fully_qualified_name=$(echo $test_suite | sed 's/\//./g' | sed 's/.cs//g') | |
echo "fully_qualified_name=$fully_qualified_name" >> $GITHUB_ENV | |
- run: 'echo "This runner will execute the following tests: ${{ steps.split-test.outputs.test-suite }}."' | |
- run: dotnet test RazorPagesMovie.Tests/RazorPagesMovie.Tests.csproj --filter "FullyQualifiedName~${{ env.fully_qualified_name }}" --logger "console;verbosity=detailed" --logger "trx;LogFileName=./testresults-${{ matrix.index }}-testresults-${{ matrix.os }}-${{ github.run_id }}-${{ github.run_attempt }}.trx" --results-directory ./testresults | |
- name: Upload test results | |
if : always() | |
uses: actions/[email protected] | |
with: | |
name: testresults-${{ github.run_id }}-split-${{ matrix.index }} | |
path: ./testresults | |
if-no-files-found: warn | |
compression-level: 6 | |
runner-indexes: # job to generate the runner indexes for the unit-parallel-tests job | |
runs-on: ubuntu-latest | |
name: Generate runner indexes | |
outputs: | |
json: ${{ steps.generate-index-list.outputs.json }} # output the json with the runner indexes | |
steps: | |
- id: generate-index-list # generate the runner indexes and save them to the json file | |
run: | | |
MAX_INDEX=$((${{ env.total-runners }}-1)) # calculate the max index | |
INDEX_LIST=$(seq 0 ${MAX_INDEX}) # generate the list of indexes | |
INDEX_JSON=$(jq --null-input --compact-output '. |= [inputs]' <<< ${INDEX_LIST}) # convert the list to the json | |
echo "json=${INDEX_JSON}" >> $GITHUB_OUTPUT # save the json to the GITHUB_OUTPUT environment variable |