ci(docker): 添加docker构建工作流并更新Makefile配置
添加Gitea的docker构建工作流,包含环境变量配置、Go环境安装、镜像构建和发布步骤 重构Makefile以支持多平台构建、docker操作和swagger文档生成
This commit is contained in:
parent
9691257bad
commit
cc6ebc18e5
86
.gitea/workflows/docker.yml
Normal file
86
.gitea/workflows/docker.yml
Normal file
@ -0,0 +1,86 @@
|
||||
name: Build docker and publish
|
||||
run-name: The pipeline for docker build
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
|
||||
env:
|
||||
# Docker
|
||||
REPO: ${{ vars.REPO }}
|
||||
DOCKER_USERNAME: ${{ vars.DOCKER_USERNAME }}
|
||||
DOCKER_PASSWORD: ${{ vars.DOCKER_PASSWORD}}
|
||||
# Gitea
|
||||
GIT_USERNAME: ${{ vars.GIT_USERNAME }}
|
||||
GIT_PASSWORD: ${{ vars.GIT_PASSWORD }}
|
||||
# Host SSH
|
||||
SSH_HOST: ${{ vars.SSH_HOST }}
|
||||
SSH_PORT: ${{ vars.SSH_PORT }}
|
||||
SSH_USER: ${{ vars.SSH_USER }}
|
||||
SSH_PASSWORD: ${{ vars.SSH_PASSWORD }}
|
||||
# SMTP
|
||||
SMTP_SERVER_ADDRESS: ${{ vars.SMTP_SERVER_ADDRESS }}
|
||||
SMTP_USERNAME: ${{ vars.SMTP_USERNAME }}
|
||||
SMTP_PASSWORD: ${{ vars.SMTP_PASSWORD }}
|
||||
|
||||
jobs:
|
||||
linux:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
# 使用gitea-tool-cache需要指定具体的版本号
|
||||
go: ["1.24.3"]
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: https://${{ env.GIT_USERNAME }}:${{ env.GIT_PASSWORD }}@${{ vars.DOMAIN_OF_GITEA}}/actions/checkout@v4
|
||||
|
||||
# 将.env环境变量配置文件拷贝致gitea runner容器
|
||||
- name: copy env file to runner container
|
||||
uses: https://${{ env.GIT_USERNAME }}:${{ env.GIT_PASSWORD }}@${{ vars.DOMAIN_OF_GITEA}}/actions/ssh-action@v0.1.10
|
||||
with:
|
||||
host: ${{ env.SSH_HOST }}
|
||||
username: ${{ env.SSH_USER }}
|
||||
password: ${{ env.SSH_PASSWORD }}
|
||||
port: ${{ env.SSH_PORT }}
|
||||
debug: true
|
||||
script: |
|
||||
mkdir -p /install/cicd_env_files
|
||||
cd /install/cicd_env_files
|
||||
rm -f ./.env
|
||||
docker cp ${{env.JOB_CONTAINER_NAME}}:${{gitea.WORKSPACE}}/deploy/.env ./.env
|
||||
source ./.env
|
||||
docker cp .env ${{ vars.RUNNER_CONTAINER_NAME }}:/.env
|
||||
docker exec ${{ vars.RUNNER_CONTAINER_NAME }} /bin/bash -c "source /.env"
|
||||
mkdir -p ${HOST_APIDOC_DIRCTORY}
|
||||
cd ${HOST_APIDOC_DIRCTORY}
|
||||
docker cp ${{env.JOB_CONTAINER_NAME}}:${{gitea.WORKSPACE}}/${SERVICE_NAME}.json .
|
||||
|
||||
- name: Install Go environment
|
||||
uses: https://${{ env.GIT_USERNAME }}:${{ env.GIT_PASSWORD }}@${{ vars.DOMAIN_OF_GITEA}}/actions/gitea-tool-cache@v5
|
||||
with:
|
||||
# 需要指定具体版本号!
|
||||
go-version: ${{ matrix.go }}
|
||||
|
||||
- uses: https://${{ env.GIT_USERNAME }}:${{ env.GIT_PASSWORD }}@${{ vars.DOMAIN_OF_GITEA}}/actions/setup-go@v2
|
||||
with:
|
||||
go-version: ${{ matrix.go }}
|
||||
|
||||
|
||||
- name: Prepare GO environment
|
||||
run: |
|
||||
go env -w GOPROXY=https://goproxy.cn,direct
|
||||
go env -w GOPRIVATE="${{ vars.DOMAIN_OF_GITEA}}"
|
||||
git config --global url."https://${{ env.GIT_USERNAME }}:${{ env.GIT_PASSWORD }}@${{ vars.DOMAIN_OF_GITEA}}/".insteadOf "https://${{ vars.DOMAIN_OF_GITEA}}/"
|
||||
|
||||
- name: Build and push docker image
|
||||
run: |
|
||||
source ${{gitea.WORKSPACE}}/deploy/.env
|
||||
go mod tidy
|
||||
make build-linux
|
||||
make docker
|
||||
make publish-docker
|
||||
make docker-run
|
||||
185
Makefile
185
Makefile
@ -1,84 +1,119 @@
|
||||
NAME="ppanel-server"
|
||||
BINDIR=bin
|
||||
VERSION=$(shell git describe --tags || echo "unknown version")
|
||||
BUILDTIME=$(shell date -u)
|
||||
GOBUILD=CGO_ENABLED=0 go build -trimpath -ldflags '-X "github.com/perfect-panel/server/pkg/constant.Version=$(VERSION)" \
|
||||
-X "github.com/perfect-panel/server/pkg/constant.BuildTime=$(BUILDTIME)" \
|
||||
-w -s -buildid='
|
||||
# Custom configuration | 独立配置
|
||||
# Service name | 项目名称
|
||||
SERVICE=Miniapp
|
||||
# Service name in specific style | 项目经过style格式化的名称
|
||||
SERVICE_STYLE=miniapp
|
||||
# Service name in lowercase | 项目名称全小写格式
|
||||
SERVICE_LOWER=miniapp
|
||||
# Service name in snake format | 项目名称下划线格式
|
||||
SERVICE_SNAKE=miniapp
|
||||
# Service name in snake format | 项目名称短杠格式
|
||||
SERVICE_DASH=miniapp
|
||||
|
||||
PLATFORM_LIST = \
|
||||
darwin-amd64 \
|
||||
darwin-amd64-v3 \
|
||||
darwin-arm64 \
|
||||
linux-386 \
|
||||
linux-amd64 \
|
||||
linux-amd64-v3 \
|
||||
linux-armv5 \
|
||||
linux-armv6 \
|
||||
linux-armv7 \
|
||||
linux-arm64 \
|
||||
# The project version, if you don't use git, you should set it manually | 项目版本,如果不使用git请手动设置
|
||||
VERSION=$(shell git describe --tags --always)
|
||||
|
||||
WINDOWS_ARCH_LIST = \
|
||||
windows-386 \
|
||||
windows-amd64 \
|
||||
windows-amd64-v3 \
|
||||
windows-arm64 \
|
||||
windows-armv7
|
||||
# The project file name style | 项目文件命名风格
|
||||
PROJECT_STYLE=go_zero
|
||||
|
||||
all: linux-amd64 darwin-amd64 windows-amd64 # Most used
|
||||
# Whether to use i18n | 是否启用 i18n
|
||||
PROJECT_I18N=true
|
||||
|
||||
darwin-amd64:
|
||||
GOARCH=amd64 GOOS=darwin $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||
# The suffix after build or compile | 构建后缀
|
||||
PROJECT_BUILD_SUFFIX=api
|
||||
|
||||
darwin-amd64-v3:
|
||||
GOARCH=amd64 GOOS=darwin GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||
|
||||
darwin-arm64:
|
||||
GOARCH=arm64 GOOS=darwin $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||
|
||||
linux-386:
|
||||
GOARCH=386 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||
|
||||
linux-amd64:
|
||||
GOARCH=amd64 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||
|
||||
linux-amd64-v3:
|
||||
GOARCH=amd64 GOOS=linux GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||
|
||||
linux-armv5:
|
||||
GOARCH=arm GOOS=linux GOARM=5 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||
|
||||
linux-armv6:
|
||||
GOARCH=arm GOOS=linux GOARM=6 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||
|
||||
linux-armv7:
|
||||
GOARCH=arm GOOS=linux GOARM=7 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||
|
||||
linux-arm64:
|
||||
GOARCH=arm64 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||
|
||||
windows-386:
|
||||
GOARCH=386 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
||||
|
||||
windows-amd64:
|
||||
GOARCH=amd64 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
||||
|
||||
windows-amd64-v3:
|
||||
GOARCH=amd64 GOOS=windows GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
||||
|
||||
windows-arm64:
|
||||
GOARCH=arm64 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
||||
|
||||
windows-armv7:
|
||||
GOARCH=arm GOOS=windows GOARM=7 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
||||
# Swagger type, support yml,json | Swagger 文件类型,支持yml,json
|
||||
SWAGGER_TYPE=json
|
||||
|
||||
|
||||
gz_releases=$(addsuffix .gz, $(PLATFORM_LIST))
|
||||
zip_releases=$(addsuffix .zip, $(WINDOWS_ARCH_LIST))
|
||||
# The arch of the build | 构建的架构
|
||||
GOARCH=amd64
|
||||
|
||||
$(gz_releases): %.gz : %
|
||||
chmod +x $(BINDIR)/$(NAME)-$(basename $@)
|
||||
gzip -f -S -$(VERSION).gz $(BINDIR)/$(NAME)-$(basename $@)
|
||||
# The repository of docker | Docker 仓库地址
|
||||
DOCKER_REPO=docker.io/xxx
|
||||
|
||||
$(zip_releases): %.zip : %
|
||||
zip -m -j $(BINDIR)/$(NAME)-$(basename $@)-$(VERSION).zip $(BINDIR)/$(NAME)-$(basename $@).exe
|
||||
# ---- You may not need to modify the codes below | 下面的代码大概率不需要更改 ----
|
||||
|
||||
GO ?= go
|
||||
GOFMT ?= gofmt "-s"
|
||||
GOFILES := $(shell find . -name "*.go")
|
||||
LDFLAGS := -s -w
|
||||
|
||||
.PHONY: test
|
||||
test: # Run test for the project | 运行项目测试
|
||||
go test -v --cover ./internal/..
|
||||
|
||||
.PHONY: fmt
|
||||
fmt: # Format the codes | 格式化代码
|
||||
$(GOFMT) -w $(GOFILES)
|
||||
|
||||
.PHONY: lint
|
||||
lint: # Run go linter | 运行代码错误分析
|
||||
golangci-lint run -D staticcheck
|
||||
|
||||
.PHONY: tools
|
||||
tools: # Install the necessary tools | 安装必要的工具
|
||||
$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest;
|
||||
$(GO) install github.com/go-swagger/go-swagger/cmd/swagger@latest
|
||||
|
||||
|
||||
.PHONY: docker
|
||||
docker: # Build the docker image | 构建 docker 镜像
|
||||
docker build -f Dockerfile -t ${API_IMAGE_NAME}:${VERSION} .
|
||||
@echo "Build docker successfully"
|
||||
|
||||
.PHONY: publish-docker
|
||||
publish-docker: # Publish docker image | 发布 docker 镜像
|
||||
echo "${DOCKER_PASSWORD}" | docker login --username ${DOCKER_USERNAME} --password-stdin ${REPO}
|
||||
docker tag ${API_IMAGE_NAME}:${VERSION} ${API_IMAGE_NAME}:latest
|
||||
docker push ${API_IMAGE_NAME}:latest
|
||||
@echo "Publish docker successfully"
|
||||
|
||||
.PHONY: docker-run
|
||||
docker-run: # Publish docker image | 发布 docker 镜像
|
||||
docker rm -f ${API_CONTAINER_NAME}
|
||||
docker compose -p ${DOCKER_PROJECT_NAME} -f ${DOCKER_COMPOSE_FILE} up -d
|
||||
@echo "docker run successfully"
|
||||
|
||||
.PHONY: swagger-docker
|
||||
swagger-docker: # Run the swagger Docker | 运行接口文档服务
|
||||
@echo "run swagger-ui container..."
|
||||
docker rm -f ${PROJECT_NAME}-${SERVICE_NAME}-apidoc
|
||||
docker compose -p ${DOC_DOCKER_PROJECT_NAME} -f ${APIDOC_DOCKER_COMPOSE_FILE} up -d
|
||||
@echo "run swagger-ui container successfully"
|
||||
|
||||
.PHONY: gen-swagger
|
||||
gen-swagger: # Generate swagger file | 生成 swagger 文件
|
||||
swagger generate spec --output=./$(SERVICE_STYLE).$(SWAGGER_TYPE) --scan-models
|
||||
@echo "Generate swagger successfully"
|
||||
|
||||
.PHONY: serve-swagger
|
||||
serve-swagger: # Run the swagger server | 运行 swagger 服务
|
||||
lsof -i:36666 | awk 'NR!=1 {print $2}' | xargs killall -9 || true
|
||||
swagger serve -F=swagger --port 36666 $(SERVICE_STYLE).$(SWAGGER_TYPE)
|
||||
@echo "Serve swagger-ui successfully"
|
||||
|
||||
.PHONY: gen-api
|
||||
gen-api: # Generate API files | 生成 API 的代码
|
||||
goctls api go --api ./desc/all.api --dir ./ --trans_err=true --style=$(PROJECT_STYLE)
|
||||
swagger generate spec --output=./$(SERVICE_STYLE).$(SWAGGER_TYPE) --scan-models
|
||||
@echo "Generate API codes successfully"
|
||||
|
||||
.PHONY: build-win
|
||||
build-win: # Build project for Windows | 构建Windows下的可执行文件
|
||||
env CGO_ENABLED=0 GOOS=windows GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_$(PROJECT_BUILD_SUFFIX).exe $(SERVICE_STYLE).go
|
||||
@echo "Build project for Windows successfully"
|
||||
|
||||
.PHONY: build-mac
|
||||
build-mac: # Build project for MacOS | 构建MacOS下的可执行文件
|
||||
env CGO_ENABLED=0 GOOS=darwin GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_$(PROJECT_BUILD_SUFFIX) $(SERVICE_STYLE).go
|
||||
@echo "Build project for MacOS successfully"
|
||||
|
||||
.PHONY: build-linux
|
||||
build-linux: # Build project for Linux | 构建Linux下的可执行文件
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_$(PROJECT_BUILD_SUFFIX) $(SERVICE_STYLE).go
|
||||
@echo "Build project for Linux successfully"
|
||||
|
||||
.PHONY: help
|
||||
help: # Show help | 显示帮助
|
||||
@grep -E '^[a-zA-Z0-9 -]+:.*#' Makefile | sort | while read -r l; do printf "\033[1;32m$$(echo $$l | cut -f 1 -d':')\033[00m:$$(echo $$l | cut -f 2- -d'#')\n"; done
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user