GitHub Actions 完整教程:从 0 到 1 自动化部署
# GitHub Actions 完整教程:从 0 到 1 自动化部署(5 大实战) > GitHub Actions 是 2025 年最流行 CI/CD——**85% 开源项目在用**。本文**完整教程** + 5 大实战 + 避坑指南。 > **读完保证**:能 1 小时内搭好 CI/CD + 自动部署 + 节省 50% 运维时间。 ## 一、为什
GitHub Actions 完整教程:从 0 到 1 自动化部署(5 大实战)
GitHub Actions 是 2025 年最流行 CI/CD——85% 开源项目在用。本文完整教程 + 5 大实战 + 避坑指南。
读完保证:能 1 小时内搭好 CI/CD + 自动部署 + 节省 50% 运维时间。
一、为什么 GitHub Actions 重要?
1.1 数据
2025 年 CI/CD 工具市场:
- GitHub Actions:85% 开源项目在用
- GitLab CI:10%
- Jenkins:5%
- 其他:5%
优势:
- 与 GitHub 完美集成
- 2,000+ 官方 Actions
- 免费(公开仓库)/ 便宜(私有)
1.2 真实案例
我们的 CI/CD 改造:
- 之前:手动部署 = 30 分钟 / 次
- 之后:自动部署 = 5 分钟(自动测试 + 构建 + 部署)
- 节省:6 倍
二、5 分钟上手
Step 1:创建仓库(1 分钟)
# 在 GitHub 创建仓库
# 或本地初始化
git init
git remote add origin https://github.com/your/repo.git
Step 2:创建 .github/workflows/(1 分钟)
mkdir -p .github/workflows
touch .github/workflows/ci.yml
Step 3:第一个 workflow(2 分钟)
# .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- run: pip install -r requirements.txt
- run: pytest
Step 4:提交并推送(1 分钟)
git add .github/workflows/ci.yml
git commit -m "Add CI workflow"
git push
→ GitHub Actions 自动运行!打开仓库 → Actions → 查看结果
三、5 大实战场景
场景 1:Python 项目 CI(最常用)
# .github/workflows/python-ci.yml
name: Python CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12']
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Cache pip
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest pytest-cov
- name: Test with pytest
run: |
pytest --cov=./ --cov-report=xml
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
file: ./coverage.xml
场景 2:Node.js / TypeScript CI
# .github/workflows/node-ci.yml
name: Node.js CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm run lint
- run: npm run test
- run: npm run build
场景 3:自动部署到服务器
# .github/workflows/deploy.yml
name: Deploy to Production
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Deploy to server
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /app
git pull
docker compose restart
场景 4:Docker 镜像构建 + 推送
# .github/workflows/docker.yml
name: Docker Image
on:
push:
branches: [main]
tags: ['v*']
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: |
yourname/app:latest
yourname/app:${{ github.sha }}
场景 5:自动发布到 PyPI
# .github/workflows/pypi.yml
name: Publish to PyPI
on:
push:
tags: ['v*']
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Build
run: |
python -m pip install build
python -m build
- name: Publish
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
四、5 大常见错误
错误 1:没用缓存
错:每次重新装依赖(5 分钟)
对:用 actions/cache(30 秒)
错误 2:硬编码密钥
# 错
env:
API_KEY: sk-1234567890
# 对
env:
API_KEY: ${{ secrets.API_KEY }}
错误 3:单点失败
# 错:一个 step 失败 = 整个 workflow 失败
# 对:continue-on-error: true 或允许手动重试
错误 4:不缓存 Docker
# 错:每次重新构建镜像(10 分钟)
# 对:缓存 Docker layer(2 分钟)
- uses: actions/cache@v4
with:
path: /tmp/.docker-cache
key: ${{ runner.os }}-docker-${{ hashFiles('**/Dockerfile') }}
错误 5:太多 Jobs
错:1 个 workflow 跑 50 个 job
对:拆分多个 workflow,按需触发
五、3 大真实案例
案例 1:Python SaaS 项目
我们的 CI/CD:
- PR 时跑测试(5 分钟)
- main 合并时部署(10 分钟)
- 节省:每天 1 小时
案例 2:Node.js Web 应用
CI/CD 流程:
- 推送触发测试
- 合并触发部署
- 失败自动回滚
- 节省:80% 部署时间
案例 3:AI 服务(Python)
完整 pipeline:
- Lint → Test → Build → Deploy → Monitor
- 12 个 step
- 15 分钟完成
六、5 大必学 Action
1. actions/checkout
- uses: actions/checkout@v4
2. actions/setup-*
- uses: actions/setup-python@v5
- uses: actions/setup-node@v4
- uses: actions/setup-java@v4
3. actions/cache
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
4. actions/upload-artifact
- uses: actions/upload-artifact@v4
with:
name: build-output
path: dist/
5. 第三方 Actions
- appleboy/ssh-action:SSH 部署
- docker/build-push-action:Docker 镜像
- pypa/gh-action-pypi-publish:PyPI 发布
- codecov/codecov-action:代码覆盖率
七、6 个月使用路径
Month 1:基础
□ 第一个 workflow
□ 3 大 Action
□ CI 测试
Month 2:进阶
□ 多个 workflow
□ 缓存 + 优化
□ 自动部署
Month 3:Docker
□ 镜像构建
□ Docker Hub
□ K8s 部署
Month 4-5:高级
□ 矩阵策略
□ 多个环境
□ 监控集成
Month 6:专家
□ 完整 CI/CD
□ 安全扫描
□ 性能测试
八、3 大常见问答
Q1:GitHub Actions 免费吗? A:公开仓库免费 / 私有 2000 分钟/月。 Q2:怎么触发? A:on: push / pullrequest / schedule / workflowdispatch。 Q3:怎么调试? A:GitHub Actions 自带日志 + tmate SSH 调试。
反思:GitHub Actions = 2025 年 CI/CD 首选
不夸张地说:
GitHub Actions = 2025 年 CI/CD 事实标准——85% 项目在用。
立即开始:今天创建第一个 workflow = 你的 CI/CD 自动化开始。 未来 5 年,CI/CD = 后端工程师必会技能——必学。
💬 评论 19 条