Possible to do something in a job to calculate a docker tag, and execute subsequent jobs inside docker image with that tag?

The idea is that I would like to have a docker image which represents the build time environment. The image depends on a directory in my repo. If the directory does not change, the build time environment should be the exact same. I hashed the contents of this directory, built a docker image, and tagged it with that hash. Then my subsequent jobs where my application is built and tested will run inside an container from this image.

I’d like to automate this in a such a way that the first job in my workflow hashes the directory and checks a docker registry to see if an image with such a tag exists. If it does not, then it builds and pushes it.

If it does, it should proceed to the next set of jobs in the workflow.

The one part I’m not sure about is how to capture the hash in the first job and use it in my circle config to configure subsequent jobs to use that exact docker image.