Search code examples
gitjenkinskubernetesdevopsgitops

Can't deploy Docker image to Kubernetes with Jenkins if there's no change to deployment.yml


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?


Solution

  • I finally did it with kubectl patch

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