CI/CD

Publish Only After the Tests Pass

5 min read

The publish job should push an image only for a commit whose tests passed, so it waits for the test job through needs and then compiles the image itself with dotnet publish -t:PublishContainer. The test job's output is never reused, which keeps the image a build from source.

The test job and the gate

Both jobs live in .gitlab-ci.yml:

stages: [test, publish]

variables:
  NUGET_PACKAGES: $CI_PROJECT_DIR/.nuget

test:
  stage: test
  image: mcr.microsoft.com/dotnet/sdk:10.0
  interruptible: true
  script:
    - dotnet test App.sln -c Release --logger "junit;LogFilePath=$CI_PROJECT_DIR/test-results/{assembly}.xml"
  artifacts:
    reports:
      junit: test-results/*.xml
  cache:
    key: nuget-$CI_COMMIT_REF_SLUG
    paths: [.nuget]
    policy: pull-push

publish:
  stage: publish
  image: mcr.microsoft.com/dotnet/sdk:10.0
  interruptible: false
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  needs:
    - job: test
      artifacts: false
  variables:
    DOTNET_CONTAINER_REGISTRY_UNAME: $REGISTRY_USER
    DOTNET_CONTAINER_REGISTRY_PWORD: $REGISTRY_PASSWORD
  cache:
    key: nuget-$CI_COMMIT_REF_SLUG
    paths: [.nuget]
    policy: pull
  script:
    - dotnet publish Api/Api.csproj -c Release -t:PublishContainer
      -p:ContainerRegistry=registry.example.com
      -p:ContainerImageTag=$CI_COMMIT_SHORT_SHA

stages: has to name publish, because it is not one of GitLab's default stages. Left out, GitLab rejects the whole file with publish job: chosen stage publish does not exist; available stages are .pre, test, .post. The rule on publish keeps the push to the default branch, as in Pipelines for Branches and Merge Requests, so a feature branch runs its tests without producing an image.

The stages already order the two jobs, because a job in a later stage does not run when a job in an earlier stage fails, so the exit code of dotnet test decides whether this commit gets an image even without needs. needs: - job: test makes that dependency explicit on the job itself and lets publish start the moment test succeeds, instead of waiting for every other job in the test stage. artifacts: false on the same entry skips downloading anything the test job uploaded, because publish compiles Api/Api.csproj from the checkout and has no use for test output. The Image Tag Is the Commit covers the tag on the last line.

Test results the merge request can read

The junit logger is not built into dotnet test. Each test project needs the JunitXml.TestLogger package, and without it the job fails before any test runs, with Could not find a test logger with AssemblyQualifiedName, URI or FriendlyName 'junit'. That failure also stops publish, so a missing package looks like a broken pipeline rather than a reporting gap.

The path matters as much as the package. A relative LogFilePath is resolved from each test project's own folder, so LogFilePath=test-results.xml across a solution with two test projects wrote Api.Tests/test-results.xml and Web.Tests/test-results.xml, and a report path of test-results.xml at the root of the checkout would match neither. $CI_PROJECT_DIR/test-results/{assembly}.xml writes every project's file into one folder, with the {assembly} token keeping the names apart, and junit: test-results/*.xml collects them all. GitLab uploads artifacts:reports whether the job passed or failed, which is when the test list matters most, and a failing test still wrote its file, with failures="1" on the testsuite element.

The solution file is whatever the repository has. dotnet new sln on the .NET 10 SDK writes App.slnx, the XML format, and dotnet test App.slnx ran both test projects the same way.

Credentials for the push

-t:PublishContainer pushes from inside the SDK, so it reads registry credentials the SDK way, not through docker login. With no credentials, the push to a registry that needs a login failed with CONTAINER1008: Failed retrieving credentials for "registry.test:5000": Docker config file doesn't exist, because the SDK image has no Docker config to look in. DOTNET_CONTAINER_REGISTRY_UNAME and DOTNET_CONTAINER_REGISTRY_PWORD are the variables it reads instead, so the job's variables: block maps them from REGISTRY_USER and REGISTRY_PASSWORD, two protected, masked CI/CD variables that only pipelines on protected branches can read. With them set, the same command printed Pushed image 'api:a1b2c3d', and with a wrong user name it failed with CONTAINER1016: Unable to access the repository 'api'. For GitLab's own container registry, the predefined $CI_REGISTRY_USER and $CI_REGISTRY_PASSWORD take the place of the two project variables, and $CI_REGISTRY takes the place of registry.example.com.

The registry name has to look like a host name. A bare name such as reg:5000 failed with CONTAINER2012: Could not recognize registry 'reg:5000', which only matters for a local test registry, since a real one has a domain.

A NuGet cache one job writes

NUGET_PACKAGES points NuGet's package folder at .nuget inside the project directory, which is the path both cache blocks name. The test job restores into it and uploads it with policy: pull-push, so the next pipeline on the same branch starts with the packages already present.

The publish job uses policy: pull, which downloads the cache and never uploads it. That matters once there are several publish jobs, one per image, because jobs that all upload to the same nuget-$CI_COMMIT_REF_SLUG key race each other, and whichever upload finishes last becomes the cache. Two pipelines for the same branch can still race through their test jobs, and the same rule applies there: the last upload is the cache the next pipeline restores.

Cancel the tests and keep the push

interruptible: true on test lets GitLab cancel that job when a newer commit lands on the same branch, since a test run for a superseded commit is wasted runner time. interruptible: false is already the default, and spelling it out on publish records the intent: with GitLab's default auto-cancel mode, a pipeline is cancelled only while every job that has started is interruptible, so once publish starts, a newer commit no longer cancels a push to the registry that is under way. GitLab applies this through the project's auto-cancel setting for redundant pipelines, so the keywords take effect only while that setting is on.

What a run showed

The two jobs' commands ran in mcr.microsoft.com/dotnet/sdk:10.0 (SDK 10.0.404) against a solution with an API project and two xUnit test projects, pushing to a local registry:3 that required a login, and GitLab 19.6.1's CI lint read the whole file:

Case Result
--logger junit without JunitXml.TestLogger Exit 1, Could not find a test logger … 'junit'
With the package, LogFilePath=test-results.xml One file in each test project's folder, none at the root
With $CI_PROJECT_DIR/test-results/{assembly}.xml test-results/Api.Tests.xml and test-results/Web.Tests.xml
One failing test Exit 1, both files written, failures="1" in the failing project's file
Push with no credentials CONTAINER1008, no Docker config
Push with DOTNET_CONTAINER_REGISTRY_UNAME and _PWORD Pushed image 'api:a1b2c3d'
Push with a wrong user CONTAINER1016, no access to the repository
Lint on the default branch, then on another branch test and publish, then test alone
Lint with publish missing from stages: Rejected: stage publish does not exist

The lint shows which jobs a branch gets; it does not run them, so the skip of publish after a failed test rests on GitLab's stage ordering rather than on a pipeline that was watched.

Put together, the test job's exit code decides whether a commit gets tagged, and the publish job still builds the image from source with dotnet publish instead of zipping the test output. That second compile is the cost of the design: the pulled NuGet cache keeps the restore cheap, but the build itself runs twice.

Back to articles
Share X LinkedIn
An unhandled error has occurred. Reload 🗙