name: CI/CD Pipeline on: push: branches: [ main, develop ] pull_request: branches: [ main ] jobs: build: name: 构建和测试 runs-on: ubuntu-latest steps: - name: 检出代码 uses: actions/checkout@v3 - name: 设置 Node.js uses: actions/setup-node@v3 with: node-version: '18' - name: 检查文件存在性 run: | echo "检查项目文件..." if [ -f "index.html" ]; then echo "✓ index.html 存在"; else exit 1; fi if [ -f "style.css" ]; then echo "✓ style.css 存在"; else exit 1; fi if [ -f "game.js" ]; then echo "✓ game.js 存在"; else exit 1; fi - name: 检查 JavaScript 语法 run: | echo "检查 JavaScript 语法..." node -c game.js echo "✓ JavaScript 语法正确" - name: 检查 HTML 结构 run: | echo "检查 HTML 结构..." if grep -q "" index.html; then echo "✓ HTML 结构正确" else echo "✗ HTML 结构不正确" exit 1 fi deploy: name: 部署到生产环境 runs-on: ubuntu-latest needs: build if: github.ref == 'refs/heads/main' steps: - name: 检出代码 uses: actions/checkout@v3 - name: 准备部署 run: | echo "准备部署文件..." mkdir -p deploy cp index.html style.css game.js README.md deploy/ - name: 创建部署信息 run: | cat > deploy/deploy-info.json << EOF { "version": "${{ github.sha }}", "deployed_at": "$(date -Iseconds)", "branch": "${{ github.ref_name }}" } EOF - name: 上传部署产物 uses: actions/upload-artifact@v3 with: name: snake-game-deploy path: deploy/ retention-days: 30