Invalidate Docker cache only when Git repo is updated

I have a docker Dockerfile in which I want to clone a Git repo. If the Git repo is not updated I would like Docker to use its cache. If the repo is updated then building the Docker Image should use the Git repo updates.

Based on this stackoverflow question:

How to prevent Dockerfile caching git clone

I have added the following to my Dockerfile:

#invalidate cache if micropython is updated
ADD https://api.github.com/repos/dwjbosman/micropython/git/refs/heads/master /root/micropython_version.json

RUN git clone --recurse-submodules https://github.com/dwjbosman/micropython.git /root/micropython && 
    cd /root/micropython && 
    git submodule update

I use a makefile with targets to build the image. First I pull the previous image from docker hub:

docker pull $(REGISTRY)/$(DOCKER_IMAGE)

and then I use this pulled image as a cache for the new build:

docker build --cache-from $(DOCKER_IMAGE) -t $(DOCKER_IMAGE) 

I am building the images in a CI environment (Gitlab). If I check the logs the ADD step always invalidates the cache and the checksum changes even if no updates were done in the Git repo.

I have checked the output of “https://api.github.com/repos/dwjbosman/micropython/git/refs/heads/master“. The contents does not seem to change.

Is there some hidden property (time perhaps?) which still changes?

Source: StackOverflow