Dotnet Sonarscanner在Docker建造过程中失败

发布于 2025-02-07 12:18:31 字数 1662 浏览 2 评论 0原文

我正在为Azure DevOps中的.NET Core应用程序内的Docker容器内的声纳扫描仪实现解决方案。

目前,安装的声纹质量是在AKS群集中的Sonarqube名称空间上,我们的DOT Net应用程序在同一群集中以不同的名称空间进行了容器。我正在努力处理以下错误:临时分析目录(通常是.sonarqube)不存在。 “开始”步骤可能没有执行。是否有任何实现这种逻辑的示例或工作码头文件?我试图按照此博客文章,但结果是相同的。

这是我的Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

RUN apt-get update && apt-get install -y openjdk-11-jdk
RUN dotnet tool install --global dotnet-sonarscanner
RUN dotnet tool install --global dotnet-reportgenerator-globaltool
RUN dotnet tool install --global coverlet.console

## Set the dotnet tools folder in the PATH env variable
ENV PATH="$PATH:/root/.dotnet/tools"

## Start scanner
RUN dotnet sonarscanner begin \
  /k:"my-token" \
  /o:"my-org" \
  /d:sonar.host.url="sonar-host" \
  /d:sonar.login="sonar-token" \
  /d:sonar.cs.opencover.reportsPaths=/coverage.opencover.xml

COPY my-project/*.csproj ./my-project/

COPY . .
WORKDIR /src/my-project
RUN dotnet restore
RUN dotnet build -c Release -o /app/build --no-restore
RUN dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput="/coverage"

## Stop scanner
RUN dotnet sonarscanner end /d:sonar.login="sonar-token"

FROM build AS publish
WORKDIR /src/my-project
RUN dotnet publish -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "my-project.dll"]

I am working on implementing a solution for a sonar scanner inside a docker container for the .net core application in azure DevOps.

Currently, the sonarqube installed was on a sonarqube namespace in the AKS cluster and our dot net application is containerized in a different namespace in the same cluster. I'm struggling with the following error: The temporary analysis directory (usually .sonarqube) doesn't exist. The "begin" step was probably not executed. Is there any sample or working Docker file which implements this type of logic? I tried to follow the steps described in this blog post but the result is the same.

Here is my Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

RUN apt-get update && apt-get install -y openjdk-11-jdk
RUN dotnet tool install --global dotnet-sonarscanner
RUN dotnet tool install --global dotnet-reportgenerator-globaltool
RUN dotnet tool install --global coverlet.console

## Set the dotnet tools folder in the PATH env variable
ENV PATH="$PATH:/root/.dotnet/tools"

## Start scanner
RUN dotnet sonarscanner begin \
  /k:"my-token" \
  /o:"my-org" \
  /d:sonar.host.url="sonar-host" \
  /d:sonar.login="sonar-token" \
  /d:sonar.cs.opencover.reportsPaths=/coverage.opencover.xml

COPY my-project/*.csproj ./my-project/

COPY . .
WORKDIR /src/my-project
RUN dotnet restore
RUN dotnet build -c Release -o /app/build --no-restore
RUN dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput="/coverage"

## Stop scanner
RUN dotnet sonarscanner end /d:sonar.login="sonar-token"

FROM build AS publish
WORKDIR /src/my-project
RUN dotnet publish -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "my-project.dll"]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似狗非友 2025-02-14 12:18:31

找到以下内容:

开始,构建和结束阶段都需要从
相同的目录。看起来您的构建脚本正在改变
构建和末端之间的目录。

请注意Dockerfile中的WorkDir语句,并确保所有这3个命令在同一目录中执行。

Found this: https://community.sonarsource.com/t/sonarqube-is-not-showing-msbuild-analysis/44455/7

This is what did the trick for me:

the begin, build and end stages all need to be run from the
same directory. It looks like your build script is changing the
directory between build and end.

Be careful with the WORKDIR statements in your Dockerfile and ensure all those 3 commands are executed within the same directory.

暗喜 2025-02-14 12:18:31

我相信您的问题可能正在运行dotnet Restore sonarscanner begin。如果您重新安排这些步骤,则可以。

I believe your issue may be running dotnet restore after the sonarscanner begin. If you re-arrange those steps, you should be OK.

神爱温柔 2025-02-14 12:18:31

通过在本地安装dotnet-sonarscanner来工作。在Dockerfile中使用以下命令

RUN dotnet new tool-manifest --name <name>
RUN dotnet tool install --local dotnet-sonarscanner --version 5.14.0

Worked by installing dotnet-sonarscanner locally. Use the below command in the dockerfile

RUN dotnet new tool-manifest --name <name>
RUN dotnet tool install --local dotnet-sonarscanner --version 5.14.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文