azure - 如何将 docker 镜像发布为 azure devops 中的工件

标签 azure docker azure-devops azure-pipelines

我正在我的 Azure 管道中构建 Docker 镜像。现在,我想根据配置参数将此镜像推送到多个 aws 帐户(dev、stage、prod)。问题是,图像在发布工件中不可用。我遇到了thisthis我研究期间的文章。我对有关保存 docker 镜像以便可以在发布工件中使用的解决方案感到困惑。我有两个具体问题:

  1. docker 构建后,如何在 Azure 管道任务中使用 docker save 命令。可用的docker任务没有此命令。
  2. 除了保存图像之外,还有其他更好的方法吗?

最佳答案

如今,编写管道的首选方式是使用 yaml。

请参阅我的代码,了解如何使用 Artifactory docker 注册表在多级管道中执行此操作。

如果我们处于 master 状态,则 MY_ADDITIONAL_TAG 也将被推送。

trigger:
- master

resources:
- repo: self

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

stages:
- stage: Build
  displayName: Build DevOps base image
  jobs:
  - job: Build_and_Push
    steps:
    - task: Docker@2
      displayName: Build
      inputs:
        containerRegistry: ''
        repository: 'MY_REPO/IMAGE'
        command: 'build'
        Dockerfile: 'PATH_TO_MY_DOCKERFILE'
        tags: $(tag)
    - task: Bash@3
      displayName: Save Docker Image
      inputs:
        targetType: 'inline'
        script: |
          docker save MY_DOCKER_REPO_IMAGE_NAME:$(tag) -o $(Pipeline.Workspace)/MY_IMAGE_FILE.tar
    - task: PublishPipelineArtifact@1
      inputs:
        targetPath: '$(Pipeline.Workspace)/MY_IMAGE_FILE.tar'
        artifact: 'MY_ARTIFACT'
        publishLocation: 'pipeline'
- stage: Push
  displayName: Push DevOps base image
  jobs:
  - job: Push
    steps:
    - task: DownloadPipelineArtifact@2
      inputs:
        buildType: 'current'
        artifactName: 'MY_ARTIFACT'
        targetPath: '$(Pipeline.Workspace)'
    - task: Bash@3
      displayName: Load Docker Image
      inputs:
        targetType: 'inline'
        script: |      
          docker load --input $(Pipeline.Workspace)/MY_IMAGE_FILE.tar
          docker tag MY_DOCKER_REPO_IMAGE_NAME:$(tag) MY_DOCKER_REPO_IMAGE_NAME:MY_ADDITIONAL_TAG
    - task: Docker@2
      displayName: push development tags
      inputs:
        containerRegistry: 'MY_DOCKER_REGISTRY'
        repository: 'MY_REPO/IMAGE'
        command: 'push'
        tags: | 
          $(tag)
    - task: Docker@2
      condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')
      displayName: push 0-devops tag
      inputs:
        containerRegistry: 'MY_ARTIFACT'
        repository: 'MY_REPO/IMAGE'
        command: 'push'
        tags: | 
          MY_ADDITIONAL_TAG

关于azure - 如何将 docker 镜像发布为 azure devops 中的工件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66468815/

相关文章:

underscore.js - Visual Studio Online 中托管构建 Controller 上的 underscore.d.ts 中的 TypeScript 编译错误

azure - 控制 ADF 中管道的手动取消

sql-server - Azure逻辑应用程序: How do I pass a single For Each variable to an Execute Stored Procedure step?

azure - 在 Azure 逻辑应用内联代码中使用变量

postgresql - Docker dpage/pgadmin4错误:指定的用户不存在

python - 在 Docker 中运行 selenium 测试

java - Azure DevOps Pipeline 中的 Maven 构建错误

按日期的 Azure Blob 复制分区

ruby-on-rails - Azure DevOps - 自动运行 Rails 迁移

node.js - 将TFS2017任务更新到最新版本