Skip to content

Inject git commit hash and message as Docker build args #4006

@nurikk

Description

@nurikk

Summary

During Docker image builds, automatically inject the last git commit hash and message as build args (DOKPLOY_COMMIT_HASH and DOKPLOY_COMMIT_MESSAGE), so users can expose them as runtime environment variables in their Dockerfiles.

Motivation

Currently there's no way to access git commit information at runtime inside deployed containers. This is useful for:

  • Displaying version/commit info in application UIs or health endpoints
  • Debugging which exact commit is running in a given deployment
  • Logging and observability

Proposed Solution

After cloning the repo and before running docker build, extract the commit info via git commands and pass them as --build-arg:

DOKPLOY_COMMIT_HASH=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
DOKPLOY_COMMIT_MESSAGE=$(git log -1 --pretty=%s 2>/dev/null || echo "unknown")

docker build ... --build-arg DOKPLOY_COMMIT_HASH="$DOKPLOY_COMMIT_HASH" --build-arg DOKPLOY_COMMIT_MESSAGE="$DOKPLOY_COMMIT_MESSAGE"

Users would then add to their Dockerfile:

ARG DOKPLOY_COMMIT_HASH
ARG DOKPLOY_COMMIT_MESSAGE
ENV DOKPLOY_COMMIT_HASH=$DOKPLOY_COMMIT_HASH
ENV DOKPLOY_COMMIT_MESSAGE=$DOKPLOY_COMMIT_MESSAGE

This should be applied to all relevant builders (dockerfile, railpack, etc.).


I'm going to work on this and submit a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions