165 lines
5.5 KiB
YAML
165 lines
5.5 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
|
|
|
|
- name: Checkout
|
|
uses: https://gitea.com/actions/checkout@v4
|
|
|
|
- name: Setup environment
|
|
run: |
|
|
apk add --no-cache curl
|
|
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 to server
|
|
uses: docker://appleboy/scp-action:v0.1.7
|
|
with:
|
|
host: ${{ secrets.HOST_DEV }}
|
|
username: ${{ secrets.USER_DEV }}
|
|
password: ${{ secrets.PWD_DEV }}
|
|
port: 22
|
|
source: dist.tar
|
|
target: /www/builder
|
|
|
|
- name: Deploy
|
|
uses: docker://appleboy/ssh-action:v1.0.3
|
|
with:
|
|
host: ${{ secrets.HOST_DEV }}
|
|
username: ${{ secrets.USER_DEV }}
|
|
password: ${{ secrets.PWD_DEV }}
|
|
port: 22
|
|
envs: APP_NAME,APP_PORT
|
|
script: |
|
|
set -e
|
|
cd /www/builder
|
|
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
|
|
|
|
- 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
|
|
|
|
- name: Checkout
|
|
uses: https://gitea.com/actions/checkout@v4
|
|
|
|
- name: Setup environment
|
|
run: |
|
|
apk add --no-cache curl
|
|
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 to server
|
|
uses: docker://appleboy/scp-action:v0.1.7
|
|
with:
|
|
host: ${{ secrets.HOST_PROD }}
|
|
username: ${{ secrets.USER_PROD }}
|
|
password: ${{ secrets.PWD_PROD }}
|
|
port: 22
|
|
source: dist.tar
|
|
target: /www/builder
|
|
|
|
- name: Deploy
|
|
uses: docker://appleboy/ssh-action:v1.0.3
|
|
with:
|
|
host: ${{ secrets.HOST_PROD }}
|
|
username: ${{ secrets.USER_PROD }}
|
|
password: ${{ secrets.PWD_PROD }}
|
|
port: 22
|
|
envs: APP_NAME,APP_PORT
|
|
script: |
|
|
set -e
|
|
cd /www/builder
|
|
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
|
|
|
|
- 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}"
|