I started learning azure devops pipeline trying to Deploy Docker Image into AKS cluster using Azure Release Pipelines ex: simple python app,create docker image and deploy docker image into AKS cluster with dynamic user input parameters but image parameter value from yml script in the pipeline the value is not getting picked. Assist the with any doc's or syntax if i missed here Thanks in advance!

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
1

There are 1 best solutions below

0
wade zhou - MSFT On

The issue is caused by that the '\${{ parameters.containerRegistry }}' expression is not correct in your powershell task.

To replace the image string in azure-aks.yaml within powershell task, i fixed the task with below:

          - task: PowerShell@2
            inputs:
              targetType: 'inline'
              script: |
                $aksYamlPath = "/home/vsts/work/1/drop/azure-aks.yaml"
                $containerRegistry = "${{ parameters.containerRegistry }}"           # remove .azurecr.io as it alreadys exsits in your azure-aks.yaml
                $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'

I added cat $aksYamlPath at the end to check the yaml content, my pipeline result:

enter image description here