diff --git a/.github/workflows/Docker Sz-Admin Test CI.yml b/.github/workflows/Docker Sz-Admin Test CI.yml new file mode 100644 index 0000000..de74522 --- /dev/null +++ b/.github/workflows/Docker Sz-Admin Test CI.yml @@ -0,0 +1,106 @@ +name: Docker Sz-Admin Test CI + +on: + push: + branches: [ "test" ] + pull_request: + branches: [ "test" ] + +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: test + 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_TEST }} ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER_TEST }}@${{ secrets.REMOTE_HOST_TEST }} "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_TEST }} scp -o StrictHostKeyChecking=no ./nginx/default.conf ${{ secrets.REMOTE_USER_TEST }}@${{ secrets.REMOTE_HOST_TEST }}:${{ 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/ssh-action@v1.2.0 + with: + host: ${{ secrets.REMOTE_HOST_TEST }} + username: ${{ secrets.REMOTE_USER_TEST }} + password: ${{ secrets.REMOTE_PASSWORD_TEST }} + 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 }}-${{ env.VERSION }}.sh" + cat > $START_SCRIPT <