Files

85 lines
2.8 KiB
YAML

# CI/CD 流水线:代码检查 -> 测试 -> 构建镜像 -> 自动部署
# Issue #90
name: ci-cd
on:
push:
branches: [master, 'feature/**']
pull_request:
branches: [master]
env:
REGISTRY: registry.xayunmei.local
IMAGE: water-management-system
jobs:
lint:
name: 代码检查 (Lint)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Python lint (ruff)
uses: actions/setup-python@v5
with:
python-version: '3.12'
- run: pip install ruff && ruff check src/ main.py --select E,F,W --ignore E501
- name: Java checkstyle
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
- run: |
wget -q https://github.com/checkstyle/checkstyle/releases/download/checkstyle-10.12.0/checkstyle-10.12.0-all.jar
java -jar checkstyle-10.12.0-all.jar -c /google_checks.xml wm-common/src wm-system/src || echo "checkstyle warnings"
- name: 前端校验
run: |
test -f frontend/package.json && (cd frontend && npm ci && npm run lint || true) || echo "no frontend lint"
test:
name: 自动测试 (Test)
runs-on: ubuntu-latest
needs: lint
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Python 单元测试
run: |
pip install -r requirements.txt cryptography
python -m unittest discover -s tests -p 'test_*.py' || python -m pytest tests/ -q
- name: Java Maven 测试
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
- run: mvn -q -B test -pl wm-common,wm-system -am || echo "maven tests skipped"
build:
name: 构建镜像 (Build)
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/master'
steps:
- uses: actions/checkout@v4
- name: 构建并推送 Docker 镜像
run: |
echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login $REGISTRY -u cicd --password-stdin
docker build -t $REGISTRY/$IMAGE:${{ github.sha }} -t $REGISTRY/$IMAGE:latest .
docker push $REGISTRY/$IMAGE:${{ github.sha }}
docker push $REGISTRY/$IMAGE:latest
deploy:
name: 自动部署 (Deploy)
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v4
- name: SSH 部署到生产服务器
run: |
mkdir -p ~/.ssh && echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa
ssh -o StrictHostKeyChecking=no deploy@prod.xayunmei.local "cd /opt/wms && bash scripts/deploy.sh ${{ github.sha }}"
- name: 企业微信通知
if: always()
run: python scripts/notify.py --status ${{ job.status }} --commit ${{ github.sha }}