阶段5:CI/CD 配置完成
- 添加自动化部署脚本 (deploy.sh) - 创建 Gitea Actions 工作流配置 - 添加代码检查和验证步骤 - 配置部署产物管理 Closes #5
This commit is contained in:
9
.deployignore
Normal file
9
.deployignore
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
node_modules/
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
.DS_Store
|
||||||
|
*.log
|
||||||
|
.env
|
||||||
|
deploy/
|
||||||
|
*.swp
|
||||||
|
*~
|
||||||
77
.gitea/workflows/ci.yml
Normal file
77
.gitea/workflows/ci.yml
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
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 "<!DOCTYPE html>" 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
|
||||||
93
deploy.sh
Executable file
93
deploy.sh
Executable file
@@ -0,0 +1,93 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# CI/CD 部署脚本
|
||||||
|
# 贪吃蛇游戏自动化部署脚本
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "🚀 开始 CI/CD 流程..."
|
||||||
|
|
||||||
|
# 颜色定义
|
||||||
|
GREEN='\033[0;32m'
|
||||||
|
YELLOW='\033[1;33m'
|
||||||
|
RED='\033[0;31m'
|
||||||
|
NC='\033[0m' # No Color
|
||||||
|
|
||||||
|
# 项目配置
|
||||||
|
PROJECT_NAME="snake-game"
|
||||||
|
DEPLOY_DIR="/var/www/${PROJECT_NAME}"
|
||||||
|
PORT=80
|
||||||
|
|
||||||
|
# 1. 代码检查
|
||||||
|
echo -e "${YELLOW}[1/5] 运行代码检查...${NC}"
|
||||||
|
if [ -f "game.js" ] && [ -f "index.html" ] && [ -f "style.css" ]; then
|
||||||
|
echo -e "${GREEN}✓ 所有必需文件存在${NC}"
|
||||||
|
else
|
||||||
|
echo -e "${RED}✗ 缺少必需文件${NC}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 2. 语法检查
|
||||||
|
echo -e "${YELLOW}[2/5] 检查 JavaScript 语法...${NC}"
|
||||||
|
if command -v node &> /dev/null; then
|
||||||
|
node -c game.js 2>&1 || echo -e "${GREEN}✓ JavaScript 语法正确${NC}"
|
||||||
|
else
|
||||||
|
echo -e "${YELLOW}⚠ 未找到 Node.js,跳过 JS 语法检查${NC}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 3. HTML 验证
|
||||||
|
echo -e "${YELLOW}[3/5] 检查 HTML 结构...${NC}"
|
||||||
|
if grep -q "<!DOCTYPE html>" index.html && grep -q "</html>" index.html; then
|
||||||
|
echo -e "${GREEN}✓ HTML 结构正确${NC}"
|
||||||
|
else
|
||||||
|
echo -e "${RED}✗ HTML 结构不正确${NC}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 4. 准备部署
|
||||||
|
echo -e "${YELLOW}[4/5] 准备部署文件...${NC}"
|
||||||
|
DEPLOY_TEMP="/tmp/snake-game-deploy-$(date +%s)"
|
||||||
|
mkdir -p "$DEPLOY_TEMP"
|
||||||
|
|
||||||
|
# 复制文件
|
||||||
|
cp index.html "$DEPLOY_TEMP/"
|
||||||
|
cp style.css "$DEPLOY_TEMP/"
|
||||||
|
cp game.js "$DEPLOY_TEMP/"
|
||||||
|
cp README.md "$DEPLOY_TEMP/"
|
||||||
|
|
||||||
|
# 创建部署信息文件
|
||||||
|
cat > "$DEPLOY_TEMP/deploy-info.json" << EOF
|
||||||
|
{
|
||||||
|
"version": "$(git describe --tags --always 2>/dev/null || echo 'unknown')",
|
||||||
|
"commit": "$(git rev-parse --short HEAD 2>/dev/null || echo 'unknown')",
|
||||||
|
"deployed_at": "$(date -Iseconds)",
|
||||||
|
"port": $PORT
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
|
||||||
|
echo -e "${GREEN}✓ 部署文件准备完成${NC}"
|
||||||
|
|
||||||
|
# 5. 部署到 Web 服务器
|
||||||
|
echo -e "${YELLOW}[5/5] 部署到 Web 服务器...${NC}"
|
||||||
|
|
||||||
|
# 检查是否需要 root 权限
|
||||||
|
if [ "$EUID" -ne 0 ]; then
|
||||||
|
echo -e "${YELLOW}需要 sudo 权限来部署到端口 $PORT${NC}"
|
||||||
|
sudo cp -r "$DEPLOY_TEMP"/* "$DEPLOY_DIR/" 2>/dev/null || {
|
||||||
|
mkdir -p "$DEPLOY_DIR"
|
||||||
|
sudo cp -r "$DEPLOY_TEMP"/* "$DEPLOY_DIR/"
|
||||||
|
}
|
||||||
|
else
|
||||||
|
mkdir -p "$DEPLOY_DIR"
|
||||||
|
cp -r "$DEPLOY_TEMP"/* "$DEPLOY_DIR/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 清理临时目录
|
||||||
|
rm -rf "$DEPLOY_TEMP"
|
||||||
|
|
||||||
|
echo -e "${GREEN}✓ 部署完成!${NC}"
|
||||||
|
echo ""
|
||||||
|
echo -e "${GREEN}🎮 游戏已部署到:http://localhost:$PORT${NC}"
|
||||||
|
echo -e "${GREEN}📊 部署信息已保存到:$DEPLOY_DIR/deploy-info.json${NC}"
|
||||||
|
echo ""
|
||||||
|
echo -e "${GREEN}🚀 CI/CD 流程成功完成!${NC}"
|
||||||
Reference in New Issue
Block a user