Files
jdt-mer/.gitea/workflows/ci.yaml
Huakk b61413b19c
Some checks failed
CI Build & Deploy / build-and-deploy-dev (push) Failing after 41s
CI Build & Deploy / build-and-deploy-prod (push) Has been skipped
ci(other): update cicd
2026-01-19 03:53:16 +08:00

147 lines
5.2 KiB
YAML

name: CI Build & Deploy
on:
push:
branches:
- test
- master
jobs:
build-and-deploy-dev:
if: gitea.ref_name == 'test'
runs-on: gitea_act_runner
container:
image: node:24-alpine
env:
APP_NAME: jdt-mer-dev
APP_PORT: 8083
steps:
- name: Install Git
run: |
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
apk add --no-cache git openssh-client sshpass curl
- name: Checkout
uses: https://gitea.com/actions/checkout@v4
- name: Setup environment
run: |
npm config set registry https://registry.npmmirror.com/
corepack enable && corepack prepare pnpm@8 --activate
- name: Install deps
run: pnpm install --frozen-lockfile
- name: Build
run: pnpm build:test
- name: Pack artifacts
run: tar -zcf dist.tar ./dist ./default.conf ./Dockerfile
- name: Upload and Deploy
env:
HOST: ${{ secrets.HOST_DEV }}
USER: ${{ secrets.USER_DEV }}
PWD: ${{ secrets.PWD_DEV }}
run: |
sshpass -p "$PWD" scp -o StrictHostKeyChecking=no -P 22 dist.tar ${USER}@${HOST}:/www/builder/
sshpass -p "$PWD" ssh -o StrictHostKeyChecking=no -p 22 ${USER}@${HOST} << 'ENDSSH'
set -e
cd /www/builder
APP_NAME=jdt-mer-dev
APP_PORT=8083
rm -rf $APP_NAME && mkdir -p $APP_NAME
tar -xzf dist.tar -C $APP_NAME && rm -f dist.tar
cd $APP_NAME
docker build -t $APP_NAME .
docker rm -f $APP_NAME 2>/dev/null || true
docker run -d -p $APP_PORT:80 --restart=always --name $APP_NAME $APP_NAME
cd .. && rm -rf $APP_NAME
ENDSSH
- name: Notify WeCom
if: always()
env:
WEBHOOK_KEY: ${{ secrets.QYWX_WEBHOOK_KEY }}
STATUS: ${{ job.status }}
run: |
EMOJI=$( [ "$STATUS" = "success" ] && echo "✅" || echo "❌" )
MSG="${EMOJI}**${{ gitea.repository }}** (Run #${{ gitea.run_number }})\n"
MSG+=">**构建结果**: ${STATUS}\n"
MSG+=">**构建详情**: [点击查看](${{ gitea.server_url }}/${{ gitea.repository }}/actions/runs/${{ gitea.run_id }})\n"
MSG+=">**代码分支**: ${{ gitea.ref_name }}\n"
MSG+=">**提交标识**: ${{ gitea.sha }}\n"
MSG+=">**提交发起**: ${{ gitea.actor }}"
curl -sS -H 'Content-Type: application/json' \
-d "{\"msgtype\":\"markdown\",\"markdown\":{\"content\":\"$MSG\"}}" \
"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${WEBHOOK_KEY}"
build-and-deploy-prod:
if: gitea.ref_name == 'master'
runs-on: gitea_act_runner
container:
image: node:24-alpine
env:
APP_NAME: jdt-mer-prod
APP_PORT: 8083
steps:
- name: Install Git
run: |
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
apk add --no-cache git openssh-client sshpass curl
- name: Checkout
uses: https://gitea.com/actions/checkout@v4
- name: Setup environment
run: |
npm config set registry https://registry.npmmirror.com/
corepack enable && corepack prepare pnpm@8 --activate
- name: Install deps
run: pnpm install --frozen-lockfile
- name: Build
run: pnpm build:prod
- name: Pack artifacts
run: tar -zcf dist.tar ./dist ./default.conf ./Dockerfile
- name: Upload and Deploy
env:
HOST: ${{ secrets.HOST_PROD }}
USER: ${{ secrets.USER_PROD }}
PWD: ${{ secrets.PWD_PROD }}
run: |
sshpass -p "$PWD" scp -o StrictHostKeyChecking=no -P 22 dist.tar ${USER}@${HOST}:/www/builder/
sshpass -p "$PWD" ssh -o StrictHostKeyChecking=no -p 22 ${USER}@${HOST} << 'ENDSSH'
set -e
cd /www/builder
APP_NAME=jdt-mer-prod
APP_PORT=8083
rm -rf $APP_NAME && mkdir -p $APP_NAME
tar -xzf dist.tar -C $APP_NAME && rm -f dist.tar
cd $APP_NAME
docker build -t $APP_NAME .
docker rm -f $APP_NAME 2>/dev/null || true
docker run -d -p $APP_PORT:80 --restart=always --name $APP_NAME $APP_NAME
cd .. && rm -rf $APP_NAME
ENDSSH
- name: Notify WeCom
if: always()
env:
WEBHOOK_KEY: ${{ secrets.QYWX_WEBHOOK_KEY }}
STATUS: ${{ job.status }}
run: |
EMOJI=$( [ "$STATUS" = "success" ] && echo "✅" || echo "❌" )
MSG="${EMOJI}**${{ gitea.repository }}** (Run #${{ gitea.run_number }})\n"
MSG+=">**构建结果**: ${STATUS}\n"
MSG+=">**构建详情**: [点击查看](${{ gitea.server_url }}/${{ gitea.repository }}/actions/runs/${{ gitea.run_id }})\n"
MSG+=">**代码分支**: ${{ gitea.ref_name }}\n"
MSG+=">**提交标识**: ${{ gitea.sha }}\n"
MSG+=">**提交发起**: ${{ gitea.actor }}"
curl -sS -H 'Content-Type: application/json' \
-d "{\"msgtype\":\"markdown\",\"markdown\":{\"content\":\"$MSG\"}}" \
"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${WEBHOOK_KEY}"