Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mark stage as unstable if Powershell condition results into true?

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?

like image 290
Olaf Svenson Avatar asked Jul 15 '21 14:07

Olaf Svenson


People also ask

What is true and false statement in PowerShell?

Any condition other than zero, false, blank are considered as true only .for example if any conditional expression gives an output of 0,”, false all these are considered as false statements. How does if statement work in PowerShell works?

How do I Mark a specific stage as unstable?

To mark only specific stages as unstable, use the step unstable (message: String) as described here within your stage and install/update the following plugins: Note: This also marks the overall build status as unstable. Show activity on this post.

What is the syntax of if statement in PowerShell?

Syntax: Syntax of if in PowerShell is very much similar to other programming languages. It checks for the condition if the condition expression is true it will be got to if block if the condition expression is false it will go to else. if (condition) {. // Executes when the condition is true. }else {.

Can we use PowerShell test-path inside condition statements?

We can use normal PowerShell inside the condition statement. Test-Path returns $true or $false when it executes. This also applies to commands that return other values. It evaluates to $true if there's a returned process and $false if there'sn'thing.


Video Answer


2 Answers

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!

like image 113
Olaf Svenson Avatar answered Sep 28 '22 19:09

Olaf Svenson


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

like image 20
SPMSE Avatar answered Sep 28 '22 17:09

SPMSE