Skip to content

Instantly share code, notes, and snippets.

@songbinliu
Last active September 6, 2023 09:51
Show Gist options
  • Save songbinliu/0bad538d6b4f8da4705f59643909fd40 to your computer and use it in GitHub Desktop.
Save songbinliu/0bad538d6b4f8da4705f59643909fd40 to your computer and use it in GitHub Desktop.
Add git commit hash to docker image ENV
#!/bin/bash

echo "SOURCE_COMMIT: $SOURCE_COMMIT"

if [[ -z "$SOURCE_COMMIT" ]]; then
	export SOURCE_COMMIT="${SOURCE_COMMIT:-$(git rev-parse HEAD)}"
	echo "Updating SOURCE_COMMIT from git rev-parse HEAD"
	echo "SOURCE_COMMIT: $SOURCE_COMMIT"
fi


echo "DOCKER_TAG: $DOCKER_TAG"

docker build -t "$IMAGE_NAME" --build-arg "SOURCE_COMMIT=$SOURCE_COMMIT" --build-arg "DOCKER_TAG=$DOCKER_TAG" .

To use the variable SOURCE_COMMIT in Docker file, add

ARG SOURCE_COMMIT
LABEL git-rev="$SOURCE_COMMIT"

source: docker-issue-600

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment