name: deploy on: push: branches: - main jobs: build-and-deploy: runs-on: self-hosted steps: - name: Checkout uses: actions/checkout@v4 - name: Setup Node uses: actions/setup-node@v4 with: node-version: 20 - name: Cache npm dependencies # gitea 不支持 actions/cache@v4,使用 v3 代替 uses: actions/cache@v3 with: path: ~/.npm key: npm-${{ hashFiles('package-lock.json') }} restore-keys: npm- - name: Install run: npm ci - name: Build env: SITE_URL: ${{ vars.SITE_URL }} run: npm run build - name: Install sshpass run: | if ! command -v sshpass &> /dev/null; then if command -v apt-get &> /dev/null; then sudo apt-get update && sudo apt-get install -y sshpass elif command -v yum &> /dev/null; then sudo yum install -y sshpass elif command -v apk &> /dev/null; then sudo apk add --no-cache sshpass fi fi - name: Add known_hosts run: | mkdir -p ~/.ssh ssh-keyscan -p "${{ vars.DEPLOY_SSH_PORT }}" -H "${{ vars.DEPLOY_SSH_HOST }}" >> ~/.ssh/known_hosts - name: Deploy via tar over SSH env: SSHPASS: ${{ secrets.DEPLOY_SSH_PASSWORD }} run: | tar -C dist -czf - . | \ sshpass -e ssh -o StrictHostKeyChecking=yes -o ConnectTimeout=15 -p "${{ vars.DEPLOY_SSH_PORT }}" "${{ vars.DEPLOY_SSH_USER }}@${{ vars.DEPLOY_SSH_HOST }}" \ "set -euo pipefail; tmp='${{ vars.DEPLOY_PATH }}.tmp'; rm -rf \"\$tmp\"; mkdir -p \"\$tmp\"; tar -xzf - -C \"\$tmp\"; rm -rf '${{ vars.DEPLOY_PATH }}'; mv \"\$tmp\" '${{ vars.DEPLOY_PATH }}'"