name: github-actions-workflow description: 构建全面的GitHub Actions工作流,用于CI/CD、测试、安全和部署。掌握工作流、作业、步骤和条件执行。
GitHub Actions 工作流
概览
创建强大的GitHub Actions工作流,以自动化测试、构建、安全扫描和部署过程,直接从你的GitHub仓库进行。
何时使用
- 持续集成和测试
- 构建自动化
- 安全扫描和分析
- 依赖更新
- 自动化部署
- 版本管理
- 代码质量检查
实施示例
1. 完整的CI/CD工作流
# .github/workflows/ci.yml
name: CI/CD Pipeline
on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x, 18.x, 20.x]
steps:
- uses: actions/checkout@v3
- name: 设置Node ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: 安装依赖
run: npm ci
- name: 运行linter
run: npm run lint
- name: 运行测试
run: npm run test:coverage
- name: 上传覆盖率
uses: codecov/codecov-action@v3
build:
runs-on: ubuntu-latest
needs: test
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v3
- name: 设置Docker Buildx
uses: docker/setup-buildx-action@v2
- name: 登录到Registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: 提取元数据
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
- name: 构建并推送镜像
uses: docker/build-push-action@v4
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: 运行Trivy漏洞扫描器
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
scan-ref: '.'
format: 'sarif'
output: 'trivy-results.sarif'
- name: 将Trivy结果上传到GitHub安全标签页
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: 'trivy-results.sarif'
deploy:
runs-on: ubuntu-latest
needs: [test, build]
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- uses: actions/checkout@v3
- name: 部署到生产环境
run: |
回显“部署到生产环境...”
# 添加部署脚本
3. 自动化发布工作流
# .github/workflows/release.yml
name: 版本发布
on:
push:
tags:
- 'v*'
jobs:
create-release:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: 生成变更日志
id: changelog
uses: mikepenz/action-github-changelog-generator@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: 创建版本
uses: ncipollo/release-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ github.ref }}
body: ${{ steps.changelog.outputs.changelog }}
draft: false
- name: 发布到npm
uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.NPM_TOKEN }}
5. Docker构建和推送
name: Docker构建
on: [push]
jobs:
docker:
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- uses: actions/checkout@v3
- uses: docker/setup-buildx-action@v2
- uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}:latest
最佳实践
✅ DO
- 使用依赖缓存(npm, pip, Maven)
- 使用矩阵策略并行运行测试
- 在受保护的分支上需要状态检查
- 使用环境秘密和变量
- 使用
if:实现条件作业 - 在测试前进行lint和格式化
- 设置明确的权限与权限
- 使用特定硬件的运行器标签
- 缓存Docker层以加快构建速度
❌ DON’T
- 在工作流文件中存储秘密
- 在工作流中运行不受信任的代码
- 在来自fork的拉取请求中使用
secrets.* - 硬编码凭证或令牌
- 使用
continue-on-error错过错误处理 - 创建过于复杂的工作流
- 在拉取请求上跳过测试
秘密和变量
# 通过CLI设置秘密
gh secret set MY_SECRET --body "secret-value"
gh secret list
# 设置组织变量
gh variable set MY_VAR --body "value" --org myorg
工作流权限
permissions:
actions: read
contents: read
checks: write
pull-requests: write
security-events: write
packages: write