0

基于 ADO 管道文档和几个 SO 问题/答案,应该可以在一个步骤中构建图像并在另一个步骤中将其推送到 ACR。

我遇到了“***/”-prefix 问题,这是在构建步骤中获取 ACR URI 前缀的图像名称,链接在这个SO 答案(和其他)中。

我应该处理这个,但我仍然收到推送作业找不到图像的错误。

下面是我的管道的简短版本(旁注:我需要将图像发布为人工制品并在以后的多个阶段下载它。我不想多次构建它)。

  • 构建步骤有效
  • 保存图像步骤检测带有“myacrregistry.azurecr.io”前缀的图像
  • 发布人工制品步骤有效
  • 下一阶段的加载图像步骤有效
  • Push image 步骤失败,输出如下:
/usr/bin/docker images
/usr/bin/docker push ***/myclient:latest
REPOSITORY                                                    TAG         IMAGE ID       CREATED          SIZE
***/myclient                                                  latest      cb770a5b04ec   50 seconds ago   130MB
ubuntu                                                        20.04       d13c942271d6   13 ... // removed lines
The push refers to repository [***/myclient]
An image does not exist locally with the tag: ***/myclient
##[error]An image does not exist locally with the tag: ***/myclient
##[error]The process '/usr/bin/docker' failed with exit code 1

我试过这个

  • Docker@2 构建任务(如下)
  • 带有运行 docker build 的脚本的 Bash@3 任务...不使用 ACR_URL 为图像添加前缀
  • 带有运行 docker build ... 的脚本的 Bash@3 任务,并在图像前面加上 ACR_URL
  • Docker@1 构建任务手动指定镜像前缀
  • 使用 myacrregistry.azurecr.io 字符串作为前缀
  • 使用服务连接资源 ID 作为前缀(在许多 SO 帖子之一中看到)
  • 使用服务连接名称作为前缀

一切都是一样的结果:保存图像找到图像没有问题,加载图像加载正常,即使我可以在推送(尝试)之前在图像列表中看到图像,推送也会失败。

我注意到,对于推送任务,我没有在图像名称中指定前缀(尝试过,不起作用),因此 Docker@2 推送任务必须假定前缀是某个字符串。也许该字符串不是我在构建图像时提供的 ACR_URI?不幸的是,我不知道如何查看推送步骤中“***”背后的内容。

有任何想法吗?

编辑:如果构建步骤和推送步骤处于同一阶段,则下面的管道有效。(保存、发布、加载当然是多余的)

管道 YAML:

stages:
  - stage: BuildAndTest
    jobs:
    - job: BuildImageAndRunTestsJob
      steps:
        - task: Docker@2
          inputs:
              command: build
              repository: $(imageRepository)
              containerRegistry: $(dockerRegistryServiceConnectionTest)
              dockerfile: '$(Build.SourcesDirectory)/PROJECT_FOLDER/Dockerfile'
              buildContext: '$(Build.SourcesDirectory)'
              tags: $(dockerImageTag)
              arguments: '--progress=plain' # Print output of dockerfile commands to pipeline shell 
        - task: Docker@0
          displayName: Save docker image
          inputs:
            containerRegistryType: 'Azure Container Registry'
            action: 'Run a Docker command'
            customCommand: 'image save myacrregistry.azurecr.io/$(imageRepository):$(dockerImageTag) -o $(Build.ArtifactStagingDirectory)/client-image.tar'
        # Publish the docker image artifact as output of this stage
        - publish: $(Build.ArtifactStagingDirectory)
          artifact: docker-images

  - stage: BuildAndPushImageToACR_Develop
    dependsOn: BuildAndTest
    condition: and(succeeded('BuildAndTest'), in(variables['Build.SourceBranchName'], 'develop'))
    jobs:
    - job: LoadImageAndPushJob
      steps: 
        # Download the docker image artifact to use in this stage
        - download: current
          artifact: docker-images
        # Load the docker image from file
        - task: Docker@0
          displayName: Load docker image
          inputs:
            containerRegistryType: 'Azure Container Registry'
            action: 'Run a Docker command'
            customCommand: 'load --input $(Pipeline.Workspace)/docker-images/client-image.tar'
        # Push the image to ACR
        - task: Docker@2
          displayName: Docker push image
          inputs:
            containerRegistry: $(dockerRegistryServiceConnectionTest)
            repository: $(imageRepository)
            command: push
            tags: $(dockerImageTag)
4

0 回答 0