CI/CD

使用 GitHub Actions,配置在 .github/workflows/pipeline.yml

流水线

PR 触发

对 main 分支的 Pull Request 触发:

  1. Lintgo vet ./...
  2. Test:启动 PostgreSQL + Redis service container,运行 go test -v ./...

测试使用 GitHub Actions 的 service container 功能,自动启动 PostgreSQL 和 Redis,测试完成后自动销毁,不需要外部数据库和缓存。

Push main 触发

代码合并到 main 后,在 lint + test 通过的基础上:

  1. Build & Push:构建 Docker 镜像,推送到 GitHub Container Registry (ghcr.io)

镜像标签:

  • sha-<commit-hash>:每次提交一个标签
  • latest:最新标签

镜像使用

# 登录 GHCR
docker login ghcr.io -u <username> -p <github-token>

# 拉取镜像
docker pull ghcr.io/<owner>/hr:latest

# 运行
docker run -d \
  -p 8080:8080 \
  -e DB_HOST=your-db-host \
  -e DB_PASSWORD=your-password \
  ghcr.io/<owner>/hr:latest

自部署

如果不用 GHCR,也可以在服务器上直接从源码构建:

git clone <repo-url>
cd hr
docker compose up -d --build