Skip to content

Commit

Permalink
根据git tag增加项目版本信息和版本接口
Browse files Browse the repository at this point in the history
  • Loading branch information
gjing1st committed Feb 7, 2025
1 parent 45a9e7c commit f06660e
Show file tree
Hide file tree
Showing 13 changed files with 807 additions and 19 deletions.
36 changes: 29 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,43 @@ REPOSITORY := 192.168.1.1:443/$(IMAGE_NAME)/$(IMAGE_NAME)-backend
#保存的镜像存放的目录
TARGET_DIR := /opt/build-host/ha-server/$(IMAGE_TAG)

#
# Go.
#
# Use GOPROXY environment variable if set
GOPROXY := $(shell go env GOPROXY)
ifeq ($(GOPROXY),)
GOPROXY := https://goproxy.cn,direct
endif
export GOPROXY
# This option is for running docker manifest command
export DOCKER_CLI_EXPERIMENTAL := enabled

# Active module mode, as we use go modules to manage dependencies
export GO111MODULE=on
# Set build time variables including version details
LDFLAGS := $(shell version/version.sh)

.PHONY: help
help: ## 使用帮助.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n\nTargets:\n"} /^[0-9A-Za-z_-]+:.*?##/ { printf " \033[36m%-45s\033[0m %s\n", $$1, $$2 } /^\$$\([0-9A-Za-z_-]+\):.*?##/ { gsub("_","-", $$1); printf " \033[36m%-45s\033[0m %s\n", tolower(substr($$1, 3, length($$1)-7)), $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

# 运行
.PHONY: run
run:
run: ## 运行服务
@echo "Building image with tag '$(IMAGE_TAG)'"
go run cmd/ha/main.go

.PHONY: build
build:
go build -o ha cmd/ha/main.go
build: ## 编译二进制文件
go build -trimpath -ldflags "$(LDFLAGS)" -o ha cmd/ha/main.go

# 打包成docker镜像 make docker VERSION=v1.1.0
.PHONY: docker
TAG := $(IMAGE_NAME):$(IMAGE_TAG)
docker:
docker: ## 打包成docker镜像
@echo "Building image with tag '$(TAG)'"
docker build --build-arg APP_VERSION=$(IMAGE_TAG) -f ./build/docker/Dockerfile -t $(TAG) .
docker build --build-arg LDFLAGS="$(LDFLAGS)" -f ./build/docker/Dockerfile -t $(TAG) .
#如果运行在gitlab runner可能创建/opt/build-host/ha-server 权限不足,需要到服务器手动创建项目目录,并将目录权限更改为gitlab-runner
# chown -R gitlab-runner:gitlab-runner ./opt/build-host/***
mkdir -p $(TARGET_DIR)
Expand All @@ -43,13 +65,13 @@ docker:

# 推送到镜像仓库
.PHONY: push_docker
push_docker:
push_docker: ## 推送到镜像仓库
@echo "Pushing image with tag '$(IMAGE_NAME):$(IMAGE_TAG)' to repository '$(REPOSITORY):$(IMAGE_TAG)'"
docker tag $(IMAGE_NAME):$(IMAGE_TAG) $(REPOSITORY):$(IMAGE_TAG)
docker login -u admin -p 123456 192.168.1.1:443
docker push $(REPOSITORY):$(IMAGE_TAG)

#生成swag接口文档
.PHONY: swag
swag:
swag: ## 生成swag接口文档
swag init -g ./cmd/ha/main.go
5 changes: 2 additions & 3 deletions build/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
ARG GO_VERSION=1.23.0
ARG ALPINE_VERSION=3.20
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS build
# 定义项目版本,在Makefile中传入
ARG APP_VERSION=1.0.0
ARG LDFLAGS
WORKDIR /src
ENV GOPROXY https://goproxy.cn,direct
RUN --mount=type=cache,target=/go/pkg/mod/ \
Expand All @@ -11,7 +10,7 @@ RUN --mount=type=cache,target=/go/pkg/mod/ \
go mod download -x
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,target=. \
CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w -X github.com/gjing1st/hertz-admin/version.version=${APP_VERSION}" -o /bin/server ./cmd/ha/main.go
CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w ${LDFLAGS}" -o /bin/server ./cmd/ha/main.go

FROM alpine:${ALPINE_VERSION}
COPY --from=build /bin/server /bin/
Expand Down
5 changes: 4 additions & 1 deletion cmd/ha/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package main

import (
"encoding/json"
"fmt"
"github.com/gjing1st/hertz-admin/internal/apiserver"
"github.com/gjing1st/hertz-admin/internal/apiserver/store"
Expand All @@ -29,7 +30,9 @@ import (
//go:generate go env -w GOPROXY=https://goproxy.cn,direct
//go:generate go mod tidy
func main() {
fmt.Println("ha-version:", version.GetVersion())
v := version.Get()
y, _ := json.MarshalIndent(&v, "", " ")
fmt.Println("ha-version: ", string(y))
//加载配置文件
config.Init()
//加载数据库驱动并初始化数据
Expand Down
13 changes: 13 additions & 0 deletions internal/apiserver/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gjing1st/hertz-admin/internal/apiserver/model/dict"
"github.com/gjing1st/hertz-admin/internal/apiserver/model/response"
"github.com/gjing1st/hertz-admin/internal/apiserver/service"
"github.com/gjing1st/hertz-admin/version"
)

type ConfigController struct {
Expand Down Expand Up @@ -56,3 +57,15 @@ func (cc ConfigController) LoginType(ctx context.Context, c *app.RequestContext)
res.LoginType = v
response.OkWithData(c, res)
}

// GetVersion
// @description: 获取版本信息
// @param:
// @author: GJing
// @email: [email protected]
// @date: 2025/2/7 上午11:00
// @success:
func (cc ConfigController) GetVersion(ctx context.Context, c *app.RequestContext) {
v := version.Get()
response.OkWithData(c, v)
}
1 change: 0 additions & 1 deletion internal/apiserver/router/v1/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func InitApi(h *server.Hertz) {
initWithoutConfigRouter(apiV1)
initCategory(apiV1)
initUser(apiV1)
initIdCard(apiV1)
}
{
//需要登录才能访问
Expand Down
1 change: 1 addition & 0 deletions internal/apiserver/router/v1/without.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ func initWithoutConfigRouter(apiV1 *route.RouterGroup) {
configController := controller.ConfigController{}
apiV1.GET("init/step", configController.GetInitStep) //初始化状态步骤
apiV1.GET("login-type", configController.LoginType) //登录方式
apiV1.GET("version", configController.GetVersion) //登录方式

}
8 changes: 4 additions & 4 deletions internal/apiserver/store/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func MysqlEmptyDsn() string {
// @date: 2022/10/26 18:15
// @success:
func createDatabase(dsn string, driver string, createSql string) error {
db, err := sql.Open(driver, dsn)
db1, err := sql.Open(driver, dsn)
if err != nil {
return err
}
Expand All @@ -210,11 +210,11 @@ func createDatabase(dsn string, driver string, createSql string) error {
if err != nil {
fmt.Println(err)
}
}(db)
if err = db.Ping(); err != nil {
}(db1)
if err = db1.Ping(); err != nil {
return err
}
_, err = db.Exec(createSql)
_, err = db1.Exec(createSql)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Log struct {
type Database struct {
Host string `default:"localhost"`
UserName string `default:"root"`
Password string `default:"123456"`
Password string `default:"root"`
DBName string `default:"ha"`
Port string `default:"3306"`
MinConns int `default:"90"` //连接池最大空闲连接数量 不要太小
Expand Down
29 changes: 29 additions & 0 deletions scripts/ci-lint-dockerfiles.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# Copyright 2022 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

HADOLINT_VER=${1:-latest}
HADOLINT_FAILURE_THRESHOLD=${2:-warning}

FILES=$(find -- * -name Dockerfile)
while read -r file; do
echo "Linting: ${file}"
# Configure the linter to fail for warnings and errors. Can be set to: error | warning | info | style | ignore | none
docker run --rm -i ghcr.io/hadolint/hadolint:"${HADOLINT_VER}" hadolint --failure-threshold "${HADOLINT_FAILURE_THRESHOLD}" - < "${file}"
done <<< "${FILES}"
Loading

0 comments on commit f06660e

Please sign in to comment.