docker Azure Devops管道停靠器生成

pinkon5k  于 2022-11-28  发布在  Docker
关注(0)|答案(1)|浏览(171)

我的问题是如何在构建管道中根据拉取请求/分支使用不同的环境构建一个Docker。我使用.net6和Key Vault + Variable Groups。据我所知,当Docker构建映像时,他使用appsetings.json。如果我不想将prod设置传递给此文件,而我想覆盖或以某种方式将变量组中的设置传递给Docker文件,或者是否有其他方法可以做到这一点?

trigger:
    - main
    - test
    - development
    stages:
    - stage: Build
      displayName: Build stage
      jobs:
      - job: Build
        displayName: Build
        pool:
          vmImage: $(vmImageName)
        steps:
        - task: Docker@2
          displayName: Build and push an image to container registry
          inputs:
            command: buildAndPush
            repository: $(imageRepository)
            dockerfile: $(dockerfilePath)
            containerRegistry: $(dockerRegistryServiceConnection)
            tags: |
              $(tag)
              latest
0ve6wy6x

0ve6wy6x1#

您可以有条件地为YAML管道中的变量赋值。
例如,假设有4个Docker注册表服务连接到4个不同的Docker注册表(Registry01Registry02Registry03Registry04),以及Git存储库中的3个分支(maintestdevelopment)。
目标是:

  • 为**main分支运行构建时,需要构建Docker映像并将其推送到Registry01**。
  • 为**test分支运行构建时,需要构建Docker映像并将其推送到Registry02**。
  • 为**development分支运行构建时,需要构建Docker映像并将其推送到Registry03**。
  • 为拉取请求运行构建时,需要构建Docker映像并将其推送到**Registry04**。

我们可以如下配置YAML管道:

trigger:
  branches:
    include:
    - main
    - test
    - development

variables:
  ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/main') }}:
    dockerRegistryServiceConnection: 'Registry01'
  ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/test') }}:
    dockerRegistryServiceConnection: 'Registry02'
  ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/development') }}:
    dockerRegistryServiceConnection: 'Registry03'
  ${{ if contains(variables['Build.SourceBranch'], 'refs/pull/') }}:
    dockerRegistryServiceConnection: 'Registry04'

stages:
- stage: Build
  displayName: 'Build stage'
  jobs:
  - job: Build
    displayName: 'Build job'
    pool:
      vmImage: windows-latest
    steps:
    - task: Docker@2
      displayName: 'Build and push an image to container registry'
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        Dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)
          latest

对于Docker任务上的其他输入参数(repositoryDockerfiletags等)的值,可以使用相同的方法有条件地分配其值。还可以有条件地分配要用于运行生成作业的代理的vmImage
比如说。

variables:
  ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/main') }}:
    vmImageName: 'windows-latest'
    dockerRegistryServiceConnection: 'Registry01'
    imageRepository: 'repository01'
    dockerfilePath: 'Dockerfile01'
    tag: 'tag01'
  ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/test') }}:
    vmImageName: 'windows-2019'
    dockerRegistryServiceConnection: 'Registry02'
    imageRepository: 'repository02'
    dockerfilePath: 'Dockerfile02'
    tag: 'tag02'
  ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/development') }}:
    vmImageName: 'ubuntu-latest'
    dockerRegistryServiceConnection: 'Registry03'
    imageRepository: 'repository03'
    dockerfilePath: 'Dockerfile03'
    tag: 'tag03'
  ${{ if contains(variables['Build.SourceBranch'], 'refs/pull/') }}:
    vmImageName: 'macOS-latest'
    dockerRegistryServiceConnection: 'Registry04'
    imageRepository: 'repository04'
    dockerfilePath: 'Dockerfile04'
    tag: 'tag04'

相关问题