Skip to content

Merge remote-tracking branch 'origin/main' into preview #9

Merge remote-tracking branch 'origin/main' into preview

Merge remote-tracking branch 'origin/main' into preview #9

name: Docker Sz-Admin CI
on:
push:
branches: [ "preview" ]
pull_request:
branches: [ "preview" ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
env:
# 服务名
APP_NAME: sz-admin
# 运行环境变量
RUNNING_ACTIVE: preview
# 端口。若修改端口,请保持与 Dockerfile中的 EXPOSE属性一致
SERVICE_PORT: 9800
# nginx配置文件路径
NGINX_CONF_DIR: /home/conf/sz-admin-nginx/conf.d
# Docker 仓库域名
ACR_DOMAIN: registry.cn-beijing.aliyuncs.com
# 命名空间
ACR_ZONE: sz-action
VERSION: latest
SHELL_RUN_DIR: /home/run
# 预览环境标识(预览环境某些功能将禁用)
VITE_PREVIEW: true
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install pnpm
run: npm install -g pnpm
- name: Install dependencies
run: pnpm install
- name: Build project
env:
VITE_PREVIEW: ${{ env.VITE_PREVIEW }}
run: pnpm run build
- name: Install sshpass
run: sudo apt-get update && sudo apt-get install -y sshpass
- name: Check if default.conf exists on remote
id: check-file
run: |
file_exists=$(sshpass -p ${{ secrets.REMOTE_PASSWORD }} ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} "if [ -f ${{ env.NGINX_CONF_DIR }}/default.conf ]; then echo true; else echo false; fi")
echo "file_exists=$file_exists" >> $GITHUB_ENV
- name: Upload default.conf if needed
if: ${{ env.file_exists == 'false' }}
run: sshpass -p ${{ secrets.REMOTE_PASSWORD }} scp -o StrictHostKeyChecking=no ./nginx/default.conf ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }}:${{ env.NGINX_CONF_DIR }}
- name: Build Docker image
run: docker build -t ${{ env.APP_NAME }}:${{ env.VERSION }} .
- name: Log in to ACR
run: echo "${{ secrets.ACR_PASSWORD }}" | docker login --username=${{ secrets.ACR_USERNAME }} ${{ env.ACR_DOMAIN }} --password-stdin
- name: Tag Docker image
run: docker tag ${{ env.APP_NAME }}:${{ env.VERSION }} ${{ env.ACR_DOMAIN }}/${{ env.ACR_ZONE }}/${{ env.APP_NAME }}:${{ env.VERSION }}
- name: Push Docker image to ACR
run: docker push ${{ env.ACR_DOMAIN }}/${{ env.ACR_ZONE }}/${{ env.APP_NAME }}:${{ env.VERSION }}
- name: Deploy to remote server
uses: appleboy/[email protected]
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
password: ${{ secrets.REMOTE_PASSWORD }}
script: |
docker pull ${{ env.ACR_DOMAIN }}/${{ env.ACR_ZONE }}/${{ env.APP_NAME }}:${{ env.VERSION }}
echo "==================== 生成启动命令脚本 ===================="
mkdir -p ${{ env.SHELL_RUN_DIR }}
START_SCRIPT="${{ env.SHELL_RUN_DIR }}/docker_run_${{ env.APP_NAME }}_${{ env.RUNNING_ACTIVE }}.sh"
cat > $START_SCRIPT <<EOL
#!/bin/bash
echo "==================== 停止旧应用容器 ===================="
docker stop ${{ env.APP_NAME }} || true
docker rm ${{ env.APP_NAME }} || true
docker image prune -f
docker builder prune -f
echo "==================== 启动应用容器 ===================="
docker run -itd \\
--name ${{ env.APP_NAME }} \\
--restart always \\
-p ${{ env.SERVICE_PORT }}:${{ env.SERVICE_PORT }} \\
-e TZ=Asia/Shanghai \\
-v ${{ env.NGINX_CONF_DIR }}:/etc/nginx/conf.d \\
${{ env.ACR_DOMAIN }}/${{ env.ACR_ZONE }}/${{ env.APP_NAME }}:${{ env.VERSION }}
EOL
chmod +x $START_SCRIPT
echo "启动脚本已生成:$START_SCRIPT"
echo "可以运行以下命令手动启动容器:"
echo "bash $START_SCRIPT"
bash $START_SCRIPT