From 2cf668e455665ddbc72f5144602ca0e4204d26b8 Mon Sep 17 00:00:00 2001 From: cloverstd Date: Wed, 5 Jul 2023 20:59:07 +0800 Subject: [PATCH 1/2] Fix incompatible in low version glibc --- backend/build.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/build.sh b/backend/build.sh index a311ef3a..66a26cbb 100644 --- a/backend/build.sh +++ b/backend/build.sh @@ -147,7 +147,7 @@ BuildRelease() { for arch in ${archs[@]} do - env GOOS=linux GOARCH=${arch} go build -o ./build/${project_name}-linux-${arch} + env GOOS=linux GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-linux-${arch} mkdir -p npm/linux-${arch}/bin cp -r ./build/${project_name}-linux-${arch} npm/linux-${arch}/bin/${project_name} PublishAndGeneratePackageJson "linux" "${arch}" "npm/linux-${arch}" @@ -157,7 +157,7 @@ BuildRelease() { for arch in ${win_archs[@]} do - env GOOS=windows GOARCH=${arch} go build -o ./build/${project_name}-win32-${arch}.exe + env GOOS=windows GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-win32-${arch}.exe mkdir -p npm/win32-${arch} cp -r ./build/${project_name}-win32-${arch}.exe npm/win32-${arch}/${project_name}.exe PublishAndGeneratePackageJson "win32" "${arch}" "npm/win32-${arch}" @@ -167,7 +167,7 @@ BuildRelease() { for arch in ${mac_archs[@]} do - env GOOS=darwin GOARCH=${arch} go build -o ./build/${project_name}-darwin-${arch} + env GOOS=darwin GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-darwin-${arch} mkdir -p npm/darwin-${arch}/bin cp -r ./build/${project_name}-darwin-${arch} npm/darwin-${arch}/bin/${project_name} PublishAndGeneratePackageJson "darwin" "${arch}" "npm/darwin-${arch}" From 66fa676ff31ef40b445c4caa9896ea71412f0411 Mon Sep 17 00:00:00 2001 From: cloverstd Date: Tue, 23 Jan 2024 15:57:26 +0800 Subject: [PATCH 2/2] Add git info for logger --- .github/workflows/build_docker.yml | 3 +++ .github/workflows/release_docker.yml | 5 ++++- .github/workflows/version_docker.yml | 5 ++++- Dockerfile | 8 +++++++- backend/build.sh | 8 +++++--- backend/main.go | 7 +++++++ 6 files changed, 30 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build_docker.yml b/.github/workflows/build_docker.yml index 79aa9f5c..2f78b370 100644 --- a/.github/workflows/build_docker.yml +++ b/.github/workflows/build_docker.yml @@ -61,5 +61,8 @@ jobs: with: context: . push: true + build-args: | + VERSION=main + COMMIT=${{ github.sha }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} diff --git a/.github/workflows/release_docker.yml b/.github/workflows/release_docker.yml index 7fca1a6b..7e85125d 100644 --- a/.github/workflows/release_docker.yml +++ b/.github/workflows/release_docker.yml @@ -11,7 +11,7 @@ name: Create and publish a Docker image on: push: - branches: ["release"] + branches: ['release'] env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} @@ -66,6 +66,9 @@ jobs: with: context: . push: true + build-args: | + VERSION=release + COMMIT=${{ github.sha }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} platforms: linux/amd64,linux/arm64,linux/arm/v7 diff --git a/.github/workflows/version_docker.yml b/.github/workflows/version_docker.yml index 237bde0b..2ef04a0a 100644 --- a/.github/workflows/version_docker.yml +++ b/.github/workflows/version_docker.yml @@ -12,7 +12,7 @@ name: Create and publish a Docker image on: push: tags: - - "v*.*.*" + - 'v*.*.*' env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} @@ -67,6 +67,9 @@ jobs: with: context: . push: true + build-args: | + VERSION=${{ steps.meta.outputs.tags }} + COMMIT=${{ github.sha }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} platforms: linux/amd64,linux/arm64,linux/arm/v7 diff --git a/Dockerfile b/Dockerfile index b2071732..88c8b127 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,15 @@ + FROM golang:1.20-buster AS backend + WORKDIR /app COPY backend/go.mod backend/go.sum ./ + RUN go mod download + COPY backend/. . -RUN go build -o main . +ARG VERSION=unknown +ARG COMMIT=unknown +RUN go build -ldflags="-X main.Version=$VERSION -X main.GitHash=$COMMIT" -o main . FROM debian:bullseye-slim WORKDIR /app diff --git a/backend/build.sh b/backend/build.sh index 449314f5..51cc2c8f 100644 --- a/backend/build.sh +++ b/backend/build.sh @@ -2,9 +2,11 @@ git_repository="https://github.com/HuolalaTech/page-spy-web" git_version=$(git describe --tags $(git rev-list --tags --max-count=1)) +git_hash=$(git rev-parse HEAD) npm_version=$(echo "$git_version" | sed 's/^v//') project_name="page-spy-api" organization="@huolala-tech" +build_with_git_info="-X main.Version=$npm_version -X main.GitHash=$git_hash" GenerateMainPackageJson() { @@ -141,7 +143,7 @@ BuildRelease() { for arch in ${archs[@]} do - env GOOS=linux GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-linux-${arch} + env GOOS=linux GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-linux-${arch} -ldflags="${build_with_git_info}" mkdir -p npm/linux-${arch}/bin cp -r ./build/${project_name}-linux-${arch} npm/linux-${arch}/bin/${project_name} PublishAndGeneratePackageJson "linux" "${arch}" "npm/linux-${arch}" @@ -151,7 +153,7 @@ BuildRelease() { for arch in ${win_archs[@]} do - env GOOS=windows GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-win32-${arch}.exe + env GOOS=windows GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-win32-${arch}.exe -ldflags="${build_with_git_info}" mkdir -p npm/win32-${arch} cp -r ./build/${project_name}-win32-${arch}.exe npm/win32-${arch}/${project_name}.exe PublishAndGeneratePackageJson "win32" "${arch}" "npm/win32-${arch}" @@ -161,7 +163,7 @@ BuildRelease() { for arch in ${mac_archs[@]} do - env GOOS=darwin GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-darwin-${arch} + env GOOS=darwin GOARCH=${arch} CGO_ENABLED=0 go build -o ./build/${project_name}-darwin-${arch} -ldflags="${build_with_git_info}" mkdir -p npm/darwin-${arch}/bin cp -r ./build/${project_name}-darwin-${arch} npm/darwin-${arch}/bin/${project_name} PublishAndGeneratePackageJson "darwin" "${arch}" "npm/darwin-${arch}" diff --git a/backend/main.go b/backend/main.go index 27d7812e..e0ea97ca 100644 --- a/backend/main.go +++ b/backend/main.go @@ -12,12 +12,19 @@ import ( //go:embed dist/* var publicContent embed.FS +var ( + Version string + GitHash string +) + func main() { container := container.Container() err := container.Provide(func() *config.StaticConfig { return &config.StaticConfig{ DirName: "dist", Files: publicContent, + Version: Version, + GitHash: GitHash, } })