🚀 GitHub Actions 自动化工作流完全指南
💡 GitHub Actions 是 GitHub 的持续集成和持续部署(CI/CD)平台,允许开发者自动化构建、测试和部署代码。
📖 目录导航
🎯 GitHub Actions 简介
✨ 核心特性
特性 | 描述 | 优势 |
---|---|---|
🔄 自动化 | 代码推送自动触发工作流 | 提升开发效率 |
🌐 多平台 | 支持 Linux、Windows、macOS | 广泛的兼容性 |
🔌 生态丰富 | 丰富的预构建 Actions | 快速集成第三方服务 |
💰 免费配额 | 公共仓库免费使用 | 降低成本 |
🏗️ 基本概念
- Workflow(工作流): 自动化过程的完整流程
- Job(作业): 工作流中的一个执行单元
- Step(步骤): 作业中的单个任务
- Action(动作): 可重用的代码单元
🏗️ 工作流基础
📁 目录结构
.github/
└── workflows/
├── ci.yml # 持续集成
├── deploy.yml # 部署流程
└── test.yml # 测试流程
1
2
3
4
5
2
3
4
5
🔧 基础配置
yaml
name: CI/CD Pipeline
# 触发条件
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
# 作业定义
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
📝 配置文件详解
🎯 触发器配置
yaml
on:
# 推送触发
push:
branches: [ main, develop ]
paths: [ 'src/**' ]
tags: [ 'v*' ]
# PR 触发
pull_request:
branches: [ main ]
types: [ opened, synchronize ]
# 定时触发
schedule:
- cron: '0 2 * * *' # 每天凌晨2点
# 手动触发
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy'
required: true
default: 'staging'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
🔧 作业配置
yaml
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16, 18, 20]
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build project
run: npm run build
- name: Run tests
run: npm test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
🔧 常用操作
📥 代码检出
yaml
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0 # 获取完整历史
1
2
3
4
2
3
4
🔧 环境配置
yaml
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
registry-url: 'https://registry.npmjs.org'
1
2
3
4
5
6
2
3
4
5
6
🔒 密钥管理
yaml
- name: Deploy to production
run: |
echo "API_KEY=${{ secrets.API_KEY }}" >> $GITHUB_ENV
npm run deploy
env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
1
2
3
4
5
6
2
3
4
5
6
🌟 实战案例
🚀 前端项目 CI/CD
yaml
name: Frontend CI/CD
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run linter
run: npm run lint
- name: Run tests
run: npm run test:coverage
- name: Upload coverage
uses: codecov/codecov-action@v3
build:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build project
run: npm run build
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
if: github.ref == 'refs/heads/main'
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
🐳 Docker 部署
yaml
name: Docker Deploy
on:
push:
branches: [ main ]
tags: [ 'v*' ]
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v4
with:
images: myapp
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
💡 最佳实践
🎯 性能优化
缓存使用
yaml- name: Cache dependencies uses: actions/cache@v3 with: path: node_modules key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
1
2
3
4
5并行执行
yamlstrategy: matrix: os: [ubuntu-latest, windows-latest, macos-latest] node-version: [16, 18, 20]
1
2
3
4条件执行
yaml- name: Deploy to production if: github.ref == 'refs/heads/main' run: npm run deploy
1
2
3
🔒 安全考虑
- 密钥管理: 使用 GitHub Secrets 存储敏感信息
- 权限控制: 最小权限原则
- 代码审查: 对工作流文件进行审查
📊 监控和通知
yaml
- name: Notify on failure
if: failure()
uses: 8398a7/action-slack@v3
with:
status: failure
webhook_url: ${{ secrets.SLACK_WEBHOOK }}
1
2
3
4
5
6
2
3
4
5
6
🎯 总结
GitHub Actions 为现代软件开发提供了强大的自动化能力:
🚀 核心价值
- ⚡ 自动化: 减少手动操作,提升效率
- 🔄 持续集成: 确保代码质量
- 🚀 快速部署: 自动化部署流程
- 🌐 多平台: 支持各种运行环境
💡 应用建议
- 从简单开始: 先实现基本的 CI/CD 流程
- 逐步优化: 根据需求添加更多功能
- 监控和改进: 定期查看工作流执行情况
- 团队协作: 建立统一的工作流标准
掌握 GitHub Actions 将大幅提升开发效率和代码质量!🎉
🌟 自动化未来,高效开发 - 让 GitHub Actions 成为你的得力助手!