Skip to content

Commit 3f6557d

Browse files
committed
fix(api): 🐛 build time & version set as argument
1 parent ad2351e commit 3f6557d

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ jobs:
2424
go-version: '1.23'
2525

2626
- name: Build Docker Image
27-
run: docker build . -f build/Dockerfile -t ghcr.io/${{ github.repository }}:latest
27+
VERSION=$(git describe --tags --always)
28+
BUILDTIME=$(date -R)
29+
run: docker build --build-arg VERSION=$VERSION --build-arg BUILDTIME='$BUILDTIME' . -f build/Dockerfile -t ghcr.io/${{ github.repository }}:latest
2830

2931
- name: Log in to GitHub Container Registry
3032
uses: docker/login-action@v2

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ vendor
66
main
77
cmd/__debug*
88
__debug*
9+
server

Makefile

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,24 @@ install:
1515
go mod tidy
1616

1717
# Build the server executable
18-
build:
19-
go build cmd/main.go
18+
VERSION=$(shell git describe --tags --always)
19+
LDVERSION=-X 'ctf01d/internal/handler.version=$(VERSION)'
20+
BUILDTIME=$(shell date -R)
21+
LDBUILDTIME=-X 'ctf01d/internal/handler.buildTime=$(BUILDTIME)'
22+
server-build:
23+
go build -ldflags "$(LDVERSION) $(LDBUILDTIME)" -o server ./cmd/main.go
24+
25+
# Run the local development server
26+
server-run:
27+
go run cmd/main.go
2028

2129
# Format go files
2230
fmt:
2331
go fmt ./internal/...; \
2432
go fmt ./cmd/...;
2533

26-
# Run the local development server
27-
run-server:
28-
go run cmd/main.go
29-
3034
# Run PostgreSQL container for local development
31-
run-db:
35+
database-run:
3236
@if [ $$(docker ps -a -q -f name=ctf01d-postgres) ]; then \
3337
echo "Container ctf01d-postgres already exists. Restarting..."; \
3438
docker start ctf01d-postgres; \
@@ -44,8 +48,12 @@ run-db:
4448
-p 4112:4112 postgres:16.4; \
4549
fi
4650

51+
# Attach to the running PostgreSQL container
52+
database-attach:
53+
docker exec -it ctf01d-postgres psql -U postgres -d ctf01d_training_platform
54+
4755
# Stop PostgreSQL container
48-
stop-db:
56+
database-stop:
4957
@if [ $$(docker ps -q -f name=ctf01d-postgres) ]; then \
5058
echo "Stopping container ctf01d-postgres..."; \
5159
docker stop ctf01d-postgres; \
@@ -54,14 +62,23 @@ stop-db:
5462
fi
5563

5664
# Cleanup db and restart db and rebuild main app
57-
reset-db:
65+
database-reset:
5866
make stop-db; \
5967
sudo rm -rf docker_tmp/pg_data; \
6068
make run-db; \
6169
make build;
6270

71+
# Remove PostgreSQL container
72+
database-remove:
73+
@if [ $$(docker ps -a -q -f name=ctf01d-postgres) ]; then \
74+
echo "Removing container ctf01d-postgres..."; \
75+
docker rm -f ctf01d-postgres; \
76+
else \
77+
echo "Container ctf01d-postgres does not exist."; \
78+
fi
79+
6380
# Setup test database and run tests
64-
test-db:
81+
test:
6582
# Ensure the PostgreSQL container is running
6683
@if ! [ $$(docker ps -q -f name=ctf01d-postgres) ]; then \
6784
echo "Starting PostgreSQL container..."; \
@@ -74,19 +91,6 @@ test-db:
7491
# Run the tests
7592
@go test -v ./test/server_integration_test.go
7693

77-
# Remove PostgreSQL container
78-
remove-db:
79-
@if [ $$(docker ps -a -q -f name=ctf01d-postgres) ]; then \
80-
echo "Removing container ctf01d-postgres..."; \
81-
docker rm -f ctf01d-postgres; \
82-
else \
83-
echo "Container ctf01d-postgres does not exist."; \
84-
fi
85-
86-
# Attach to the running PostgreSQL container
87-
attach-db:
88-
docker exec -it ctf01d-postgres psql -U postgres -d ctf01d_training_platform
89-
9094
# Generate Go server boilerplate from OpenAPI 3
9195
codegen:
9296
oapi-codegen -generate models,chi -o internal/httpserver/httpserver.gen.go --package httpserver api/openapi.yaml

build/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ RUN go mod tidy
99

1010
COPY ./ ./
1111

12-
RUN CGO_ENABLED=0 GOOS=linux go build -o server ./cmd/main.go
12+
ARG VERSION
13+
ARG BUILDTIME
14+
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-X 'ctf01d/internal/handler.version=${VERSION}' -X ctf01d/internal/handler.buildTime=${BUILDTIME}" -o server cmd/main.go
1315

1416
FROM alpine:latest as prod
1517
LABEL "maintainer"="Evgenii Sopov <mrseakg@gmail.com>"

internal/handler/health.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010

1111
var (
1212
version = "dev"
13-
buildTime = time.Now()
13+
buildTime = time.Now().Format(time.RFC822Z)
1414
)
1515

1616
func (h *Handler) GetVersion(w http.ResponseWriter, r *http.Request) {
1717
res := map[string]string{
1818
"version": version,
1919
"golang": runtime.Version(),
20-
"build_time": buildTime.Format(time.RFC822Z),
20+
"build_time": buildTime,
2121
}
2222
helper.RespondWithJSON(w, http.StatusOK, res)
2323
}

0 commit comments

Comments
 (0)