chore: update deploy workflow to use sshpass for SSH authentication and cache npm dependencies
Some checks failed
deploy / build-and-deploy (push) Failing after 1m6s

This commit is contained in:
voson
2026-02-03 17:31:42 +08:00
parent c2e540cc0d
commit 96c35afab6

View File

@@ -16,37 +16,44 @@ jobs:
uses: actions/setup-node@v4 uses: actions/setup-node@v4
with: with:
node-version: 20 node-version: 20
cache: npm
- 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 - name: Install
run: npm ci run: npm ci
- name: Build - name: Build
env: env:
SITE_URL: ${{ secrets.SITE_URL }} SITE_URL: ${{ vars.SITE_URL }}
run: npm run build run: npm run build
- name: Write deploy key - name: Install sshpass
env:
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
run: | run: |
mkdir -p ~/.ssh if ! command -v sshpass &> /dev/null; then
python3 - <<'PY' if command -v apt-get &> /dev/null; then
import os sudo apt-get update && sudo apt-get install -y sshpass
from pathlib import Path elif command -v yum &> /dev/null; then
key = os.environ['DEPLOY_SSH_KEY'] sudo yum install -y sshpass
p = Path.home() / '.ssh' / 'deploy_key' elif command -v apk &> /dev/null; then
p.write_text(key, encoding='utf-8') sudo apk add --no-cache sshpass
PY fi
chmod 600 ~/.ssh/deploy_key fi
- name: Add known_hosts - name: Add known_hosts
run: | run: |
mkdir -p ~/.ssh mkdir -p ~/.ssh
ssh-keyscan -p "${{ secrets.DEPLOY_SSH_PORT }}" -H "${{ secrets.DEPLOY_SSH_HOST }}" >> ~/.ssh/known_hosts ssh-keyscan -p "${{ vars.DEPLOY_SSH_PORT }}" -H "${{ vars.DEPLOY_SSH_HOST }}" >> ~/.ssh/known_hosts
- name: Deploy via tar over SSH - name: Deploy via tar over SSH
env:
SSHPASS: ${{ secrets.DEPLOY_SSH_PASSWORD }}
run: | run: |
tar -C dist -czf - . | \ tar -C dist -czf - . | \
ssh -o BatchMode=yes -o StrictHostKeyChecking=yes -o ConnectTimeout=15 -i ~/.ssh/deploy_key -p "${{ secrets.DEPLOY_SSH_PORT }}" "${{ secrets.DEPLOY_SSH_USER }}@${{ secrets.DEPLOY_SSH_HOST }}" \ sshpass -e ssh -o StrictHostKeyChecking=yes -o ConnectTimeout=15 -p "${{ vars.DEPLOY_SSH_PORT }}" "${{ vars.DEPLOY_SSH_USER }}@${{ vars.DEPLOY_SSH_HOST }}" \
"bash -lc 'set -euo pipefail; tmp=\"\${{ secrets.DEPLOY_PATH }}.tmp\"; rm -rf \"\$tmp\"; mkdir -p \"\$tmp\"; tar -xzf - -C \"\$tmp\"; rm -rf \"\${{ secrets.DEPLOY_PATH }}\"; mv \"\$tmp\" \"\${{ secrets.DEPLOY_PATH }}\"'" "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 }}'"