Skip to content

Commit

Permalink
Add version sub command to server app (caoyingjunz#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytaxii authored Feb 3, 2024
1 parent 1862ce5 commit 735c855
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ jobs:
uses: actions/checkout@v3

- name: Get short commit hash
run: echo "COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
run: |
echo "COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
echo "TIMESTAMP=$(date +%Y%m%d%H%M%S)" >> $GITHUB_ENV
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand All @@ -36,6 +38,8 @@ jobs:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm64
build-args: |
VERSION=${{ env.COMMIT_HASH }}-${{ env.TIMESTAMP }}
push: true
tags: |
${{ secrets.DOCKER_NAME }}/pixiu:latest
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
FROM golang:1.17-alpine as builder
WORKDIR /app
ARG VERSION
ENV GOPROXY=https://goproxy.cn
COPY ./go.mod ./
COPY ./go.sum ./
#RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -ldflags "-s -w" -o pixiu ./cmd
RUN CGO_ENABLED=0 go build -ldflags "-s -w -X 'main.version=${VERSION}'" -o pixiu ./cmd

FROM busybox as runner
COPY --from=builder /app/pixiu /app
ENTRYPOINT ["/app"]
ENTRYPOINT ["/app"]
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

tag = v0.1
releaseName = pixiu
dockerhubUser = jacky06
dockerhubUser ?= jacky06
k8sVersion ?= v1.23.6
helmVersion ?= v3.7.1
targetDir ?= dist
commitHash = $(shell git rev-parse --short HEAD)
# e.g. 1862ce5-20240203180617
version = $(commitHash)-$(shell date +%Y%m%d%H%M%S)

ALL: run

run: build
./pixiu --configfile ./config.yaml

build:
go build -o $(releaseName) ./cmd/
go build -o $(targetDir)/$(releaseName) -ldflags "-X 'main.version=$(version)'" ./cmd/

image:
docker build -t $(dockerhubUser)/$(releaseName):$(tag) .
docker build -t $(dockerhubUser)/$(releaseName):$(tag) --build-arg VERSION=$(version) .

push: image
docker push $(dockerhubUser)/$(releaseName):$(tag)
Expand Down
12 changes: 11 additions & 1 deletion cmd/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/caoyingjunz/pixiu/cmd/app/options"
)

func NewServerCommand() *cobra.Command {
func NewServerCommand(version string) *cobra.Command {
opts, err := options.NewOptions()
if err != nil {
klog.Fatalf("unable to initialize command options: %v", err)
Expand Down Expand Up @@ -67,6 +67,16 @@ func NewServerCommand() *cobra.Command {

// 绑定命令行参数
opts.BindFlags(cmd)

verCmd := &cobra.Command{
Use: "version",
Short: "Print the version",
Long: "Print version and exit.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version)
},
}
cmd.AddCommand(verCmd)
return cmd
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/pixiuserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"github.com/caoyingjunz/pixiu/cmd/app"
)

var version string

// @title Pixiu API Documentation
// @version 1.0
// @termsOfService https://github.com/caoyingjunz/pixiu
Expand All @@ -53,7 +55,7 @@ func main() {
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = io.Discard

cmd := app.NewServerCommand()
cmd := app.NewServerCommand(version)
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down

0 comments on commit 735c855

Please sign in to comment.