Skip to content

Commit 9f94d36

Browse files
committed
Add git info for logger
1 parent 6a27fc8 commit 9f94d36

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

.github/workflows/build_docker.yml

+3
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,8 @@ jobs:
6161
with:
6262
context: .
6363
push: true
64+
build-args: |
65+
VERSION=main
66+
COMMIT=${{ github.sha }}'
6467
tags: ${{ steps.meta.outputs.tags }}
6568
labels: ${{ steps.meta.outputs.labels }}

.github/workflows/release_docker.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ name: Create and publish a Docker image
1111

1212
on:
1313
push:
14-
branches: ["release"]
14+
branches: ['release']
1515
env:
1616
REGISTRY: ghcr.io
1717
IMAGE_NAME: ${{ github.repository }}
@@ -66,6 +66,9 @@ jobs:
6666
with:
6767
context: .
6868
push: true
69+
build-args: |
70+
VERSION=release
71+
COMMIT=${{ github.sha }}'
6972
tags: ${{ steps.meta.outputs.tags }}
7073
labels: ${{ steps.meta.outputs.labels }}
7174
platforms: linux/amd64,linux/arm64,linux/arm/v7

.github/workflows/version_docker.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ name: Create and publish a Docker image
1212
on:
1313
push:
1414
tags:
15-
- "v*.*.*"
15+
- 'v*.*.*'
1616
env:
1717
REGISTRY: ghcr.io
1818
IMAGE_NAME: ${{ github.repository }}
@@ -67,6 +67,9 @@ jobs:
6767
with:
6868
context: .
6969
push: true
70+
build-args: |
71+
VERSION=${{ steps.meta.outputs.tags }}
72+
COMMIT=${{ github.sha }}'
7073
tags: ${{ steps.meta.outputs.tags }}
7174
labels: ${{ steps.meta.outputs.labels }}
7275
platforms: linux/amd64,linux/arm64,linux/arm/v7

Dockerfile

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
12
FROM golang:1.20-buster AS backend
3+
24
WORKDIR /app
35
COPY backend/go.mod backend/go.sum ./
6+
47
RUN go mod download
8+
59
COPY backend/. .
6-
RUN go build -o main .
10+
ARG VERSION=unknown
11+
ARG COMMIT=unknown
12+
RUN go build -ldflags="-X main.Version=$VERSION -X main.GitHash=$COMMIT" -o main .
713

814
FROM debian:bullseye-slim
915
WORKDIR /app

backend/build.sh

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
git_repository="https://github.com/HuolalaTech/page-spy-web"
44
git_version=$(git describe --tags $(git rev-list --tags --max-count=1))
5+
git_hash=$(git rev-parse HEAD)
56
npm_version=$(echo "$git_version" | sed 's/^v//')
67
project_name="page-spy-api"
78
organization="@huolala-tech"
9+
build_with_git_info="-X main.Version=$npm_version -X main.GitHash=$git_hash"
810

911

1012
GenerateMainPackageJson() {
@@ -141,7 +143,7 @@ BuildRelease() {
141143

142144
for arch in ${archs[@]}
143145
do
144-
env GOOS=linux GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-linux-${arch}
146+
env GOOS=linux GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-linux-${arch} -ldflags="${build_with_git_info}"
145147
mkdir -p npm/linux-${arch}/bin
146148
cp -r ./build/${project_name}-linux-${arch} npm/linux-${arch}/bin/${project_name}
147149
PublishAndGeneratePackageJson "linux" "${arch}" "npm/linux-${arch}"
@@ -151,7 +153,7 @@ BuildRelease() {
151153

152154
for arch in ${win_archs[@]}
153155
do
154-
env GOOS=windows GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-win32-${arch}.exe
156+
env GOOS=windows GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-win32-${arch}.exe -ldflags="${build_with_git_info}"
155157
mkdir -p npm/win32-${arch}
156158
cp -r ./build/${project_name}-win32-${arch}.exe npm/win32-${arch}/${project_name}.exe
157159
PublishAndGeneratePackageJson "win32" "${arch}" "npm/win32-${arch}"
@@ -161,7 +163,7 @@ BuildRelease() {
161163

162164
for arch in ${mac_archs[@]}
163165
do
164-
env GOOS=darwin GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-darwin-${arch}
166+
env GOOS=darwin GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-darwin-${arch} -ldflags="${build_with_git_info}"
165167
mkdir -p npm/darwin-${arch}/bin
166168
cp -r ./build/${project_name}-darwin-${arch} npm/darwin-${arch}/bin/${project_name}
167169
PublishAndGeneratePackageJson "darwin" "${arch}" "npm/darwin-${arch}"

backend/main.go

+7
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ import (
1212
//go:embed dist/*
1313
var publicContent embed.FS
1414

15+
var (
16+
Version string
17+
GitHash string
18+
)
19+
1520
func main() {
1621
container := container.Container()
1722
err := container.Provide(func() *config.StaticConfig {
1823
return &config.StaticConfig{
1924
DirName: "dist",
2025
Files: publicContent,
26+
Version: Version,
27+
GitHash: GitHash,
2128
}
2229
})
2330

0 commit comments

Comments
 (0)