Files
docker_practice/21_case_devops/github_actions.md
2026-02-22 15:33:20 -08:00

54 lines
1.3 KiB
Go
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.

# 21.2 GitHub Actions
GitHub [Actions](https://github.com/features/actions) 是 GitHub 推出的一款 CI/CD 工具。
我们可以在每个 `job` `step` 中使用 Docker 执行构建步骤
## 21.2.1 最小可用示例
在仓库根目录创建 `/.github/workflows/ci.yml`
```yaml
name: CI
on:
push:
pull_request:
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
context: .
push: false
tags: local/test:ci
```
该示例会在 GitHub Actions 中构建当前仓库的 Docker 镜像不推送到 registry
## 21.2.2 最佳实践
* 固定 action 的主版本例如 `@v4` / `@v6`避免使用 `@master` 这类浮动引用
* 设置最小权限例如 `contents: read`需要写入权限时再打开
* 需要依赖缓存时优先使用官方支持的缓存方案例如针对语言包管理器的 cache BuildKit cache
如果你需要在某个步骤里直接运行容器镜像而不是构建镜像可以使用 `docker://` 语法
```yaml
- name: Run container step
uses: docker://golang:alpine
with:
args: go version
```
## 21.2.3 参考资料
* [Actions Docs](https://docs.github.com/en/actions)