Skip to content

Commit 7104719

Browse files
committed
feat: add Git and Docker metadata to build process and application response
- Introduced build arguments for Git branch, commit, and Docker tag in the Dockerfile and Makefile to enhance traceability of builds. - Updated GitHub workflows to pass Git and Docker metadata during image builds and releases. - Modified the application response to include Git and Docker information, improving visibility into the deployed version.
1 parent aad3dd1 commit 7104719

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

.github/workflows/docker-image.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ jobs:
5656
platforms: linux/amd64
5757
build-args: |
5858
BUILD_VERSION=${{ steps.version.outputs.version }}-${{ steps.version.outputs.build_id }}
59+
GIT_BRANCH=${{ github.ref_name }}
60+
GIT_COMMIT=${{ github.sha }}
61+
DOCKER_TAG=${{ steps.version.outputs.version }}-${{ steps.version.outputs.build_id }}

.github/workflows/release.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ jobs:
4242
password: ${{ secrets.GITHUB_TOKEN }}
4343
access_token: ${{ secrets.GITHUB_TOKEN }}
4444
github_token: ${{ secrets.GITHUB_TOKEN }}
45-
build-args: BUILD_VERSION={{NEW_VERSION}}
45+
build-args: |
46+
BUILD_VERSION={{NEW_VERSION}}
47+
GIT_BRANCH=${{ github.ref_name }}
48+
GIT_COMMIT=${{ github.sha }}
49+
DOCKER_TAG={{NEW_VERSION}}

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ FROM node:22-alpine AS production
1616

1717
ARG NODE_ENV=production
1818
ARG BUILD_VERSION=dev
19+
ARG GIT_BRANCH=unknown
20+
ARG GIT_COMMIT=unknown
21+
ARG DOCKER_TAG=unknown
1922
ENV NODE_ENV=${NODE_ENV}
2023
ENV BUILD_VERSION=${BUILD_VERSION}
24+
ENV GIT_BRANCH=${GIT_BRANCH}
25+
ENV GIT_COMMIT=${GIT_COMMIT}
26+
ENV DOCKER_TAG=${DOCKER_TAG}
2127
ENV ALLOW_RUNTIME_BUILD=true
2228
ENV DO_NOT_TRACK=1
2329
ENV PYTHONWARNINGS=ignore::UserWarning

Makefile

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ PLATFORM = "linux/amd64"
1515

1616
include .env
1717

18+
GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || echo unknown)
19+
GIT_COMMIT ?= $(shell git rev-parse HEAD 2>/dev/null || echo unknown)
20+
DOCKER_TAG ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo unknown)
21+
1822
CERT_DIR = ./certificates
1923
COMMON_NAME = localhost
2024
DAYS_VALID = 365
@@ -31,12 +35,20 @@ help:
3135
| sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[32m%-15s\033[0m %s\n", $$1, $$2}'
3236

3337
build: ## Build the container
34-
@docker build --platform $(PLATFORM) -t $(IMG_NAME) --no-cache --progress=plain .
38+
@docker build --platform $(PLATFORM) -t $(IMG_NAME) --no-cache --progress=plain \
39+
--build-arg BUILD_VERSION=$(DOCKER_TAG) \
40+
--build-arg GIT_BRANCH=$(GIT_BRANCH) \
41+
--build-arg GIT_COMMIT=$(GIT_COMMIT) \
42+
--build-arg DOCKER_TAG=$(DOCKER_TAG) \
43+
.
3544

3645
simulation: ## Start production environment in simulation mode
3746
@docker run --rm -it \
3847
-e NODE_ENV=production \
3948
-e NODE_TLS_REJECT_UNAUTHORIZED=0 \
49+
-e GIT_BRANCH=$(GIT_BRANCH) \
50+
-e GIT_COMMIT=$(GIT_COMMIT) \
51+
-e DOCKER_TAG=$(DOCKER_TAG) \
4052
--add-host host.docker.internal:host-gateway \
4153
--platform $(PLATFORM) \
4254
--network dev \
@@ -62,6 +74,9 @@ prod: ## Start production environment
6274
@docker run --rm -it \
6375
-e NODE_ENV=production \
6476
-e NODE_TLS_REJECT_UNAUTHORIZED=0 \
77+
-e GIT_BRANCH=$(GIT_BRANCH) \
78+
-e GIT_COMMIT=$(GIT_COMMIT) \
79+
-e DOCKER_TAG=$(DOCKER_TAG) \
6580
--add-host host.docker.internal:host-gateway \
6681
--platform $(PLATFORM) \
6782
--network dev \
@@ -79,6 +94,9 @@ dev: ## Start development environment
7994
@docker run --rm -it \
8095
-e NODE_ENV=development \
8196
-e NODE_TLS_REJECT_UNAUTHORIZED=0 \
97+
-e GIT_BRANCH=$(GIT_BRANCH) \
98+
-e GIT_COMMIT=$(GIT_COMMIT) \
99+
-e DOCKER_TAG=$(DOCKER_TAG) \
82100
--add-host host.docker.internal:host-gateway \
83101
--platform $(PLATFORM) \
84102
--network dev \
@@ -97,6 +115,9 @@ debug: ## Start debug environment
97115
@docker run --rm -it \
98116
-e NODE_ENV=development \
99117
-e NODE_TLS_REJECT_UNAUTHORIZED=0 \
118+
-e GIT_BRANCH=$(GIT_BRANCH) \
119+
-e GIT_COMMIT=$(GIT_COMMIT) \
120+
-e DOCKER_TAG=$(DOCKER_TAG) \
100121
--add-host host.docker.internal:host-gateway \
101122
--platform $(PLATFORM) \
102123
--network dev \

apps/api/src/app.controller.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ export class AppController extends AbstractController {
6060
public getInfo(@Res() res: Response): Response {
6161
return res.json({
6262
...this.appService.getInfo(),
63+
git: {
64+
branch: process.env.GIT_BRANCH || process.env.GITHUB_REF_NAME || 'unknown',
65+
commit: process.env.GIT_COMMIT || process.env.GIT_SHA || process.env.GITHUB_SHA || 'unknown',
66+
},
67+
docker: {
68+
tag: process.env.DOCKER_TAG || process.env.IMAGE_TAG || 'unknown',
69+
},
6370
});
6471
}
6572

0 commit comments

Comments
 (0)