使用 Azure Release Pipelines 以及 clustername、acr、resourcegroup 等参数将 Docker 映像部署到 AKS 集群

问题描述 投票:0回答:1

我开始学习azure devops管道,尝试使用Azure Release Pipelines将Docker镜像部署到AKS集群中,例如:简单的python应用程序,创建docker镜像并使用动态用户输入参数将docker镜像部署到AKS集群中但图像参数值来自yml脚本中管道的值没有被选择。如果我错过了这里,请协助提供任何文档或语法,提前致谢!

parameters:
- name: 'containerRegistry'
  displayName: 'Azure Container Registry'
  type: object
 
- name: 'ResourceGroup'
  displayName: 'Azure ResourceGroup'
  type: object

- name: 'Cluster'
  displayName: 'Azure Cluster'
  type: object

pool:
  name: Azure Pipelines

stages:
- stage: Build
  displayName: Build image
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: ubuntu-latest
    steps:
    - task: Docker@0
      displayName: 'Build an image'
      inputs:
        #command: build
        azureSubscription: 'TEST'
        azureContainerRegistry: ${{ parameters.containerRegistry }}.azurecr.io
        action: 'Build an image'
        dockerfile: '$(Build.SourcesDirectory)/Dockerfile'
        includeLatestTag: true

    - task: Docker@0
      displayName: 'Push an image'
      inputs:
        azureSubscription: 'TEST'
        azureContainerRegistry: ${{ parameters.containerRegistry }}.azurecr.io
        action: 'Push an image'
        includeLatestTag: true

    - task: CopyFiles@2
      displayName: 'Copy Files to: $(build.artifactstagingdirectory)'
      inputs:
        Contents: 'azure-aks.yaml'
        TargetFolder: '$(build.artifactstagingdirectory)'
    
    - task: PublishBuildArtifacts@1
      displayName: 'Publish Artifact: drop'

- stage: DeployToKubernetes
  displayName: 'Deploy to Kubernetes'
  dependsOn: Build
  jobs:
  - deployment: Deploy
    displayName: 'Release pipeline'
    environment: 'dev'
    strategy:
      runOnce:
        deploy:
          steps:
          - download: current
            artifact: 'drop'
            displayName: 'Download Artifact'

          - task: PowerShell@2
            inputs:
              targetType: 'inline'
              script: |
                $aksYamlPath = "/home/vsts/work/1/drop/azure-aks.yaml"
                $containerRegistry = "${{ parameters.containerRegistry }}.azurecr.io"
                $content = Get-Content -Path $aksYamlPath -Raw
                $content = $content -replace '\${{ parameters.containerRegistry }}', $containerRegistry
                Set-Content -Path $aksYamlPath -Value $content
            displayName: 'Replace Container Registry in YAML


          - task: Kubernetes@1
            displayName: 'Create Deployments & Services in AKS'
            inputs:
              connectionType: 'Azure Resource Manager'
              azureSubscriptionEndpoint: 'TEST'
              azureResourceGroup: ${{ parameters.ResourceGroup }}
              kubernetesCluster: ${{ parameters.Cluster }}
              useClusterAdmin: true
              namespace: default
              command: apply
              useConfigurationFile: true
              configuration:  '/home/vsts/work/1/drop/azure-aks.yaml' 
              azureContainerRegistry: ${{ parameters.containerRegistry }}
              checkLatest: true 
                
azure-aks.yaml
--------------
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-python-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-python-app
  template:
    metadata:
      labels:
        app: my-python-app
    spec:
      containers:
      - name: dockerfile
        image: ${{ parameters.containerRegistry }}.azurecr.io/dockerfile:latest  ## value for image is not getting populated from pipeline
        ports:
        - containerPort: 5000       
---
apiVersion: v1
kind: Service
metadata:
  name: my-python-app-svc
spec:
  selector:
    app: my-python-app
  ports:
    - protocol: TCP
      port: 5000
      targetPort: 5000
  type: LoadBalancer
azure-devops yaml azure-pipelines azure-aks azure-pipelines-release-pipeline
1个回答
0
投票

该问题是由于您的 powershell 任务中的

'\${{ parameters.containerRegistry }}'
表达式不正确引起的。

要替换 powershell 任务中

azure-aks.yaml
中的图像字符串,我用以下方法修复了该任务:

          - task: PowerShell@2
            inputs:
              targetType: 'inline'
              script: |
                $aksYamlPath = "/home/vsts/work/1/drop/azure-aks.yaml"
                $containerRegistry = "${{ parameters.containerRegistry }}.azurecr.io"
                $content = Get-Content -Path $aksYamlPath -Raw
                $content = $content -replace '\$\{\{ parameters\.containerRegistry \}\}', $containerRegistry        # fix the expression
                Set-Content -Path $aksYamlPath -Value $content
                cat $aksYamlPath
            displayName: 'Replace Container Registry in YAML'

我在最后添加了

cat $aksYamlPath
来检查yaml内容,我的管道结果:

© www.soinside.com 2019 - 2024. All rights reserved.