Files
publish/.gitea/workflows/deploy.yml
voson 8019ebd226
All checks were successful
deploy / build-and-deploy (push) Successful in 58s
chore: update deploy workflow to run on Debian and add notification step for deployment status
2026-02-04 09:29:55 +08:00

97 lines
3.4 KiB
YAML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: deploy
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: debian
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 }}'"
- name: 发送通知
if: always()
continue-on-error: true
env:
WEBHOOK_URL: ${{ vars.WEBHOOK_URL }}
run: |
[ -z "$WEBHOOK_URL" ] && exit 0
if [ "${{ job.status }}" = "success" ]; then
status_text="部署成功 ✅"
else
status_text="部署失败 ❌"
fi
commit_title=$(git log -1 --pretty=format:"%s" 2>/dev/null || echo "N/A")
workflow_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_number }}"
site_url="${{ vars.SITE_URL }}"
if [ "${{ job.status }}" = "success" ]; then
payload=$(jq -n \
--arg title "${{ github.event.repository.name }} ${status_text}" \
--arg commit "$commit_title" \
--arg author "${{ github.actor }}" \
--arg site "$site_url" \
--arg url "$workflow_url" \
'{msg_type: "text", content: {text: "\($title)\n提交\($commit)\n作者\($author)\n站点\($site)\n详情\($url)"}}')
else
payload=$(jq -n \
--arg title "${{ github.event.repository.name }} ${status_text}" \
--arg commit "$commit_title" \
--arg author "${{ github.actor }}" \
--arg url "$workflow_url" \
'{msg_type: "text", content: {text: "\($title)\n提交\($commit)\n作者\($author)\n详情\($url)"}}')
fi
curl -sS -H "Content-Type: application/json" -X POST -d "$payload" "$WEBHOOK_URL"