This commit is contained in:
Havoc420Mac 2025-04-04 09:57:40 +08:00
parent 72ba8efb25
commit 5c84e51795
5 changed files with 123 additions and 34 deletions

View File

@ -0,0 +1,71 @@
name: Build and Push Docker Image
on:
push:
branches:
- "**" # 匹配所有分支
paths-ignore: # 忽略一些不必要的文件
- ".gitignore"
- "README.md"
- ".vscode/**"
pull_request:
branches: ["main"]
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 获取分支名称
id: branch_name
run: |
# 从 GITHUB_REF 提取分支名
branch=${GITHUB_REF#refs/heads/}
echo "branch=$branch" >> $GITHUB_OUTPUT
# 如果是 main 分支,使用 prod 标签,否则使用 dev-分支名
if [ "$branch" = "main" ]; then
echo "env_suffix=prod" >> $GITHUB_OUTPUT
else
echo "env_suffix=dev-${branch}" >> $GITHUB_OUTPUT
fi
# - name: 登录到 Gitea Container Registry
# uses: docker/login-action@v3
# with:
# registry: ${{ env.REGISTRY }}
# username: ${{ gitea.actor }}
# password: ${{ secrets.DOCKER_TOKEN}}
# - name: 提取 Docker 元数据
# id: meta
# uses: docker/metadata-action@v5
# with:
# images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# tags: |
# # main 分支使用标准标签
# type=raw,value=latest,enable=${{ steps.branch_name.outputs.branch == 'main' }}
# type=raw,value=${{ steps.branch_name.outputs.env_suffix }}
# type=sha,format=short,prefix=${{ steps.branch_name.outputs.env_suffix }}-
# type=ref,event=tag,prefix=${{ steps.branch_name.outputs.env_suffix }}-
# - name: 构建并推送 Docker 镜像
# uses: docker/build-push-action@v5
# with:
# context: .
# push: true
# tags: ${{ steps.meta.outputs.tags }}
# labels: ${{ steps.meta.outputs.labels }}
# build-args: |
# BUILDKIT_INLINE_CACHE=0
# # 添加清理缓存的命令
# outputs: type=docker,cleancache=true
# - name: Trigger Portainer Webhook
# run: |
# curl -k -X POST ${{ secrets.BACKEND_WEBHOOK_URL }}

View File

@ -1,31 +0,0 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
name: Go
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Create Directory
run: mkdir public && mkdir -p store/logs/
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22.5'
- name: Build
run: go build -v ./...
# - name: Test
# run: go test -v ./...

49
Dockerfile Normal file
View File

@ -0,0 +1,49 @@
# 使用官方 Go 镜像作为构建阶段
FROM golang:1.22 AS builder
# 个别 go mod 依赖,换源获取。
ENV GOPROXY=https://goproxy.cn,direct
WORKDIR /app
# 复制 go.mod 和 go.sum 文件
COPY go.mod go.sum ./
RUN go mod download
# 复制源代码并构建
COPY . .
# 设置环境变量以确保静态编译
ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64
# 构建静态链接的二进制文件
RUN go build -a -o myapp .
# 使用轻量级的 Alpine 镜像作为最终镜像
FROM alpine:latest
WORKDIR /root/
# 安装必要的运行时库(如果需要)
# 对于完全静态的二进制文件,这一步可以省略
# 但是为了保险起见,可以安装一些基本工具
# RUN apk add --no-cache ca-certificates # 这一步无权
# 复制构建好的二进制文件
COPY --from=builder /app/myapp .
# 需要的文件夹
COPY ./config ./config
RUN mkdir -p store/logs && mkdir -p public
# 确保二进制文件具有可执行权限
RUN chmod +x myapp
# 暴露 端口
EXPOSE 20201
# 运行应用
CMD ["./myapp"]
# TEST 挂起效果,测试用
# CMD ["tail", "-f", "/dev/null"]

View File

@ -174,7 +174,7 @@ QiNiu:
SecretKey: "UP5-GmSmAYNbMlSb6LYLuKZ-fT35nlEzGvOKKm9S"
ElasticSearch:
Start: 1 # 0 不启动1 启动
Start: 0 # 0 不启动1 启动
Addr: "http://localhost:9200"
UserName: "elastic"
Password: ""

View File

@ -5,8 +5,8 @@ Gormv2: # 只针对 gorm 操作数据库有效
IsInitGlobalGormMysql: 1 # 随项目启动为gorm db初始化一个全局 variable.GormDbMysql完全等于*gorm.Db,正确配置数据库,该值必须设置为: 1
SlowThreshold: 30 # 慢 SQL 阈值(sql执行时间超过此时间单位就会触发系统日志记录)
Write:
# Host: "113.44.68.213"
Host: "127.0.0.1"
Host: "113.44.68.213"
# Host: "127.0.0.1"
DataBase: "hav_cats"
Port: 3306
Prefix: "tb_" # 目前没有用到该配置项