Files
junhong_cmp_fiber/.gitea/workflows/deploy.yaml
huang 4d86799448
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Failing after 1s
全面优化 Docker 构建和部署流程
- Dockerfile.api/worker: 添加阿里云 Alpine 镜像源加速(构建+运行阶段)
- Dockerfile.api/worker: 提前设置 Go 环境变量(GOPROXY、CGO_ENABLED)
- Dockerfile.api: 移除 curl 依赖,改用 Alpine 自带 wget
- 工作流: 添加 Docker Hub 镜像加速(3个国内源)
- 工作流: 修复部署目录不存在问题(自动创建+复制配置)

预期构建时间:从 3+ 小时降低到 15-20 分钟
2026-01-20 09:43:42 +08:00

118 lines
4.1 KiB
YAML
Raw 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.
name: 构建并部署到测试环境(无 SSH
on:
push:
branches:
- main
- dev
- test
env:
REGISTRY: registry.boss160.cn
API_IMAGE: registry.boss160.cn/junhong/cmp-fiber-api
WORKER_IMAGE: registry.boss160.cn/junhong/cmp-fiber-worker
DEPLOY_DIR: /home/qycard001/app/junhong_cmp
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: 配置 Docker 镜像加速
run: |
# 配置 Docker Hub 镜像加速阿里云、中科大、Docker 官方镜像)
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json > /dev/null <<EOF
{
"registry-mirrors": [
"https://docker.1ms.run",
"https://docker.m.daocloud.io",
"https://docker.unsee.tech"
]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
# 等待 Docker 重启完成
sleep 5
docker info | grep -A 10 "Registry Mirrors"
- name: 检出代码
run: |
git clone https://git.boss160.cn/csxj2026/junhong_cmp_fiber.git .
git checkout ${{ github.sha }}
- name: 设置镜像标签
id: tag
run: |
if [ "${{ github.ref }}" = "refs/heads/main" ]; then
echo "tag=latest" >> $GITHUB_OUTPUT
elif [ "${{ github.ref }}" = "refs/heads/dev" ]; then
echo "tag=dev" >> $GITHUB_OUTPUT
elif [ "${{ github.ref }}" = "refs/heads/test" ]; then
echo "tag=test" >> $GITHUB_OUTPUT
else
echo "tag=unknown" >> $GITHUB_OUTPUT
fi
- name: 登录 Docker Registry
run: |
echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login "${{ env.REGISTRY }}" --username "${{ secrets.REGISTRY_USERNAME }}" --password-stdin
- name: 构建 API 镜像
run: |
docker build -f Dockerfile.api -t ${{ env.API_IMAGE }}:${{ steps.tag.outputs.tag }} .
docker tag ${{ env.API_IMAGE }}:${{ steps.tag.outputs.tag }} ${{ env.API_IMAGE }}:${{ github.sha }}
- name: 构建 Worker 镜像
run: |
docker build -f Dockerfile.worker -t ${{ env.WORKER_IMAGE }}:${{ steps.tag.outputs.tag }} .
docker tag ${{ env.WORKER_IMAGE }}:${{ steps.tag.outputs.tag }} ${{ env.WORKER_IMAGE }}:${{ github.sha }}
- name: 推送镜像到 Registry
run: |
docker push ${{ env.API_IMAGE }}:${{ steps.tag.outputs.tag }}
docker push ${{ env.API_IMAGE }}:${{ github.sha }}
docker push ${{ env.WORKER_IMAGE }}:${{ steps.tag.outputs.tag }}
docker push ${{ env.WORKER_IMAGE }}:${{ github.sha }}
- name: 部署到本地(仅 main 分支)
if: github.ref == 'refs/heads/main'
run: |
# 确保部署目录存在
mkdir -p ${{ env.DEPLOY_DIR }}
# 复制 docker-compose.prod.yml 到部署目录
cp docker-compose.prod.yml ${{ env.DEPLOY_DIR }}/
cd ${{ env.DEPLOY_DIR }}
echo "拉取最新镜像..."
docker compose -f docker-compose.prod.yml pull
echo "执行滚动更新..."
docker compose -f docker-compose.prod.yml up -d --no-deps
echo "等待服务健康检查..."
sleep 10
echo "清理旧镜像(保留最近 3 个版本)..."
docker images ${{ env.API_IMAGE }} --format "{{.ID}}" | tail -n +4 | xargs -r docker rmi -f || true
docker images ${{ env.WORKER_IMAGE }} --format "{{.ID}}" | tail -n +4 | xargs -r docker rmi -f || true
echo "清理悬空镜像..."
docker image prune -f
echo "部署完成!"
docker compose -f docker-compose.prod.yml ps
- name: 构建结果通知
if: always()
run: |
if [ "${{ job.status }}" = "success" ]; then
echo "✅ 构建成功: ${{ steps.tag.outputs.tag }}"
echo "📦 镜像标签: ${{ github.sha }}"
else
echo "❌ 构建失败"
fi