Problem
In my GitLab pipeline, I often have a build
job that builds a Docker container and pushes it to the GitLab container registry. Importantly, I like to tag the docker images using the branch name so only one image exists per branch.
docker build -t registry.gitlab.com/gitlabProject/projectName:$CI_COMMIT_REF_SLUG .
docker push registry.gitlab.com/gitlabProject/projectName:$CI_COMMIT_REF_SLUG
Later jobs, such as test
use the image built in the build
job to perform their actions. E.g.,
image: registry.gitlab.com/gitlabProject/projectName:$CI_COMMIT_REF_SLUG
Unfortunately, it seems that GitLab caches images. When a pipeline is rerun and a new build
job pushes a new image, the test
job still uses an older, cached image instead of the newest pushed image created by the latest build
job.
Is there a way to prevent GitLab from using cached Docker images and force it to always pull the correct image from the container registry?
Non-ideal solution
A non-ideal solution is to tag the image with the commit SHA in the build
job rather than the branch name. That way each build
has a unique image and subsequent jobs always have only one possible image to use. However, this litters the container registry with many many images and the tag names provide no reference, requiring users who may want to pull specific images to cross-reference with the branch’s commits.