大家好,我是程序员柏风,AI 最全的 Vibecoding 学习地图 http://coderoadmap.cn 作者。
Checkpoint: 自动化
你将完成什么
- 用 GitHub Actions 实现 CI(自动测试、构建)
- 用 GitHub Actions 实现 CD(自动部署)
- 用 Ansible 或 Terraform 自动化基础设施
验收标准
- 代码提交后自动测试
- 测试通过后自动部署
- 基础设施用代码管理
CI 流水线
# .github/workflows/ci.yml
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm ci
- run: npm test
- run: npm run build
CD 流水线
# .github/workflows/deploy.yml
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /var/www/app
git pull origin main
npm install --production
npm run build
pm2 restart app
基础设施自动化
Terraform
# main.tf
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "web-server"
}
}
terraform init
terraform plan
terraform apply
Ansible
# playbook.yml
- name: Setup web servers
hosts: webservers
tasks:
- name: Install nginx
apt:
name: nginx
state: present
ansible-playbook -i inventory.ini playbook.yml
常见卡点
- CI 失败:查看 Actions 日志,通常是测试不通过或依赖安装失败。
- 部署失败:确认服务器 SSH 密钥正确,环境变量配置完整。
- Terraform 状态丢失:配置远程状态存储(S3 + DynamoDB)。
加分项
- 用蓝绿部署实现零停机发布
- 用 Prometheus + Grafana 监控应用
- 用 ELK 收集日志
下一步
完成本课后,你已完成全栈路线图的全部阶段。继续深入学习:
- 微服务架构
- Kubernetes 容器编排
- 性能优化
- 安全防护