passing $SOURCE_COMMIT to Dockerfile commands on docker hub

I am building docker image for a service at During compilation, the commit hash of the source is passed as an argument to CMake (so that it can be embedded in the version information). According to Advanced options for Autobuild and Autotest, the commit hash is automatically available as SOURCE_COMMIT environment variable.

In order to call cmake with that information, it is going as follows — I am not sure this labyrinth is the most straightforward way:

1. hooks/build

This takes care of actually passing the variable to docker build (taken from this example):


2. Dockerfile

The value is grabbed using those two lines:

ARG SOURCE_COMMIT                  # get it from the --build-arg
ENV SOURCE_COMMIT $SOURCE_COMMIT   # set shell variable (of the same name)

and finally is passed to cmake in


In summary, an environment variable of the builder shell is passed as build argument, then obtained and assigned to inner shell environment variable which is then expanded and passed as cmake argument.

Did I miss something or does it have to be this complex?

Source: StackOverflow