Persona: 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 Continuous Integration
Set up production-grade CI/CD pipelines for Go projects using GitHub Actions.
Action Versions
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., actions/checkout, actions/setup-go, golangci/golangci-lint-action, codecov/codecov-action, goreleaser/goreleaser-action, etc.). Use the latest version you find, not the one hardcoded in the examples.
Quick Reference
| Stage | Tool | Purpose |
|---|
| Test | INLINECODE5 | Unit + race detection |
| Coverage |
codecov/codecov-action | Coverage reporting |
|
Lint |
golangci-lint | Comprehensive linting |
|
Vet |
go vet | Built-in static analysis |
|
SAST |
gosec,
CodeQL,
Bearer | Security static analysis |
|
Vuln scan |
govulncheck | Known vulnerability detection |
|
Docker |
docker/build-push-action | Multi-platform image builds |
|
Deps | Dependabot / Renovate | Automated dependency updates |
|
Release | GoReleaser | Automated binary releases |
Testing
INLINECODE14 — see test.yml
Adapt the Go version matrix to match go.mod:
CODEBLOCK0
Use fail-fast: false so a failure on one Go version doesn't cancel the others.
Test flags:
- -
-race: CI MUST run tests with the -race flag (catches data races — undefined behavior in Go) - INLINECODE19 : Randomize test order to catch inter-test dependencies
- INLINECODE20 : Generate coverage data
- INLINECODE21 : Fails if
go mod tidy changes anything
Coverage Configuration
CI SHOULD enforce code coverage thresholds. Configure thresholds in codecov.yml at the repo root — see codecov.yml
Integration Tests
INLINECODE24 — see integration.yml
Use -count=1 to disable test caching — cached results can hide flaky service interactions.
Linting
INLINECODE26 MUST be run in CI on every PR. .github/workflows/lint.yml — see lint.yml
golangci-lint Configuration
Create .golangci.yml at the root of the project. See the samber/cc-skills-golang@golang-linter skill for the recommended configuration.
Security & SAST
INLINECODE30 — see security.yml
CI MUST run govulncheck. 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.
CodeQL Configuration
Create .github/codeql/codeql-config.yml to use the extended security query suite — see codeql-config.yml
Available 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
Container Image Scanning
If the project produces Docker images, Trivy container scanning is included in the Docker workflow — see docker.yml
Dependency Management
Dependabot
INLINECODE33 — see dependabot.yml
Minor/patch updates are grouped into a single PR. Major updates get individual PRs since they may have breaking changes.
Auto-Merge for Dependabot
INLINECODE34 — see dependabot-auto-merge.yml
Security warning: This workflow requires contents: write and pull-requests: write — these are elevated permissions that allow merging PRs and modifying repository content. The if: github.actor == 'dependabot[bot]' guard restricts execution to Dependabot only. Do not remove this guard. Note that github.actor 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.
Renovate (alternative)
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.
Install the Renovate GitHub App, then create renovate.json at the repo root — see renovate.json
Key advantages over Dependabot:
- -
gomodTidy: Automatically runs go mod tidy after updates - 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
Release Automation
GoReleaser automates binary builds, checksums, and GitHub Releases. The configuration varies significantly depending on the project type.
Release Workflow
INLINECODE42 — see release.yml
Security warning: This workflow requires contents: write to create GitHub Releases. It is restricted to tag pushes (tags: ["v*"]) so it cannot be triggered by pull requests or branch pushes. Only users with push access to the repository can create tags.
GoReleaser for CLI/Programs
Programs need cross-compiled binaries, archives, and optionally Docker images.
INLINECODE45 — see goreleaser-cli.yml
GoReleaser for Libraries
Libraries don't produce binaries — they only need a GitHub Release with a changelog. Use a minimal config that skips the build.
INLINECODE46 — see goreleaser-lib.yml
For libraries, you may not even need GoReleaser — a simple GitHub Release created via the UI or gh release create is often sufficient.
GoReleaser for Monorepos / Multi-Binary
When a repository contains multiple commands (e.g., cmd/api/, cmd/worker/).
INLINECODE50 — see goreleaser-monorepo.yml
Docker Build & Push
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.
INLINECODE51 — see docker.yml
Security warning: Permissions are scoped per job: the container-scan job only gets contents: read + security-events: write, while the docker job gets packages: write (to push to GHCR) and attestations: write + id-token: write (for provenance/SBOM signing). This ensures the scan job cannot push images even if compromised. The push flag is set to false on pull requests so untrusted code cannot publish images. The DOCKERHUB_USERNAME and DOCKERHUB_TOKEN secrets must be configured in the repository secrets settings — never hardcode credentials.
Key details:
- - QEMU + Buildx: Required for multi-platform builds (
linux/amd64,linux/arm64). Remove platforms you don't need. push: false on PRs: Images are built but never pushed on pull requests — this validates the Dockerfile without publishing untrusted code.- Metadata action: Automatically generates semver tags (
v1.2.3 → 1.2.3, 1.2, 1), branch tags (main), and SHA tags. - Provenance + SBOM:
provenance: mode=max and sbom: true generate supply chain attestations. These require attestations: write and id-token: write permissions. - Dual registry: Pushes to both GHCR (using
GITHUB_TOKEN, no extra secret needed) and Docker Hub (requires DOCKERHUB_USERNAME + DOCKERHUB_TOKEN secrets). Remove the Docker Hub login and image line if not needed. - 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
docker.io/ line from images:.
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
Common Mistakes
| Mistake | Fix |
|---|
Missing -race in CI tests | Always use INLINECODE80 |
| No INLINECODE81 |
Randomize test order to catch inter-test dependencies |
| Caching integration test results | Use
-count=1 to disable caching |
|
go mod tidy not checked | Add
go mod tidy && git diff --exit-code step |
| Missing
fail-fast: false | One Go version failing shouldn't cancel other jobs |
| Not pinning action versions | GitHub Actions MUST use pinned major versions (e.g.
@vN, not
@master) |
| No
permissions block | Follow least-privilege per job |
| Ignoring govulncheck findings | Fix or suppress with justification |
Related Skills
See samber/cc-skills-golang@golang-linter, samber/cc-skills-golang@golang-security, samber/cc-skills-golang@golang-testing, samber/cc-skills-golang@golang-dependency-management skills.
角色定位: 你是一名Go DevOps工程师。你将CI视为质量门禁——每个流水线决策都要权衡构建速度、信号可靠性和安全态势。
模式:
- - 搭建 — 首次为项目添加CI:从快速参考表开始,然后按以下顺序生成工作流:测试 → 代码检查 → 安全 → 发布。在编写YAML前始终检查最新的Action版本。
- 改进 — 审计或扩展现有流水线:先读取当前工作流文件,对照快速参考表识别差距,然后在不重复现有步骤的前提下提出针对性补充。
Go持续集成
使用GitHub Actions为Go项目搭建生产级CI/CD流水线。
Action版本
以下示例中显示的版本是参考版本,可能已过时。在生成工作流文件之前,请搜索互联网上每个GitHub Action的最新稳定主版本(例如actions/checkout、actions/setup-go、golangci/golangci-lint-action、codecov/codecov-action、goreleaser/goreleaser-action等)。使用你找到的最新版本,而不是示例中硬编码的版本。
快速参考
| 阶段 | 工具 | 用途 |
|---|
| 测试 | go test -race | 单元测试 + 竞态检测 |
| 覆盖率 |
codecov/codecov-action | 覆盖率报告 |
|
代码检查 | golangci-lint | 全面代码检查 |
|
静态分析 | go vet | 内置静态分析 |
|
SAST | gosec、CodeQL、Bearer | 安全静态分析 |
|
漏洞扫描 | govulncheck | 已知漏洞检测 |
|
Docker | docker/build-push-action | 多平台镜像构建 |
|
依赖 | Dependabot / Renovate | 自动化依赖更新 |
|
发布 | GoReleaser | 自动化二进制发布 |
测试
.github/workflows/test.yml — 参见 test.yml
根据go.mod调整Go版本矩阵:
go 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]
使用fail-fast: false,这样某个Go版本的失败不会取消其他版本。
测试标志:
- - -race:CI必须使用-race标志运行测试(捕获数据竞态——Go中的未定义行为)
- -shuffle=on:随机化测试顺序以捕获测试间依赖
- -coverprofile:生成覆盖率数据
- git diff --exit-code:如果go mod tidy更改了任何内容则失败
覆盖率配置
CI应强制执行代码覆盖率阈值。在仓库根目录的codecov.yml中配置阈值 — 参见 codecov.yml
集成测试
.github/workflows/integration.yml — 参见 integration.yml
使用-count=1禁用测试缓存——缓存结果可能隐藏不稳定的服务交互。
代码检查
golangci-lint必须在CI中对每个PR运行。.github/workflows/lint.yml — 参见 lint.yml
golangci-lint配置
在项目根目录创建.golangci.yml。推荐配置请参见samber/cc-skills-golang@golang-linter技能。
安全与SAST
.github/workflows/security.yml — 参见 security.yml
CI必须运行govulncheck。它只报告项目实际调用的代码路径中的漏洞——与通用CVE扫描器不同。CodeQL结果显示在仓库的“安全”选项卡中。Bearer擅长检测敏感数据流问题。
CodeQL配置
创建.github/codeql/codeql-config.yml以使用扩展安全查询套件 — 参见 codeql-config.yml
可用的查询套件:
- - default:标准安全查询
- security-extended:额外安全查询,精度略低
- security-and-quality:安全查询加上可维护性和可靠性检查
容器镜像扫描
如果项目生成Docker镜像,Trivy容器扫描包含在Docker工作流中 — 参见 docker.yml
依赖管理
Dependabot
.github/dependabot.yml — 参见 dependabot.yml
次要/补丁更新合并为一个PR。主要更新获得单独的PR,因为它们可能包含破坏性变更。
Dependabot自动合并
.github/workflows/dependabot-auto-merge.yml — 参见 dependabot-auto-merge.yml
安全警告: 此工作流需要contents: write和pull-requests: write——这些是提升的权限,允许合并PR和修改仓库内容。if: github.actor == dependabot[bot]守卫将执行限制为仅Dependabot。不要移除此守卫。请注意,github.actor检查并非完全防欺骗——分支保护规则才是真正的安全网。确保配置了分支保护(参见仓库安全设置),包含必需的状态检查和必需的审批,这样自动合并只有在所有检查通过后才成功,无论谁触发了工作流。
Renovate(替代方案)
Renovate是Dependabot更成熟且可配置的替代方案。它原生支持自动合并、分组、调度、正则表达式管理器以及单体仓库感知更新。如果Dependabot感觉过于受限,Renovate是首选。
安装Renovate GitHub应用,然后在仓库根目录创建renovate.json — 参见 renovate.json
相对于Dependabot的主要优势:
- - gomodTidy:更新后自动运行go mod tidy
- 原生自动合并:无需单独的工作流
- 更好的分组:更灵活的分组PR规则
- 正则表达式管理器:可以更新Dockerfile、Makefile等中的版本
- 单体仓库支持:处理Go工作区和多模块仓库
发布自动化
GoReleaser自动化二进制构建、校验和和GitHub发布。配置根据项目类型差异很大。
发布工作流
.github/workflows/release.yml — 参见 release.yml
安全警告: 此工作流需要contents: write来创建GitHub发布。它被限制为标签推送(tags: [v*]),因此不能由拉取请求或分支推送触发。只有具有仓库推送权限的用户才能创建标签。
用于CLI/程序的GoReleaser
程序需要交叉编译的二进制文件、归档文件,以及可选的Docker镜像。
.goreleaser.yml — 参见 goreleaser-cli.yml
用于库的GoReleaser
库不生成二进制文件——它们只需要一个带有变更日志的GitHub发布。使用跳过构建的最小配置。
.goreleaser.yml — 参见 goreleaser-lib.yml
对于库,你可能甚至不需要GoReleaser——通过UI或gh release create创建的简单GitHub发布通常就足够了。
用于单体仓库/多二进制文件的GoReleaser
当仓库包含多个命令时(例如cmd/api/、cmd/worker/)。
.goreleaser.yml — 参见 goreleaser-monorepo.yml
Docker构建与推送
适用于生成Docker镜像的项目。此工作流构建多平台镜像,生成SBOM和出处证明,推送到GitHub容器注册表(GHCR)和Docker Hub,并包含Trivy容器扫描。
.github/workflows/docker.yml — 参见 docker.yml
安全警告: 权限按作业范围划分:container-scan作业仅获得contents: read + security-events: write,而docker作业获得packages: write(推送到GHCR)和attestations: write + id-token: write(用于出处/SBOM签名)。这确保即使扫描作业被攻破也无法推送镜像。在拉取请求上push标志设置为false,因此不受信任的代码无法发布镜像。DOCKERHUBUSERNAME和DOCKERHUBTOKEN