How to pass parametrized image tag in the deployment yaml file?

8/12/2021

My deployment yaml file is bitbucket and CI/CD pipeline to build the image and deploy the container is in Azure DevOps. I am able to build and tag the image correctly i.e. with each pipeline run the image version is incremented by 1 e.g. 1.0.0, 1.0.1,1.0.2 ... There is no "latest" tag in this repository. How do I pass this image tag dynamically in the deployment.yml file so that kubectl deploy stage always picks the latest tagged image ?

my deployment file currently looks like:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ubuntu-custom
  labels:
    app: ubuntu-custom
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ubuntu-custom
  template:
    metadata:
      labels:
        app: ubuntu-custom
    spec:
      containers:
      - name: ubuntu-custom
        image:  <acr>/ubuntu-custom:latest
        command: ["sleep"]
        args: ["365d"]
      imagePullSecrets:
      - name: acrsecret

I have tried to change the image to - image: acr/ubuntu-custom but I get the ImagePullBackOff error.

I am sure I need to pass the latest tag in the deployment file but I am not sure how I can achieve that.

-- notageek27
azure-pipelines
devops
kubernetes

1 Answer

8/12/2021
  1. Use sed command

    sed 's/\<arc\>/${ACR_REPO_URL}/g' deploy.yaml

  2. Use yq

    yq -i -y ".spec. template.spec.containers.image = \"${ACR_REPO_URL}\"" deploy.yaml

-- T.R
Source: StackOverflow