I have a .Net 5 solution and build the projects with code style analysis. Each violated rule results in a warning. But the build command exits with the code 0.
I created a .gitlab-ci.yml file that should mark the pipeline as unstable if any build warnings have been thrown.
image: mcr.microsoft.com/dotnet/sdk:5.0
stages:
- build
- unit-tests
build:
stage: build
script:
- |-
dotnet build --output build -consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build" -fl1 "/flp1:warningsonly";
if ((!$LASTEXITCODE) -and (Get-Content "msbuild1.log"))
{
# >>> mark stage as unstable here <<<
}
artifacts:
paths:
- build
unit-tests:
stage: unit-tests
script:
- dotnet test --no-build --output build
dependencies:
- build
The stage itself passes but I was hoping that it passes with an unstable state (because of the warnings), unfortunately it's green.
A bad solution would be adding the -warnaserror flag for the build command and use allow_failure: true for the stage. This would set the stage into an unstable state but next stages would fail because of the missing build.
So what would be the correct way to check if the build command finished with warnings to mark the stage as unstable?
I tried to add an additional stage code-quality which works perfectly fine for me
image: mcr.microsoft.com/dotnet/sdk:5.0
stages:
- build
- code-quality
- unit-tests
build:
stage: build
script:
- dotnet build --output build
artifacts:
paths:
- build
code-quality:
stage: code-quality
script:
- |-
dotnet tool install -g dotnet-format;
dotnet format -wsa --check --verbosity diagnostic;
if ($LASTEXITCODE)
{
exit 1;
}
allow_failure: true
dependencies:
- build
unit-tests:
stage: unit-tests
script:
- dotnet test --no-build --output build
dependencies:
- build
The only thing I'm wondering about is why I have to check for the exit code. If dotnet format fails it exits with the code 2. But Gitlab seems to check for the exit code 0 or 1 only? Because the error code 2 results into success. That's why I have to check for it manually.
If someone knows how to improve this please let me know!
Not quite sure if this would be what you are looking for but my understanding is that your unit-tests depends on the previous code-quality or build step (or both)?
In this case you modify your unit-tests step to be something like:
unit-tests:
stage: unit-tests
script:
- dotnet test --no-build --output build
# replace 'dependencies' with 'needs'
needs:
- job: build
artifacts: true
- job: code-quality
The consequences would be that if the build fails your pipeline will fail, in case the code-quality step fails it will still be able to run the unit-tests, except when the test step would depend on artifacts from code quality step