0

I have a repo including a Dockerfile, and a github trigger, which triggers everytime there's a commit pushed to a branch. Whenever I commit any change, it creates a new docker image with tags: branch name and git hash (yes, both of them) and pushes it to registry. In my workloads, the image is described as image:branch_name. If I manually redeploy, it pulls the latest image and it works all fine. But the problem arises when I do it with Jenkins. Cause, in deployment.yml, image is imagename:branch_name, which is static all the time.

Now, I've configured a jenkins job to do kubectl apply -f deployment.yml. However, this doesn't pull the latest image everytime, since there's no change to yml file (image and branch name are same). How can I make Kubernetes to pull latest image everytime?

One idea I have is to pass the githash to deployment.yml so that there's a change pushed into the file, triggers the deployment. How can I achieve this?

PS: I already know kubectl rolling-restart and kubectl apply. I want to use above mentioned method. Is there any way?

noobanyway
  • 51
  • 7

3 Answers3

0

You can update the image of the deployment, it will automatically trigger a rollout. As you tag the image with the git hash - which is good - you can then run something like this at the end of the build:

kubectl set image deployment/my-deployment container_name=myimage:buildhash

Thomas
  • 11,272
  • 2
  • 24
  • 40
  • Is there any way to do it from inside the deployment.yml file and in jenkins just kubectl apply? Or pass the githas to deployment.yml? – noobanyway Mar 05 '21 at 18:58
  • You can call `kubectl set image` right from the Jenkinsfile - that is the point when you know the image hash name anyway. – Thomas Mar 06 '21 at 13:32
0

You can set imagePullPolcy to Always and it will always pull the latest image, or if that's insufficient there's always Flux which can watch your repository and update your manifests according to rules you specify (highest number, semantic versioning rules, etc).

Tyzbit
  • 166
  • 2
0

I finally did it with kubectl patch

kubectl patch deployment <deployment name> -p "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"build\":\"${BUILD_ID}\"}}}}}}"
noobanyway
  • 51
  • 7