golang-continuous-integration
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePersona: You are a Go DevOps engineer. You treat CI as a quality gate — every pipeline decision is weighed against build speed, signal reliability, and security posture.
Modes:
- Setup — adding CI to a project for the first time: start with the Quick Reference table, then generate workflows in this order: test → lint → security → release. Always check latest action versions before writing YAML.
- Improve — auditing or extending an existing pipeline: read current workflow files first, identify gaps against the Quick Reference table, then propose targeted additions without duplicating existing steps.
角色定位: 你是一名Go DevOps工程师,将CI视为质量门禁——每一项流水线决策都需要权衡构建速度、信号可靠性和安全态势。
模式:
- 搭建模式——首次为项目添加CI:先参考快速参考表,再按以下顺序生成工作流:测试 → 代码检查 → 安全 → 发布。编写YAML前务必确认各Action的最新版本。
- 优化模式——审计或扩展现有流水线:先读取当前工作流文件,对照快速参考表找出缺口,然后针对性地添加内容,避免重复现有步骤。
Go Continuous Integration
Go持续集成
Set up production-grade CI/CD pipelines for Go projects using GitHub Actions.
使用GitHub Actions为Go项目搭建生产级CI/CD流水线。
Action Versions
Action版本
The versions shown in the examples below are reference versions that may be outdated. Before generating workflow files, search the internet for the latest stable major version of each GitHub Action used (e.g., , , , , , etc.). Use the latest version you find, not the one hardcoded in the examples.
actions/checkoutactions/setup-gogolangci/golangci-lint-actioncodecov/codecov-actiongoreleaser/goreleaser-action以下示例中展示的版本为参考版本,可能已过时。生成工作流文件前,请先查询所使用的每个GitHub Action的最新稳定主版本(例如:、、、、等)。请使用你找到的最新版本,而非示例中硬编码的版本。
actions/checkoutactions/setup-gogolangci/golangci-lint-actioncodecov/codecov-actiongoreleaser/goreleaser-actionQuick Reference
快速参考
| Stage | Tool | Purpose |
|---|---|---|
| Test | | Unit + race detection |
| Coverage | | Coverage reporting |
| Lint | | Comprehensive linting |
| Vet | | Built-in static analysis |
| SAST | | Security static analysis |
| Vuln scan | | Known vulnerability detection |
| Docker | | Multi-platform image builds |
| Deps | Dependabot / Renovate | Automated dependency updates |
| Release | GoReleaser | Automated binary releases |
| 阶段 | 工具 | 用途 |
|---|---|---|
| 测试 | | 单元测试 + 竞态检测 |
| 覆盖率 | | 覆盖率报告 |
| 代码检查 | | 全面代码检查 |
| 代码校验 | | 内置静态分析 |
| 静态应用安全测试 | | 安全静态分析 |
| 漏洞扫描 | | 已知漏洞检测 |
| Docker构建 | | 多平台镜像构建 |
| 依赖管理 | Dependabot / Renovate | 自动化依赖更新 |
| 发布 | GoReleaser | 自动化二进制发布 |
Testing
测试
.github/workflows/test.ymlAdapt the Go version matrix to match :
go.modgo 1.23 → matrix: ["1.23", "1.24", "1.25", "1.26", "stable"]
go 1.24 → matrix: ["1.24", "1.25", "1.26", "stable"]
go 1.25 → matrix: ["1.25", "1.26", "stable"]
go 1.26 → matrix: ["1.26", "stable"]Use so a failure on one Go version doesn't cancel the others.
fail-fast: falseTest flags:
- : CI MUST run tests with the
-raceflag (catches data races — undefined behavior in Go)-race - : Randomize test order to catch inter-test dependencies
-shuffle=on - : Generate coverage data
-coverprofile - : Fails if
git diff --exit-codechanges anythinggo mod tidy
.github/workflows/test.yml根据适配Go版本矩阵:
go.modgo 1.23 → matrix: ["1.23", "1.24", "1.25", "1.26", "stable"]
go 1.24 → matrix: ["1.24", "1.25", "1.26", "stable"]
go 1.25 → matrix: ["1.25", "1.26", "stable"]
go 1.26 → matrix: ["1.26", "stable"]设置,避免某一个Go版本测试失败就取消其他版本的测试。
fail-fast: false测试参数:
- : CI必须使用
-race参数运行测试(捕获数据竞态——Go中的未定义行为)-race - : 随机化测试顺序,捕获测试间的依赖问题
-shuffle=on - : 生成覆盖率数据
-coverprofile - : 如果
git diff --exit-code修改了任何内容则失败go mod tidy
Coverage Configuration
覆盖率配置
CI SHOULD enforce code coverage thresholds. Configure thresholds in at the repo root — see codecov.yml
codecov.ymlCI应强制代码覆盖率阈值。在仓库根目录的中配置阈值——查看 codecov.yml
codecov.ymlIntegration Tests
集成测试
.github/workflows/integration.ymlUse to disable test caching — cached results can hide flaky service interactions.
-count=1.github/workflows/integration.yml使用禁用测试缓存——缓存的结果可能会掩盖不稳定的服务交互问题。
-count=1Linting
代码检查
golangci-lint.github/workflows/lint.ymlgolangci-lint.github/workflows/lint.ymlgolangci-lint Configuration
golangci-lint配置
Create at the root of the project. See the skill for the recommended configuration.
.golangci.ymlsamber/cc-skills-golang@golang-linter在项目根目录创建。推荐配置可参考技能。
.golangci.ymlsamber/cc-skills-golang@golang-linterSecurity & SAST
安全与静态应用安全测试
.github/workflows/security.ymlCI MUST run . It only reports vulnerabilities in code paths your project actually calls — unlike generic CVE scanners. CodeQL results appear in the repository's Security tab. Bearer is good at detecting sensitive data flow issues.
govulncheck.github/workflows/security.ymlCI必须运行。它仅报告项目实际调用的代码路径中的漏洞——不同于通用CVE扫描器。CodeQL结果显示在仓库的安全标签页中。Bearer擅长检测敏感数据流问题。
govulncheckCodeQL Configuration
CodeQL配置
Create to use the extended security query suite — see codeql-config.yml
.github/codeql/codeql-config.ymlAvailable query suites:
- default: Standard security queries
- security-extended: Extra security queries with slightly lower precision
- security-and-quality: Security queries plus maintainability and reliability checks
创建以使用扩展安全查询套件——查看 codeql-config.yml
.github/codeql/codeql-config.yml可用的查询套件:
- default: 标准安全查询
- security-extended: 额外安全查询,精度略低
- security-and-quality: 安全查询加上可维护性和可靠性检查
Container Image Scanning
容器镜像扫描
If the project produces Docker images, Trivy container scanning is included in the Docker workflow — see docker.yml
如果项目生成Docker镜像,Docker工作流中包含Trivy容器扫描——查看 docker.yml
Dependency Management
依赖管理
Dependabot
Dependabot
.github/dependabot.ymlMinor/patch updates are grouped into a single PR. Major updates get individual PRs since they may have breaking changes.
.github/dependabot.yml次要/补丁更新会合并到单个PR中。主版本更新会生成单独的PR,因为它们可能包含破坏性变更。
Auto-Merge for Dependabot
Dependabot自动合并
.github/workflows/dependabot-auto-merge.ymlSecurity warning: This workflow requiresandcontents: write— these are elevated permissions that allow merging PRs and modifying repository content. Thepull-requests: writeguard restricts execution to Dependabot only. Do not remove this guard. Note thatif: github.actor == 'dependabot[bot]'checks are not fully spoof-proof — branch protection rules are the real safety net. Ensure branch protection is configured (see Repository Security Settings) with required status checks and required approvals so that auto-merge only succeeds after all checks pass, regardless of who triggered the workflow.github.actor
.github/workflows/dependabot-auto-merge.yml安全警告: 此工作流需要和contents: write权限——这些是高级权限,允许合并PR和修改仓库内容。pull-requests: write限制仅允许Dependabot执行。请勿移除该限制。注意if: github.actor == 'dependabot[bot]'检查并非完全无法伪造——分支保护规则才是真正的安全保障。确保配置了分支保护(查看 仓库安全设置),包含必需的状态检查和必需的审批,以便自动合并仅在所有检查通过后才能成功,无论谁触发了工作流。github.actor
Renovate (alternative)
Renovate(替代方案)
Renovate is a more mature and configurable alternative to Dependabot. It supports automerge natively, grouping, scheduling, regex managers, and monorepo-aware updates. If Dependabot feels too limited, Renovate is the go-to choice.
Key advantages over Dependabot:
- : Automatically runs
gomodTidyafter updatesgo mod tidy - Native automerge: No separate workflow needed
- Better grouping: More flexible rules for grouping PRs
- Regex managers: Can update versions in Dockerfiles, Makefiles, etc.
- Monorepo support: Handles Go workspaces and multi-module repos
Renovate是Dependabot的更成熟、可配置性更强的替代方案。它原生支持自动合并、分组、调度、正则管理器,以及感知单体仓库的更新。如果Dependabot功能不足,Renovate是首选。
相对于Dependabot的主要优势:
- : 更新后自动运行
gomodTidygo mod tidy - 原生自动合并: 无需单独的工作流
- 更灵活的分组: 分组PR的规则更灵活
- 正则管理器: 可更新Dockerfile、Makefile等文件中的版本
- 单体仓库支持: 处理Go工作区和多模块仓库
Release Automation
发布自动化
GoReleaser automates binary builds, checksums, and GitHub Releases. The configuration varies significantly depending on the project type.
GoReleaser自动化二进制构建、校验和以及GitHub Releases。配置会因项目类型而异。
Release Workflow
发布工作流
.github/workflows/release.ymlSecurity warning: This workflow requiresto create GitHub Releases. It is restricted to tag pushes (contents: write) so it cannot be triggered by pull requests or branch pushes. Only users with push access to the repository can create tags.tags: ["v*"]
.github/workflows/release.yml安全警告: 此工作流需要权限来创建GitHub Releases。它仅在推送标签时触发(contents: write),因此无法由拉取请求或分支推送触发。只有拥有仓库推送权限的用户才能创建标签。tags: ["v*"]
GoReleaser for CLI/Programs
用于CLI/程序的GoReleaser
Programs need cross-compiled binaries, archives, and optionally Docker images.
.goreleaser.yml程序需要跨编译二进制文件、归档文件,可选Docker镜像。
.goreleaser.ymlGoReleaser for Libraries
用于库的GoReleaser
Libraries don't produce binaries — they only need a GitHub Release with a changelog. Use a minimal config that skips the build.
.goreleaser.ymlFor libraries, you may not even need GoReleaser — a simple GitHub Release created via the UI or is often sufficient.
gh release create库不生成二进制文件——只需要带有变更日志的GitHub Release。使用最小化配置跳过构建步骤。
.goreleaser.yml对于库,你甚至可能不需要GoReleaser——通过UI或创建简单的GitHub Release通常就足够了。
gh release createGoReleaser for Monorepos / Multi-Binary
用于单体仓库/多二进制文件的GoReleaser
When a repository contains multiple commands (e.g., , ).
cmd/api/cmd/worker/.goreleaser.yml当仓库包含多个命令时(例如:、)。
cmd/api/cmd/worker/.goreleaser.ymlDocker Build & Push
Docker构建与推送
For projects that produce Docker images. This workflow builds multi-platform images, generates SBOM and provenance attestations, pushes to both GitHub Container Registry (GHCR) and Docker Hub, and includes Trivy container scanning.
.github/workflows/docker.ymlSecurity warning: Permissions are scoped per job: thejob only getscontainer-scan+contents: read, while thesecurity-events: writejob getsdocker(to push to GHCR) andpackages: write+attestations: write(for provenance/SBOM signing). This ensures the scan job cannot push images even if compromised. Theid-token: writeflag is set topushon pull requests so untrusted code cannot publish images. ThefalseandDOCKERHUB_USERNAMEsecrets must be configured in the repository secrets settings — never hardcode credentials.DOCKERHUB_TOKEN
Key details:
- QEMU + Buildx: Required for multi-platform builds (). Remove platforms you don't need.
linux/amd64,linux/arm64 - on PRs: Images are built but never pushed on pull requests — this validates the Dockerfile without publishing untrusted code.
push: false - Metadata action: Automatically generates semver tags (→
v1.2.3,1.2.3,1.2), branch tags (1), and SHA tags.main - Provenance + SBOM: and
provenance: mode=maxgenerate supply chain attestations. These requiresbom: trueandattestations: writepermissions.id-token: write - Dual registry: Pushes to both GHCR (using , no extra secret needed) and Docker Hub (requires
GITHUB_TOKEN+DOCKERHUB_USERNAMEsecrets). Remove the Docker Hub login and image line if not needed.DOCKERHUB_TOKEN - Trivy: Scans the built image for CRITICAL and HIGH vulnerabilities and uploads results to the Security tab.
- Adapt the image names and registries to your project. For GHCR-only, remove the Docker Hub login step and the line from
docker.io/.images:
适用于生成Docker镜像的项目。此工作流构建多平台镜像,生成SBOM和来源证明声明,推送到GitHub容器注册表(GHCR)和Docker Hub,并包含Trivy容器扫描。
.github/workflows/docker.yml安全警告: 权限按作业划分范围:作业仅获得container-scan+contents: read权限,而security-events: write作业获得docker(推送到GHCR)和packages: write+attestations: write(用于来源/SBOM签名)权限。这确保即使扫描作业被攻陷,也无法推送镜像。拉取请求中的id-token: write标志设置为push,因此不可信代码无法发布镜像。false和DOCKERHUB_USERNAME机密必须在仓库机密设置中配置——切勿硬编码凭据。DOCKERHUB_TOKEN
关键细节:
- QEMU + Buildx: 多平台构建()所需。移除不需要的平台。
linux/amd64,linux/arm64 - 拉取请求中: 拉取请求中仅构建镜像但不推送——这会验证Dockerfile,但不会发布不可信代码。
push: false - 元数据Action: 自动生成语义化版本标签(→
v1.2.3,1.2.3,1.2)、分支标签(1)和SHA标签。main - 来源证明 + SBOM: 和
provenance: mode=max生成供应链声明。这些需要sbom: true和attestations: write权限。id-token: write - 双注册表: 推送到GHCR(使用,无需额外机密)和Docker Hub(需要
GITHUB_TOKEN+DOCKERHUB_USERNAME机密)。如果不需要,移除Docker Hub登录步骤和DOCKERHUB_TOKEN中的images:行。docker.io/ - Trivy: 扫描构建的镜像中的CRITICAL和HIGH漏洞,并将结果上传到安全标签页。
- 根据项目调整镜像名称和注册表。如果仅使用GHCR,移除Docker Hub登录步骤和中的
images:行。docker.io/
Repository Security Settings
仓库安全设置
After creating workflow files, ALWAYS tell the developer to configure GitHub repository settings (branch protection, workflow permissions, secrets, environments) — see repo-security.md
创建工作流文件后,务必告知开发者配置GitHub仓库设置(分支保护、工作流权限、机密、环境)——查看 repo-security.md
Common Mistakes
常见错误
| Mistake | Fix |
|---|---|
Missing | Always use |
No | Randomize test order to catch inter-test dependencies |
| Caching integration test results | Use |
| Add |
Missing | One Go version failing shouldn't cancel other jobs |
| Not pinning action versions | GitHub Actions MUST use pinned major versions (e.g. |
No | Follow least-privilege per job |
| Ignoring govulncheck findings | Fix or suppress with justification |
| 错误 | 修复方案 |
|---|---|
CI测试中缺少 | 始终使用 |
未设置 | 随机化测试顺序,捕获测试间的依赖问题 |
| 缓存集成测试结果 | 使用 |
未检查 | 添加 |
未设置 | 一个Go版本测试失败不应取消其他作业 |
| 未固定Action版本 | GitHub Actions必须使用固定的主版本(例如 |
无 | 遵循作业最小权限原则 |
| 忽略govulncheck结果 | 修复或提供理由后忽略 |
Related Skills
相关技能
See , , , skills.
samber/cc-skills-golang@golang-lintersamber/cc-skills-golang@golang-securitysamber/cc-skills-golang@golang-testingsamber/cc-skills-golang@golang-dependency-management查看、、、技能。
samber/cc-skills-golang@golang-lintersamber/cc-skills-golang@golang-securitysamber/cc-skills-golang@golang-testingsamber/cc-skills-golang@golang-dependency-management