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

Azure pipeline not applying label to Docker build container

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

azure-pipeline.yml

# Docker
# Build a Docker image
# https://docs.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"]

我可以看到在检查构建步骤时没有应用标签。 powershell 步骤特别是 docker create --name testcontainer $id 行,变量 $id 是空的,它告诉我标签从未应用过,所以我无法继续。

我最终弄清楚了我的问题出在哪里。我最终得到了缓存的临时容器图像。有了这些,Docker 不再运行这些步骤,所以在我添加步骤的过程中,但是对于构建之前的内容,我没有从后续调用中得到任何东西。

除此之外,我还引用了之前带有指令 FROM 的图像,该图像阻止了我的目录被找到。