English
The backend job
The backend job turns the rules of the backend into gates: each step either passes or stops the job. This chapter goes through it from top to bottom.
Step 1. A fresh machine: the code and the SDK
Why. A runner starts empty: no repository, no .NET SDK of the right version, no packages. The first steps prepare it, and they must produce exactly the environment the developers have — otherwise CI would test something else than what runs on our computers.
Do. The job starts like this:
yaml
backend:
name: Backend
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v7
- uses: actions/setup-dotnet@v6
with:
global-json-file: global.json
- name: Cache NuGet packages
uses: actions/cache@v6
with:
path: ~/.nuget/packages
key: nuget-${{ runner.os }}-${{ hashFiles('Directory.Packages.props', '**/*.csproj') }}
restore-keys: nuget-${{ runner.os }}-name: Backendis the name of the check that appears on commits and pull requests; branch protection refers to it in Running CI.actions/checkoutclones the repository at the commit that triggered the run.actions/setup-dotnetinstalls the .NET SDK. Withglobal-json-file: global.jsonit reads the version from the same file that selects the SDK on our computers (topic 2), so the runner builds with the same compiler and the same analyzers. One file decides the SDK everywhere.actions/cachesaves the NuGet package folder after a successful run and restores it at the start of the next one, which saves downloading a few hundred megabytes. Thekeycontains a hash of every file that decides the package versions:Directory.Packages.propsand the project files. When a version changes, the key changes and a fresh cache is built;restore-keyslets the job start from the latest older cache of the same system in the meantime, so only the changed packages are downloaded.
uses: and versions
actions/checkout@v7 means the repository actions/checkout at the tag v7. The major version tag moves with bug fixes but never with breaking changes. Teams with strict supply-chain rules pin actions by the full commit hash instead of a tag, because a tag can be moved by whoever controls the action's repository.
Step 2. Restore and the format check
Why. Restoring packages and tools once, as a separate step, lets every later step run with --no-restore/--no-build and makes failures easy to locate: a red Restore step means a package problem, not a code problem. The format check comes next because it is the cheapest gate: there is no point in compiling code that will be rejected for a missing space.
Do.
yaml
- name: Restore
run: |
dotnet restore Chatex.slnx
dotnet tool restore
- name: Check formatting
run: dotnet format Chatex.slnx --verify-no-changes --no-restoredotnet restore Chatex.slnxdownloads the packages of all seven projects. NuGet audit runs here (Step 3).dotnet tool restoreinstalls the local tools ofdotnet-tools.json—dotnet-ef10.0.14, which the migration check needs.dotnet format --verify-no-changesapplies the.editorconfigrules of topic 2 in memory and fails if any file would change. It changes nothing on disk. An extra space afterusingin the first line of a file is reported as:
…\Source\Chatex.Api\Contracts\ApiContracts.cs(1,7): error WHITESPACE: Fix whitespace formatting. Delete 1 characters. […\Source\Chatex.Api\Chatex.Api.csproj]The command exits with code 2, and GitHub marks the step red. The fix is always the same: run ./Run.ps1 format (or dotnet format Chatex.slnx) locally and push again. Code reviews never have to discuss spaces.
Step 3. The Release build
Why. The deployment of topic 24 publishes a Release build, and Release can behave differently from Debug: the optimiser is on, #if DEBUG code disappears, some analyzers report other things. So CI builds what we ship.
Do.
yaml
- name: Build
run: dotnet build Chatex.slnx -c Release --no-restoreMost of the work of this step was configured long ago in Directory.Build.props (topic 2):
xml
<Project>
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>14</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<!-- .NET analyzers at the recommended level, plus the IDE style rules from .editorconfig, checked on build. -->
<AnalysisLevel>latest-recommended</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<!-- Restore checks direct and transitive packages against known advisories; with warnings as errors a
vulnerable package fails the build, in CI and in the deploy. -->
<NuGetAudit>true</NuGetAudit>
<NuGetAuditMode>all</NuGetAuditMode>
</PropertyGroup>
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
</Project>TreatWarningsAsErrorswith the analyzers: a possiblenulldereference, an unused variable or a style violation stops the build.NuGetAuditwithNuGetAuditModeall: every package, including packages that our packages depend on, is compared with the GitHub advisory database. A package with a known vulnerability produces a warning — and therefore an error. A security advisory published tomorrow will turn tomorrow's CI run red without any change in our code; that is intended.ContinuousIntegrationBuildis switched on only when the variableGITHUB_ACTIONSistrue, which GitHub sets on every runner. It makes the build output independent of the folder it was built in.
Step 4. A missing migration
Why. Topic 4 showed what happens when an entity changes without a migration: the code expects a column that the database does not have, and the error appears only at run time — in the worst case on the production server. EF Core can detect this situation statically: it compares the model of the compiled code with the snapshot stored together with the last migration.
Do.
yaml
- name: Check for model changes without a migration
run: dotnet ef migrations has-pending-model-changes --project Source/Chatex.Data --configuration Release --no-build--configuration Release --no-builduses the assembly that the previous step has just built, so the check costs a second.- The command needs no database: it compares two models in memory.
On a clean repository it prints:
No changes have been made to the model since the last migration.If someone changed, say, the maximum length of Attachment.FileName and forgot ./Run.ps1 migration <Name>, it prints the following and exits with code 1, which fails the job:
Changes have been made to the model since the last migration. Add a new migration.Step 5. Tests with a real PostgreSQL
Why. The tests of topics 6–14 run against a real PostgreSQL in a Docker container, started by Testcontainers (PostgresFixture in Chatex.Testing). Would they work in CI? Yes, and without any extra setup: the Ubuntu runners of GitHub have Docker installed and running. A service container in the workflow (services: postgres:) would also work, but then CI and the developers' computers would start the database in two different ways; with Testcontainers the tests themselves start what they need, everywhere the same.
Do.
yaml
# Testcontainers starts PostgreSQL in the runner's Docker.
- name: Test
run: dotnet test --solution Chatex.slnx -c Release --no-build
- name: Upload test results
if: failure()
uses: actions/upload-artifact@v7
with:
name: test-results
path: '**/TestResults/**'
if-no-files-found: ignoredotnet test --solutionuses the Microsoft Testing Platform runner selected inglobal.json(topic 6). Each test assembly starts onepostgres:18-alpinecontainer, applies the real migrations and resets the data before each test; the three assemblies run in parallel, each with its own container.-c Release --no-buildtests exactly the assemblies built and checked above.if: failure()runs the upload step only when an earlier step has failed. Normally a step is skipped after a failure; this condition reverses that.upload-artifactattaches every file under aTestResultsfolder to the run as the artifacttest-results. The test platform writes files there when you turn on reports or when a test process crashes; withif-no-files-found: ignorean empty upload is not an error.
The output of a successful step ends with a summary like this (the paths are shortened):
Test run summary: Passed!
…/Chatex.Api.Tests.dll (net10.0|x64) passed (26s 617ms)
…/Chatex.Core.Tests.dll (net10.0|x64) passed (19s 906ms)
…/Chatex.Data.Tests.dll (net10.0|x64) passed (14s 850ms)
total: 93
failed: 0
succeeded: 93
skipped: 0Check
Run the backend part of the job on your computer, step by step, from the repository root (Docker must be running):
powershell
dotnet format Chatex.slnx --verify-no-changes --no-restorepowershell
dotnet build Chatex.slnx -c Release --no-restorepowershell
dotnet ef migrations has-pending-model-changes --project Source/Chatex.Data --configuration Release --no-buildpowershell
dotnet test --solution Chatex.slnx -c Release --no-buildThe format check prints nothing, the build reports 0 Warning(s) and 0 Error(s), the migration check prints No changes have been made to the model since the last migration., and the tests end with total: 93, failed: 0. Running CI runs all of this with one command.