GitHub Actions

分清维护者测试与应用测试

CLI 仓库流水线验证生成器、冻结迁移资产和新模板;生成应用的流水线则验证自身契约、业务代码与依赖。不能把前者通过当作所有业务项目可升级的证据。

以下为应用单测与构建示例,沿用 CLI 源码的工具/include 基线。本轮没有在业务应用执行此示例;采用前核对项目 Makefile 与版本。

单测与构建

保存为 .github/workflows/grpc-kit.yml:

name: 应用验证
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
permissions:
  contents: read
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - uses: actions/setup-go@v7
        with:
          go-version-file: go.mod
          cache-dependency-path: go.sum
      - name: 安装生成工具
        run: |
          echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH"
          make protoc
          make protoc-gen-go
          make protoc-gen-go-grpc
          make protoc-gen-grpc-gateway
          make protoc-gen-openapiv2
      - name: 安装 Proto include
        run: |
          TASK_GOPATH="$(go env GOPATH)"
          mkdir -p "$TASK_GOPATH/src/github.com/grpc-kit" "$TASK_GOPATH/src/github.com/googleapis" "$TASK_GOPATH/src/github.com/grpc-ecosystem"
          git clone --depth 1 --branch v0.3.1 https://github.com/grpc-kit/api.git "$TASK_GOPATH/src/github.com/grpc-kit/api"
          git init "$TASK_GOPATH/src/github.com/googleapis/googleapis"
          git -C "$TASK_GOPATH/src/github.com/googleapis/googleapis" remote add origin https://github.com/googleapis/googleapis.git
          git -C "$TASK_GOPATH/src/github.com/googleapis/googleapis" fetch --depth 1 origin e0d0106516a5c613510533821e4508bc6c943b11
          git -C "$TASK_GOPATH/src/github.com/googleapis/googleapis" checkout --detach FETCH_HEAD
          git clone --depth 1 --branch v2.30.0 https://github.com/grpc-ecosystem/grpc-gateway.git "$TASK_GOPATH/src/github.com/grpc-ecosystem/grpc-gateway"
      - run: make test
      - run: make build
      - name: 检查 diff 格式
        run: git diff --check

git diff --check 只检查空白错误,不证明生成结果没有变化。团队可另设生成文件一致性门禁,并明确版本脚本生成的可预期变化。

加入黑盒 E2E

应用模板的 make test-e2e 使用 e2e build tag,必须有独立运行的服务。CI 应提供开发配置及数据库等依赖、启动构建物、等待就绪,再执行测试并在退出时清理进程;不要把配置或生产凭据打印到日志。

默认地址为 E2E_BASE_URL=http://127.0.0.1:8080,服务名通过 E2E_SERVICE_CODE 与实际配置一致;Basic/Bearer 凭据按项目 fixture 配置。忽略 Git 的 config/app-dev-local.yaml 不会自动出现在 checkout 中。依赖检查与退出验证见部署检查。

维护 CLI 时

除 make test 与 make build 外,还需验证 make test-compatibility PKG_COMPAT_VERSION=v0.5.0、候选 pkg v0.5.1,以及 make test-template-compatibility。当前 include 使用 API v0.3.1;升级 API v0.4.1 必须另做验证,参见版本与兼容。

来源:CLI Test 流水线。