【问题标题】:Azure Devops Pipeline Docker BuildAzure Devops 管道 Docker 构建
【发布时间】:2022-11-24 14:50:21
【问题描述】:
我的问题是如何根据拉取请求/分支在具有不同环境的构建管道中构建一个 docker。我使用 .net6 和 Key Vault + 变量组。据我所知,当 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
【问题讨论】:
标签:
docker
azure-devops
.net-6.0
azure-keyvault
cicd
【解决方案1】:
您可以在 YAML 管道中 conditionally assign value to the variable。
例如,假设有 4 个 Docker 注册表服务连接到 4 个不同的 Docker 注册表(Registry01,Registry02,Registry03和Registry04), 和 3 个分支 (main,test和development) 在 Git 存储库中。
目标是:
- 构建运行时
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
对于其他输入参数的值(repository,Dockerfile,tags, etc..) 在 Docker 任务上,您可以使用相同的方法有条件地分配它们的值。