更新CICD配置

This commit is contained in:
wangxiaowei
2026-04-24 15:04:00 +08:00
parent 30b6dc7d46
commit 3d23ceba92
2 changed files with 179 additions and 147 deletions

View File

@ -1,98 +1,129 @@
stages:
- build
- deploy
name: CI/CD Pipeline
variables:
NODE_VERSION: "18"
on:
push:
branches:
- develop
- master
pull_request:
branches:
- develop
- master
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
jobs:
build-dev:
if: github.ref == 'refs/heads/develop'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
before_script:
- npm config set registry https://registry.npmmirror.com
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "18"
registry-url: "https://registry.npmmirror.com"
build-dev:
stage: build
only:
- develop
tags:
- docker
image: node:18-alpine
script:
- npm ci
- npm run type-check || true
- npm run lint || true
- npm run build
artifacts:
name: "dev-$CI_COMMIT_REF_NAME-$CI_COMMIT_SHA"
paths:
- dist/
expire_in: 1 day
- name: Install dependencies
run: npm ci
deploy-dev:
stage: deploy
only:
- develop
tags:
- docker
image: alpine:latest
before_script:
- apk add --no-cache openssh-client rsync
script:
- eval $(ssh-agent -s)
- mkdir -p ~/.ssh && chmod 700 ~/.ssh
- ssh-keyscan -H $DEV_SERVER_IP >> ~/.ssh/known_hosts 2>/dev/null
- echo "$DEV_SSH_PASSWORD" | sshpass ssh -o StrictHostKeyChecking=no
root@$DEV_SERVER_IP "mkdir -p $DEV_SERVER_PATH && chmod 755
$DEV_SERVER_PATH"
- sshpass -p "$DEV_SSH_PASSWORD" rsync -avz --delete -e "ssh -o
StrictHostKeyChecking=no" dist/ root@$DEV_SERVER_IP:$DEV_SERVER_PATH
needs:
- job: build-dev
artifacts: true
when: manual
- name: Type check
run: npm run type-check || true
build-prod:
stage: build
only:
- master
- main
tags:
- docker
image: node:18-alpine
script:
- npm ci
- npm run type-check || true
- npm run lint || true
- npm run build
artifacts:
name: "prod-$CI_COMMIT_REF_NAME-$CI_COMMIT_SHA"
paths:
- dist/
expire_in: 7 days
- name: Lint
run: npm run lint || true
deploy-prod:
stage: deploy
only:
- master
- main
tags:
- docker
image: alpine:latest
before_script:
- apk add --no-cache openssh-client rsync sshpass
script:
- eval $(ssh-agent -s)
- mkdir -p ~/.ssh && chmod 700 ~/.ssh
- ssh-keyscan -H $PROD_SERVER_IP >> ~/.ssh/known_hosts 2>/dev/null
- echo "$PROD_SSH_PASSWORD" | sshpass ssh -o StrictHostKeyChecking=no
root@$PROD_SERVER_IP "mkdir -p $PROD_SERVER_PATH && chmod 755
$PROD_SERVER_PATH"
- sshpass -p "$PROD_SSH_PASSWORD" rsync -avz --delete -e "ssh -o
StrictHostKeyChecking=no" dist/ root@$PROD_SERVER_IP:$PROD_SERVER_PATH
needs:
- job: build-prod
artifacts: true
when: manual
- name: Build
run: npm run build
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: dev-dist
path: dist/
retention-days: 1
deploy-dev:
if: github.ref == 'refs/heads/develop'
needs: build-dev
runs-on: ubuntu-latest
steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: dev-dist
path: dist/
- name: Deploy to dev server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.DEV_SERVER_IP }}
username: root
password: ${{ secrets.DEV_SSH_PASSWORD }}
port: 22
source: "dist/*"
target: ${{ secrets.DEV_SERVER_PATH }}
strip_components: 0
overwrite: true
command: |
mkdir -p ${{ secrets.DEV_SERVER_PATH }}
chmod 755 ${{ secrets.DEV_SERVER_PATH }}
build-prod:
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "18"
registry-url: "https://registry.npmmirror.com"
- name: Install dependencies
run: npm ci
- name: Type check
run: npm run type-check || true
- name: Lint
run: npm run lint || true
- name: Build
run: npm run build
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: prod-dist
path: dist/
retention-days: 7
deploy-prod:
if: github.ref == 'refs/heads/master'
needs: build-prod
runs-on: ubuntu-latest
environment: production
steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: prod-dist
path: dist/
- name: Deploy to prod server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.PROD_SERVER_IP }}
username: root
password: ${{ secrets.PROD_SSH_PASSWORD }}
port: 22
source: "dist/*"
target: ${{ secrets.PROD_SERVER_PATH }}
strip_components: 0
overwrite: true
command: |
mkdir -p ${{ secrets.PROD_SERVER_PATH }}
chmod 755 ${{ secrets.PROD_SERVER_PATH }}