auto deployment of newly build docker image

5/5/2020

Every week there is a new release, and all-new docker images drop to a specific folder. My job is to do edit the Kubernetes deployments of those components, and if any new release has been done on that component, update the image name in the deployment. I want to do it automatically using a shell script, so I use the following command to copy all docker images which are saved in .tar.bz2 format with a specific tag in another directory:

find . -type f -name "*tag*tar.bz2" -mtime -1 -exec cp {} [destination dir] \;

My problems are

  1. finding the exact filename to use with docker load command
  2. updating the correct component with a tag from the .tar.bz2 file

I want to use an "if" statement to do it like below:

#! /bin/sh

 word = myword

 if [ $word = myword ]
 then 
   docker load < 
   kubectl delete svc,deployment myword -n namespace
   kubectl create -f /home/ubuntu/dockerimage/yaml/myword.yaml
fi

word = myword

 if [ $word = myword1 ]
 then 
   docker load < 
   kubectl delete svc,deployment myword1 -n namespace
   kubectl create -f /home/ubuntu/dockerimage/yaml/myword1.yaml
fi 

This script should do the docker load from the .tar.bz2 file, and as per the tag the exact image should be deployed under the exact component.

-- Niladri Dey
kubernetes
shell

1 Answer

5/6/2020

Whatever the reason you can't directly pull images from docker registry server in your case, your script can be improved a lot.

#! /bin/sh

if [ $# -lt 1 ]; then
    echo "Usage: $0 <name>"
    exit 1
fi

word = $1

docker load < 
kubectl delete svc,deployment ${word} -n namespace
kubectl create -f /home/ubuntu/dockerimage/yaml/${word}.yaml

you can add some conditions to check if the file /home/ubuntu/dockerimage/yaml/${word}.yaml is exist and if the service and deployment of ${word} is exist before delete and create it.

-- BMW
Source: StackOverflow