Azure管道未将标签应用于Docker构建容器

发布于 2025-01-18 19:32:55 字数 2696 浏览 0 评论 0原文

正如标题所说,我正在尝试将标签应用于我的Docker容器,以便我可以在管道中进一步提及所述容器。我要追求的最终结果是能够从容器中复制测试结果并发布并显示这些结果。

azure-pipeline.yml

# Docker
# Build a Docker image
# https://learn.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- main
- develop

resources:
- repo: self

variables:
  tag: '$(Build.BuildId)'

stages:
- stage: Build
  displayName: Build image
  jobs:
  - job: Build
    displayName: Build
    pool:
      name: default
    steps:
    - task: Docker@2
      displayName: Build an image
      inputs:
        command: build
        arguments: '--build-arg BuildId=$(Build.BuildId)'
        dockerfile: '$(Build.SourcesDirectory)/Dockerfile'
        tags: |
          $(tag)
    - powershell: |
        $id=docker images --filter "label=test=$(Build.BuildId)" -q | Select-Object -First 1
        docker create --name testcontainer $id
        docker cp testcontainer:/testresults ./testresults
        docker rm testcontainer
      displayName: 'Copy test results'
    - task: PublishTestResults@2
      displayName: 'Publish test results'
      inputs:
        testResultsFormat: 'xUnit'
        testResultsFiles: '**/*.trx'
        searchFolder: '$(System.DefaultWorkingDirectory)/testresults'

Dockerfile

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

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

FROM mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim-amd64 AS build
WORKDIR /src
COPY ["Forecast/Forecast.csproj", "Forecast/"]
WORKDIR "/src/Forecast"
RUN dotnet restore "Forecast.csproj"
COPY Forecast/ .
RUN dotnet build "Forecast.csproj" -c Release -o /app/build

FROM build AS publish
WORKDIR /src
RUN dotnet publish "Forecast/Forecast.csproj" --no-restore -c Release -o /app/publish

FROM build as test
ARG BuildId
LABEL test=${BuildId}
WORKDIR /src
COPY ["ForecastXUnitTest/ForecastXUnitTest.csproj", "ForecastXUnitTest/"]
WORKDIR "/src/ForecastXUnitTest"

RUN dotnet restore "ForecastXUnitTest.csproj"
COPY ForecastXUnitTest/ .
RUN dotnet build "ForecastXUnitTest.csproj" -c Release -o /app
RUN dotnet test -c Release --results-directory /testresults --logger "trx;LogFileName=test_results.trx" "ForecastXUnitTest.csproj"

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

I can see that the label hasn't been applied upon inspection of the build steps. powerShell专门针对行docker create -name testContainer $ id变量$ id是空的,并且告诉我标签永远不会申请了,所以我无法进一步走。

As the title says, I am trying to apply a label to my docker container so I can then reference said container in a further step in my pipeline. My end result that I am going for is to be able to copy my test results out of the container and publish and display those results.

azure-pipeline.yml

# Docker
# Build a Docker image
# https://learn.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- main
- develop

resources:
- repo: self

variables:
  tag: '$(Build.BuildId)'

stages:
- stage: Build
  displayName: Build image
  jobs:
  - job: Build
    displayName: Build
    pool:
      name: default
    steps:
    - task: Docker@2
      displayName: Build an image
      inputs:
        command: build
        arguments: '--build-arg BuildId=$(Build.BuildId)'
        dockerfile: '$(Build.SourcesDirectory)/Dockerfile'
        tags: |
          $(tag)
    - powershell: |
        $id=docker images --filter "label=test=$(Build.BuildId)" -q | Select-Object -First 1
        docker create --name testcontainer $id
        docker cp testcontainer:/testresults ./testresults
        docker rm testcontainer
      displayName: 'Copy test results'
    - task: PublishTestResults@2
      displayName: 'Publish test results'
      inputs:
        testResultsFormat: 'xUnit'
        testResultsFiles: '**/*.trx'
        searchFolder: '$(System.DefaultWorkingDirectory)/testresults'

Dockerfile

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

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

FROM mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim-amd64 AS build
WORKDIR /src
COPY ["Forecast/Forecast.csproj", "Forecast/"]
WORKDIR "/src/Forecast"
RUN dotnet restore "Forecast.csproj"
COPY Forecast/ .
RUN dotnet build "Forecast.csproj" -c Release -o /app/build

FROM build AS publish
WORKDIR /src
RUN dotnet publish "Forecast/Forecast.csproj" --no-restore -c Release -o /app/publish

FROM build as test
ARG BuildId
LABEL test=${BuildId}
WORKDIR /src
COPY ["ForecastXUnitTest/ForecastXUnitTest.csproj", "ForecastXUnitTest/"]
WORKDIR "/src/ForecastXUnitTest"

RUN dotnet restore "ForecastXUnitTest.csproj"
COPY ForecastXUnitTest/ .
RUN dotnet build "ForecastXUnitTest.csproj" -c Release -o /app
RUN dotnet test -c Release --results-directory /testresults --logger "trx;LogFileName=test_results.trx" "ForecastXUnitTest.csproj"

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

I can see that the label hasn't been applied upon inspection of the build steps. The powershell step specifically the line docker create --name testcontainer $id the variable $id is empty and is telling me that the label is never applied so I'm not able to go any further.

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

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

发布评论

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

评论(2

方圜几里 2025-01-25 19:32:55

在启用dockerbuildkit参数中的templates/steps/build-docker.yml@templates中的参数之后,我经历了同一问题。如建议在这里测试到buildArguments参数。

I experienced the same issue after enabling dockerBuildKit parameter in a Templates/Steps/build-docker.yml@templates. Solved as suggested here, by adding --target=test to the BuildArguments parameter.

云淡风轻 2025-01-25 19:32:55

我弄清楚是什么原因引起了我的问题。 Docker在构建和做事时最终以Cache'd Ephemeral容器图像。

有了这些,Docker不再运行这些步骤,因此在我在步骤中添加的途中,但随着构建之前的内容,我从后续的呼叫中没有得到任何东西。

最重要的是,我是指从中使用指令的先前图像,该图像阻止了我的目录被发现。

I figured out what was causing my issue. Docker ends up with cache'd ephemeral container images when building and doing its thing.

With these in place Docker no longer runs those steps so along the way I added in the steps but with what had preceded in builds I wasn't getting anything from subsequent calls.

On top of this I had been referring to a previous image with the directive FROM that was preventing my directory from being found.

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