Inject the current git revision's SHA-1 as an environment variable (e.g. STACK_GIT_REVISION)
It's useful for many web applications to show the current git revision, especially in development environments.
Right now it's possible to do this through hackery -- e.g., a post-deploy task that writes a file containing the git revision SHA-1 which is then later read into an environment variable during initialization. However, that's a little messy, and every customer would have to write the same code to make that work.
Since that would lead to a lot of duplicated code among customers, it seems smarter to bake this into the infrastructure. There's already STACK_GIT_BRANCH
, so hopefully STACK_GIT_REVISION
is not very much effort.
For Maestro stacks this is under `CLOUD66_SERVICE_GIT_REF` and for Rails the ways to obtain this are:
1. by querying from inside the application code: ie. “git_ref = `git rev-parse HEAD`.strip”
2. by querying our metadata service https://help.cloud66.com/rails/how-to-guides/deployment/querying-server-metadata.html
-
Niklas Bichinger commented
I solved it like the following way. In my own base Dockerfile I copy the .git folder into the image, stat the git revision into two files in the root dir and remove it afterwards:
ONBUILD COPY .git /app
# eventually also copy your app source into the container here
ONBUILD RUN echo -n `git show --format="%H" --no-patch` > /GIT_REVISION \
&& echo -n `git show --format="%s (%cn at %cI)" --no-patch` > /GIT_REVISION_INFO \
&& rm -rf .gitWhenever I need to access the git revision in the container, I load the files. E.g. bash: echo $(</GIT_REVISION)