【问题标题】:Azure Pipline Deploy to Kubernetes: Override image referenceAzure Pipeline 部署到 Kubernetes:覆盖映像参考
【发布时间】:2020-11-14 10:37:44
【问题描述】:

我有一个带有一个容器的 deployment.yml 文件,如下所示:

apiVersion: apps/v1
kind: Deployment
[...]
    spec:
      containers:
        - name: my-app
          image: acr/my-app:1.0 #-> should be completly overridden!

还有一个 Azure DevOps 作业,它会构建/推送并部署我的应用程序:

variables:
  imageRepository: 'my-app'
  containerRegistry: 'acr'
  dockerfilePath: '$(Build.SourcesDirectory)/myapp/Dockerfile'
  tag: '$(Build.BuildId)'

[...]

- task: KubernetesManifest@0
      displayName: Deploy on kubernetes cluster
      inputs:
        action: 'deploy'
        kubernetesServiceConnection: '...'
        namespace: 'default'
        manifests: '$(Build.SourcesDirectory)/deployment.yml'
        containers: |
          $(containerRegistry)/$(imageRepository):$(tag)

我想覆盖 deployment.yml 中的完整图像参考值。但我不知道如何编写一个工作占位符。我尝试了很多不同的东西,从 $(VALUE)VALUE 到 ``{{ VALUE }} ,例如:

image: ${{ variables.containerRegistry }} / ${{ variables.imageRepository }} : ${{ variables.tag}}。但是占位符根本没有被替换,并且 pod 失败并显示无效的图像引用。

Azure DevOps 可以做到这一点,还是我需要通过 bash 任务或类似的方式覆盖图像值?

更新

澄清我的要求: 我想覆盖完整的字符串,因为我想在多个阶段重用文件,其中只有注册表标签是困难的。例如:acr1/my-app:tag1acr2/my-app:tag2

【问题讨论】:

    标签: azure kubernetes azure-devops azure-aks


    【解决方案1】:

    取自https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/kubernetes-manifest?view=azure-devops

    The fully qualified URL of the image to be used for substitutions on the manifest files. This input accepts the specification of multiple artifact substitutions in newline-separated form. Here's an example:
    
    containers: |
      contosodemo.azurecr.io/foo:test1
      contosodemo.azurecr.io/bar:test2
    
    In this example, all references to contosodemo.azurecr.io/foo and contosodemo.azurecr.io/bar are searched for in the image field of the input manifest files. For each match found, the tag test1 or test2 replaces the matched reference.
    

    您需要确保清单中的字符串与 KubernetesManifestTask 中的 registry/image:tag 字符串匹配

    Azure DevOps 填充匹配的注册表/映像并更新标记部分

    【讨论】:

    • 谢谢,我试过了,但只有在 yml 中有 image: registry/image:tag 时才有效。但我想覆盖完整的字符串,因为我想在多个阶段重用文件,只有注册表标签是困难的。例如:acr1/my-app:tag1acr2/my-app:tag2
    • 在这种情况下,您需要使用 kustomize 或 helm2 烘焙清单并使用烘焙操作docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/… 这将允许您在将清单传递给 KubernetesManifest 之前模板化清单并更改图像任务
    • 好的,我希望我可以绕过这个额外的步骤。还是谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 2023-03-10
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多