From 5c84e51795b3b6dae5baa02f18f0807e1216b893 Mon Sep 17 00:00:00 2001 From: Havoc420Mac <2993167370@qq.com> Date: Fri, 4 Apr 2025 09:57:40 +0800 Subject: [PATCH] cicd-1 --- .gitea/workflows/docker_build.yml | 71 +++++++++++++++++++++++++++++++ .github/workflows/go.yml | 31 -------------- Dockerfile | 49 +++++++++++++++++++++ config/config.yml | 2 +- config/gorm_v2.yml | 4 +- 5 files changed, 123 insertions(+), 34 deletions(-) create mode 100644 .gitea/workflows/docker_build.yml delete mode 100644 .github/workflows/go.yml create mode 100644 Dockerfile diff --git a/.gitea/workflows/docker_build.yml b/.gitea/workflows/docker_build.yml new file mode 100644 index 0000000..a1e81f2 --- /dev/null +++ b/.gitea/workflows/docker_build.yml @@ -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 }} diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml deleted file mode 100644 index dc0ed33..0000000 --- a/.github/workflows/go.yml +++ /dev/null @@ -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 ./... diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..37b38b6 --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/config/config.yml b/config/config.yml index 9d50d89..5080504 100644 --- a/config/config.yml +++ b/config/config.yml @@ -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: "" diff --git a/config/gorm_v2.yml b/config/gorm_v2.yml index 409db62..5b779b2 100644 --- a/config/gorm_v2.yml +++ b/config/gorm_v2.yml @@ -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_" # 目前没有用到该配置项