本文介绍了在多阶段Azure Devops中访问Docker映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Azure DevOps管道,并正在尝试使用yml文件定义的多阶段功能.

I am building an Azure DevOps pipeline and was trying out the multi-stage feature, this is defined by using a yml file.

在yml定义中,我分为两个阶段,一个阶段是使用docker-compose命令构建docker映像,第二阶段是将这些映像推送到ACR.似乎这是不可能的,因为我从第一阶段访问最新构建的图像都没有任何成功.这是一个示例yml文件

In the yml definition I have two stages, one is to build docker images using a docker-compose command, second stage is to push these images to ACR. It seems this is not possible as I haven't had any success accessing the recently built images from the first stage.Here's a sample yml file

stages:
- stage: Build
  displayName: Build image
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - publish:  $(Build.ArtifactStagingDirectory)
      artifact: docker-images
    - task: DockerCompose@0
      inputs:
        containerregistrytype: 'Azure Container Registry'
        azureSubscription: '************'
        azureContainerRegistry: '************'
        dockerComposeFile: '**/docker-compose.yml'
        action: 'Build services'
        additionalImageTags: '$(Build.BuildId)'
- stage: Push
  displayName: Push image
  jobs:
  - job: Push
    displayName: Push
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - download: current
      artifact: docker-images
    - task: DockerCompose@0
      inputs:
        containerregistrytype: 'Azure Container Registry'
        azureSubscription: '************'
        azureContainerRegistry: '************'
        dockerComposeFile: '**/docker-compose.yml'
        action: 'Push services'
        additionalImageTags: '$(Build.BuildId)'

问题是,如何访问上一阶段构建的Docker映像?它存储在哪里?我尝试从第一阶段下载 $(Build.ArtifactStagingDirectory),但似乎没有.如果我有一个阶段但有不同的工作,则同样适用.如果我要在一个阶段中同时使用构建和推送,那么效果很好,但是我想为每个阶段使用一个单独的阶段.

The question is, how do I access docker images that was built in my previous stage? where is it stored? I've tried by downloading $(Build.ArtifactStagingDirectory) from the first stage but it didn't seem to have it. The same applies if I have one stage but separate jobs.If I would to use both build and push in one stage it works fine, but I want to have a separate stages for each.

推荐答案

首先,您应该始终在阶段结束时放置发布工件"任务.否则,您将只发布一个空文件夹.

First of all you should always put publish artifacts task at the end of the stage. Or you will just publish an empty folder.

第二,dock compose命令将生成映像并将其保存在托管计算机上的docker文件夹中.什么都不会输出到代理的工件文件夹 $(Build.ArtifactStagingDirectory).

Second, the dock compose command build and save the image in the docker folder on the hosted machine. Nothing will be output to the artifacts folder $(Build.ArtifactStagingDirectory) of the agent.

作为在阶段之间传递Docker映像的一种解决方法,您可以使用 docker image save 命令专门将映像保存在文件夹 $(Build.ArtifactStagingDirectory)中.并使用发布工件任务将图像发布到Azure Devops服务器.然后,您可以在下一个阶段使用下载工件下载图像.

As a workaround to pass the docker image between stages, you can use docker image save command to specifically save the image in folder $(Build.ArtifactStagingDirectory). And use publish artifacts task to publish the image to azure devops server. Then you can use download artifacts to download the image in the next stage.

您可以检查以下示例:

1,在Build Stage中,在 DockerCompose 任务后添加 Docker @ 0 (版本0. *)以运行图像保存命令,以将图像保存到文件夹$(Build.ArtifactStagingDirectory)

1, In Build Stage add a Docker@0(version 0.*) after DockerCompose task to run image save command to save the image to folder $(Build.ArtifactStagingDirectory)

- task: Docker@0
  displayName: 'Run a Docker command'
  inputs:
    containerregistrytype: 'Azure Container Registry'
    azureSubscription: '************'
    azureContainerRegistry: '************'
    action: 'Run a Docker command'
    customCommand: 'image save <imageName>:$(Build.BuildId) -o $(Build.ArtifactStagingDirectory)/imagetest.tar'

2,将发布工件任务放在Build阶段的末尾以发布图像

2, Put the publish artifact task at the end of Build stage to publish the image

- publish:  $(Build.ArtifactStagingDirectory)
  artifact: docker-images

3,现在您可以将图像存档文件从Build阶段下载到Publish阶段.而且,您可以运行 docker load 命令来加载存档映像.加载后,您可以将其推送到ACR

3, Now you can download the image archive file from Build stage to Publish stage. And you can run docker load command to load the archive image. After it is loaded, you can then push it to ACR

- download: current
  artifact: docker-images

- task: Docker@0
  displayName: 'Run a Docker command'
  inputs:
    containerregistrytype: 'Azure Container Registry'
    azureSubscription: '************'
    azureContainerRegistry: '************'
    action: 'Run a Docker command'
    customCommand: 'load --input $(Pipeline.Workspace)/docker-images/imagetest.tar'

希望上面有帮助!

这篇关于在多阶段Azure Devops中访问Docker映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:55