Skip to content

Commit 404d342

Browse files
committed
[version] add version info
1 parent 0566103 commit 404d342

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

.air.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ tmp_dir = "tmp"
44

55
[build]
66
bin = "tmp/main.exe"
7-
cmd = "go build -o ./tmp/main.exe ./cmd/sms-gateway"
7+
cmd = "go build -ldflags='-X github.com/capcom6/sms-gateway/internal/version.AppVersion=dev -X github.com/capcom6/sms-gateway/internal/version.AppRelease=1' -o ./tmp/main.exe ./cmd/sms-gateway"
88
delay = 1000
99
exclude_dir = ["api", "assets", "tmp", "vendor", "testdata", "tmp", "web"]
1010
exclude_file = []

.github/workflows/docker-build.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ jobs:
4646
username: ${{ secrets.username }}
4747
password: ${{ secrets.password }}
4848

49+
- name: Set APP_VERSION env
50+
run: echo APP_VERSION=$(echo ${GITHUB_REF} | rev | cut -d'/' -f 1 | rev ) >> ${GITHUB_ENV}
51+
- name: Set APP_RELEASE env
52+
run: echo APP_RELEASE=$(( ($(date +%s) - $(date -d "2022-06-15" +%s)) / 86400 )) >> ${GITHUB_ENV}
53+
4954
- name: Build and push Docker image
5055
uses: docker/build-push-action@v5
5156
with:
5257
file: build/package/Dockerfile
53-
build-args: APP=${{ inputs.app-name }}
58+
build-args: APP=${{ inputs.app-name }} APP_VERSION=${{ env.APP_VERSION }} APP_RELEASE_ID=${{ env.APP_RELEASE }}
5459
push: true
5560
tags: ${{ steps.meta.outputs.tags }}
5661
labels: ${{ steps.meta.outputs.labels }}

build/package/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
FROM golang:1.22-alpine AS build
33

44
ARG APP
5+
ARG APP_VERSION=1.0.0
6+
ARG APP_RELEASE_ID=1
57
WORKDIR /go/src
68

79
# Copy go.mod and go.sum
@@ -15,7 +17,7 @@ COPY . .
1517

1618
# Builds the application as a staticly linked one, to allow it to run on alpine
1719
# RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o app .
18-
RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o app ./cmd/${APP}/main.go
20+
RUN CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags="-w -s -X github.com/capcom6/${APP}/internal/version.AppVersion=${APP_VERSION} -X github.com/capcom6/${APP}/internal/version.AppRelease=${APP_RELEASE_ID}" -o app ./cmd/${APP}/main.go
1921

2022
# Build MkDocs
2123
FROM squidfunk/mkdocs-material:9.5.15 AS mkdocs

internal/sms-gateway/handlers/health.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handlers
22

33
import (
44
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/health"
5+
"github.com/capcom6/sms-gateway/internal/version"
56
"github.com/capcom6/sms-gateway/pkg/maps"
67
"github.com/capcom6/sms-gateway/pkg/smsgateway"
78
"github.com/gofiber/fiber/v2"
@@ -42,7 +43,9 @@ func (h *healthHandler) getHealth(c *fiber.Ctx) error {
4243
}
4344

4445
res := smsgateway.HealthResponse{
45-
Status: smsgateway.HealthStatus(check.Status),
46+
Status: smsgateway.HealthStatus(check.Status),
47+
Version: version.AppVersion,
48+
ReleaseID: version.AppReleaseID(),
4649
Checks: maps.MapValues(
4750
check.Checks,
4851
func(c health.CheckDetail) smsgateway.HealthCheck {

internal/version/version.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package version
2+
3+
import "strconv"
4+
5+
const notSet string = "not set"
6+
7+
// these information will be collected when build, by `-ldflags "-X main.appVersion=0.1"`
8+
var (
9+
AppVersion = notSet
10+
AppRelease = notSet
11+
)
12+
13+
func AppReleaseID() int {
14+
id, _ := strconv.Atoi(AppRelease)
15+
16+
return id
17+
}

0 commit comments

Comments
 (0)