Set docker image tag in yaml

Hi

I would like to know if it is possible to set the docker image tag in my deployment yaml dynamically?

Current in my deployment yaml i have the image latest

kind: Deployment
spec:
containers:
image: ‘registry/isa/app:latest’

This yaml is kept in a git repo which we checkout and deploy into our cluster. What I would like to do is to deploy a tag which we get as part of out build process. however since the tag is in a yaml file I cannot change the version dynamically unless I edit the yaml with each deploy which is dirty (E.g using sed).

Is there a better of doing this?

Is there a way to overwrite latest

If your deployment itself is not changing, you can use kubectl set image command

kubectl set image deployment <my-deployment> <container-name>=<image-name>:<tag>
kubectl set image deployment webserver nginx=nginx:v1.15

For more info, see the the Updating a Deployment Docs.

I use my git revision as the image version and use envsubst to perform the variable replacement. It’s clean & quick.

Here is an example Makefile:

VERSION  ?= $(shell git rev-parse HEAD)
NS       ?= default

install:

    envsubst < deployment.yaml | kubectl -n $$NS apply -f -

Then in my deployment.yaml I just use a variable:

apiVersion: apps/v1
kind:       Deployment
metadata:
  name: k8-byexamples-spring-rest
spec:
  selector:
    matchLabels:
      app: k8-byexamples-spring-rest
  template:
    metadata:
      labels:
        app: k8-byexamples-spring-rest
    spec:
      containers:
        - name:  k8-byexamples-spring-rest
          image: gcr.io/matthewdavis-byexamples/k8-byexamples-spring-rest:$VERSION
          ports:
            - containerPort: 8080
              name: http
          readinessProbe:
            httpGet:
              path: /test/is_ready
              scheme: http
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 5
          livenessProbe:
            httpGet:
              path: /test/is_alive
              scheme: http
              port: 8080
            initialDelaySeconds: 15
            periodSeconds: 15
1 Like

Hello ISA, have you resolved this issue. If yes, can you please post the resolution steps here.
Image tag is creating using build number of CI pipeline and needs to replace this everytime in deployment file and running the CD pipeline by passing this deployment file into it.