<p>I have successfully built and pushed a docker image to Docker Hub using the Docker task using Azure Pipelines.</p>
<p>Now I would like to take that Docker image and deploy it to my Kubernetes cluster in AKS.</p>
<p>I need to update the image by using <code>kubectl apply</code>. I need to use <code>kubectl apply</code> specifically and not <code>replace</code>, <code>edit</code>, or <code>patch</code>.</p>
<p>How can I achieve that while also updating <code>spec.containers.image</code> in my deploy.yml file?</p>
<p><strong>I basically want to take this variable <code>tag: '$(Build.BuildId)'</code>, replace <code>spec.containers.image</code> in deploy.yml and then run kubectl apply -f deploy.yml.</strong></p>
<p>Here's my deploy.yml file:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: myprojectname
  namespace: my-namespace
  labels:
    app: myprojectname
spec:
  replicas: 1
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      web: myprojectname
  template:
    metadata:
      labels:
        app: myprojectname
        web: myprojectname
        type: webapp
    spec:
      containers:
        - name: myprojectname
          image: mydockerhubaccount/myprojectname:25
          resources:
            requests:
              cpu: 20m
              memory: 100Mi
            limits:
              cpu: 500m
              memory: 130Mi
          ports:
            - containerPort: 80
</code></pre>
<p>Here's my azure-pipelines.yml file:</p>
<pre><code># Docker
# Build and push an image to Azure Container Registry
# https://learn.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- master

resources:
- repo: self

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'Docker Hub'
  imageRepository: 'mydockerhubaccount/myprojectname'
  dockerfilePath: '$(Build.SourcesDirectory)/my-project-name/Dockerfile'
  tag: '$(Build.BuildId)'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build and push 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)
        buildContext: '$(Build.SourcesDirectory)'
</code></pre>

PussInBoots
<p>This might be an old question but is still relevant today. The process is mostly the same for the &quot;classic&quot; visual DevOps pipeline builder as it is for the YAML step builder.</p>
<p>Generally speaking, the way to update the container image tag with the current build (without using &quot;latest&quot; to enable rollbacks) is as follows:</p>
<ol>
<li><p>In the release pipeline make note of your Artifact <code>Source Alias</code>. The artifact being the result from your build process. If your build process doesn't publish an artifact, add the default: <code>Publish Pipeline Artifact</code> step. You can call the source alias whatever you like as long as it is unique and spaces are not important.</p>
</li>
<li><p>In your Release Pipeline's Stages for <code>Deploy to Kubernetes</code> select the correct manifest.</p>
</li>
<li><p>Then update the <code>Containers</code> field to the name of your container repo and add <code>$(Release.Artifacts.YOUR_SOURCE_ALIAS_HERE.BuildId)</code> as the tag. It should looks something like: <code>YOUR_REPO.azurecr.io/YOUR_CONTAINER_NAME:$(Release.Artifacts.YOUR_SOURCE_ALIAS_HERE.BuildId)</code></p>
</li>
<li><p>When the pipeline deploys to K8 it should be using the latest tag id from your build process. You can confirm this on your cluster by describing your pod/whatever with something like: <code>kubectl describe pod POD_NAME</code></p>
</li>
</ol>
<p><strong>Example YAML deployment step:</strong></p>
<pre><code>steps:
- task: KubernetesManifest@0
  displayName: deploy
  inputs:
    kubernetesServiceConnection: 'YOUR_CONNECTION'
    namespace: 'CLUSTER_NAMEPACE'
    manifests: '$(System.DefaultWorkingDirectory)/YOUR_SOURCE_ALIAS_HERE/Job1/s/whatever-deployment.yml'
    containers: 'YOUR_REPO.azurecr.io/YOUR_CONTAINER_NAME:$(Release.Artifacts.YOUR_SOURCE_ALIAS_HERE.BuildId)'
</code></pre>
