Skip to content

Commit 27530d4

Browse files
committed
Dockerfile: check .git directory available in build context
Provide a better error and a hint if building without a .git directory. This can happen when building directly from Git with skipping adding .git to build context. Signed-off-by: Tonis Tiigi <[email protected]>
1 parent add7328 commit 27530d4

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Dockerfile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,16 @@ ENV GOFLAGS=-mod=vendor
6868
# scan the version/revision info
6969
FROM buildkit-base AS buildkit-version
7070
# TODO: PKG should be inferred from go modules
71-
RUN --mount=target=. \
72-
PKG=github.com/moby/buildkit VERSION=$(git describe --match 'v[0-9]*' --dirty='.m' --always --tags) REVISION=$(git rev-parse HEAD)$(if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi); \
73-
echo "-X ${PKG}/version.Version=${VERSION} -X ${PKG}/version.Revision=${REVISION} -X ${PKG}/version.Package=${PKG}" | tee /tmp/.ldflags; \
74-
echo -n "${VERSION}" | tee /tmp/.version;
71+
RUN --mount=target=. <<'EOT'
72+
git rev-parse HEAD 2>/dev/null || {
73+
echo >&2 "Failed to get git revision, make sure --build-arg BUILDKIT_CONTEXT_KEEP_GIT_DIR=1 is set when building from Git directly"
74+
exit 1
75+
}
76+
set -ex
77+
export PKG=github.com/moby/buildkit VERSION=$(git describe --match 'v[0-9]*' --dirty='.m' --always --tags) REVISION=$(git rev-parse HEAD)$(if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi);
78+
echo "-X ${PKG}/version.Version=${VERSION} -X ${PKG}/version.Revision=${REVISION} -X ${PKG}/version.Package=${PKG}" > /tmp/.ldflags;
79+
echo -n "${VERSION}" > /tmp/.version;
80+
EOT
7581

7682
# build buildctl binary
7783
FROM buildkit-base AS buildctl

0 commit comments

Comments
 (0)