标签

AI赋能实践 - 打造自动化CI/CD工作流

发布时间:2026-04-10 18:17来源:微信阅读:6

bash

cd your-project

bash

claude

Code

请协助我搭建一个GitHub Actions持续集成与交付流程,具体需求如下:

1. 代码推送时自动触发

2. 运行单元测试

3. 构建 Docker 镜像

4. 推送到 Docker Hub

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@v4

- name: Setup Node.js

uses: actions/setup-node@v4

with:

node-version: '20'

- name: Install dependencies

run: npm ci

- name: Run tests

run: npm test

build:

needs: test

runs-on: ubuntu-latest

steps:

- uses: actions/checkout@v4

- name: Build Docker image

run: docker build -t your-app:${{ github.sha }} .

- name: Push to Docker Hub

run: |

echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin

docker push your-app:${{ github.sha }}

deploy:

needs: build

runs-on: ubuntu-latest

steps:

- name: Deploy to server

run: |

ssh user@server "docker pull your-app:${{ github.sha }} && docker-compose up -d"

bash

claude

请对这个GitHub Actions流程进行优化升级:

1. 添加缓存加速依赖安装

2. 添加构建失败通知

3. 添加自动回滚机制

4. 添加性能测试环节

yaml

- name: Cache node modules

uses: actions/cache@v4

with:

path: ~/.npm

key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}

restore-keys: |

${{ runner.os }}-node-

yaml

- name: Rollback on failure

if: failure()

run: |

ssh user@server "docker-compose down && docker-compose up -d --force-recreate old-image"

yaml

- name: Notify on failure

if: failure()

run: |

curl -X POST "YOUR_WEBHOOK_URL" \

-H "Content-Type: application/json" \

-d '{"text": "❌ 构建失败:${{ github.repository }}"}'

yaml

deploy-prod:

if: github.ref == 'refs/heads/main'

needs: build

runs-on: ubuntu-latest

steps:

- name: Deploy to production

run: ./deploy-prod.sh