service.xpcool.com/.gitea/workflows/deploy.yml
夏犀麟 856d62f9c7
Some checks failed
Build and Deploy (service.xpcool.com) / build-and-deploy (push) Failing after 20s
FIX: workflow Go 下载源改 aliyun 镜像(go.dev 偶发 SSL 错误 exitcode 35)+ 3 次重试
2026-08-27 00:54:33 +08:00

86 lines
4.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# service.xpcool.com 自动部署Gitea act_runnerpush main/dev 触发
# 注意点与前端差异
# 1. 后端不是拷贝静态文件而是 Go 交叉编译 + Docker 镜像重建容器runner 已挂载 docker.sock
# 2. job 基础镜像 node:20-bullseye Go需下载 Go 1.24 工具链aliyun 镜像GOPROXY goproxy.cn
# 3. 容器 env 通过 Gitea Actions secrets 注入DB_DSN 必须带 mysql: 类型前缀勿写死在仓库
# 4. 数据库结构变更请手动执行 manifest/sql/ 下脚本003004004b007workflow 不做自动 DDL
name: Build and Deploy (service.xpcool.com)
on:
push:
branches: [main, dev]
workflow_dispatch:
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Install git & download Go toolchain
run: |
set -e
# apt 换腾讯云源deb.debian.org 国内直连超时
sed -i 's|deb.debian.org|mirrors.cloud.tencent.com|g; s|security.debian.org|mirrors.cloud.tencent.com|g' /etc/apt/sources.list 2>/dev/null || true
sed -i 's|deb.debian.org|mirrors.cloud.tencent.com|g; s|security.debian.org|mirrors.cloud.tencent.com|g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || true
apt-get update -qq
apt-get install -y -qq git >/dev/null
# aliyun golang 镜像实测 200 go.dev 稳定go.dev 偶发 SSL 错误 exitcode 35
for i in 1 2 3; do
curl -fsSL https://mirrors.aliyun.com/golang/go1.24.5.linux-amd64.tar.gz -o /tmp/go.tar.gz && break
echo "aliyun 下载失败,第 $i 次重试..."; sleep 5
done
mkdir -p /usr/local && tar -C /usr/local -xzf /tmp/go.tar.gz
export PATH="/usr/local/go/bin:$PATH"
echo "/usr/local/go/bin" >> "$GITHUB_ENV"
go version
- name: Checkout (local Gitea)
run: |
git clone --depth 1 --branch "${{ github.ref_name }}" https://oauth2:${{ github.token }}@git.xpcool.com/${{ github.repository }}.git .
git checkout ${{ github.sha }}
- name: Cross compile (linux/amd64, CGO disabled)
env:
GOPROXY: https://goproxy.cn,direct
GOFLAGS: -mod=mod
run: |
export PATH="/usr/local/go/bin:$PATH"
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /tmp/deploy/main main.go
ls -lh /tmp/deploy/main
- name: Assemble deploy dir (binary + config + resource + Dockerfile)
run: |
set -e
mkdir -p /tmp/deploy/manifest/config /tmp/deploy/resource
# 配置基础 config.yaml + 环境化 config.prod.yamlGF_GCFG_ENV=prod 时生效
cp manifest/config/config.prod.yaml /tmp/deploy/manifest/config/config.prod.yaml
cp manifest/config/config.prod.yaml /tmp/deploy/manifest/config/config.yaml
# 静态资源GoFrame public/template
cp -r resource/public /tmp/deploy/resource/public
mkdir -p /tmp/deploy/resource/template
# 容器 Dockerfile仓库内维护 deploy/Dockerfile避免 workflow heredoc 缩进问题
cp deploy/Dockerfile /tmp/deploy/Dockerfile
echo "assemble done"
- name: Build image & restart container
env:
DB_DSN: ${{ secrets.DB_DSN }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
run: |
set -e
cd /tmp/deploy
docker build -t service.xpcool.com:latest .
docker rm -f service.xpcool.com >/dev/null 2>&1 || true
docker run -d --name service.xpcool.com \
--network xpcool-net \
-p 127.0.0.1:10100:10100 \
-e GF_GCFG_ENV=prod \
-e "DB_DSN=$DB_DSN" \
-e "JWT_SECRET=$JWT_SECRET" \
--restart unless-stopped \
service.xpcool.com:latest
# 冒烟等待启动并验证 OpenAPI
sleep 5
curl -fsS -m 10 http://127.0.0.1:10100/api.json | head -c 120 || { echo "SMOKE TEST FAILED"; exit 1; }
echo
echo "Deployed service.xpcool.com (container restarted, 127.0.0.1:10100)"