Dotnet scanner error on Gitlab pipeline, how to resolve?

1.4k Views Asked by At

I have sonarqube installed via an image with docker on a ubuntu 20.04 machine.

I have gitlab running and use the pipeline to execute sonar scans. For python and javascript repositories this is working just fine, but I have multiple c# projects I want to analyse to.

The following script is in use in the gitlab-ci.yml file:

sonarqube-check:
  stage: sonarqube-check
  image: mcr.microsoft.com/dotnet/core/sdk:latest
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # Defines the location of the analysis task cache
    GIT_DEPTH: "0"  # Tells git to fetch all the branches of the project, required by the analysis task
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script: 
      - "sudo apt-get update"
      - "sudo apt-get install --yes openjdk-11-jre"
      - "sudo add-apt-repository universe"
      - "sudo apt-get install apt-transport-https"
      - "sudo dotnet tool update --global dotnet-sonarscanner"
      - "export PATH=\"$PATH:/root/.dotnet/tools\""
      - "echo $PATH"
      - "sudo dotnet tool list -g"
      - "sudo dotnet sonarscanner begin /k:\"machtrans_nanders-stand-up_secretkey\" /d:sonar.login=\"$SONAR_TOKEN\" /d:\"sonar.host.url=$SONAR_HOST_URL\" "
      - "sudo dotnet build"
      - "sudo dotnet sonarscanner end /d:sonar.login=\"$SONAR_TOKEN\""
  allow_failure: true
  only:
    - chore/implement-sonarqube-check

I have manually installed the net-sdk-6.0 on the gitlab runner machine, which works just fine like this.

When I run this I get the following error and tried a lot of stuff, but don't know how to solve it, I hope one of the community can help me:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/root/.dotnet/tools
$ sudo dotnet tool list -g
Package Id               Version      Commands           
---------------------------------------------------------
dotnet-sonarscanner      5.5.3        dotnet-sonarscanner
$ sudo dotnet sonarscanner begin /k:"machtrans_nanders-stand-1313216854315" /d:sonar.login="$SONAR_TOKEN" /d:"sonar.host.url=$SONAR_HOST_URL"
Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET program, but dotnet-sonarscanner does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
1

There are 1 best solutions below

0
On

Make sure the path /root/.dotnet/tools exist. My guess would be that when you use it with sudo dotnet tool update --global dotnet-sonarscanner and the next line, that it doesn't exist yet, so when you then do the dotnet sonarscanner begin, it can't find the toold in /root/.dotnet/tools because the path doesn't exist in your PATH.

Change it like so:

- "sudo apt-get install apt-transport-https"
- "mkdir -p /root/.dotnet/tools"
- "sudo dotnet tool update --global dotnet-sonarscanner"