Files
Hexo-Butterfly/.gitea/workflows/deploy.yml
T
Leon Xu 766b5c59e8
Lint, build, test and deploy / build (push) Successful in 1m33s
Lint, build, test and deploy / test (push) Successful in 5s
Lint, build, test and deploy / deploy (push) Successful in 3s
Run lint after hexo build step
2026-11-29 14:53:54 -08:00

111 lines
3.8 KiB
YAML

name: Lint, build, test and deploy
on:
push:
branches: [master]
workflow_dispatch:
jobs:
# ------------------------------------------------------------ build
# Single dependency install serves both the lint check and the build.
# Lint failures fail this job, which gates test and deploy.
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
- name: Install hexo-cli
run: npm install -g hexo-cli
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Build site
run: hexo generate
- name: Lint Markdown
run: npx markdownlint-cli2
# Best-effort image optimization at build time: originals in source/
# stay untouched, only the built artifact shrinks (~56% on this site).
# Note: oxipng is not packaged in the runner's Ubuntu apt repos; pngquant
# provides nearly all of the savings on its own.
- name: Optimize images
run: |
apt-get update -qq && apt-get install -y -qq pngquant jpegoptim
find public -type f -name '*.png' -exec sh -c 'pngquant --quality=80-95 --strip --force --skip-if-larger --output "$1" "$1"' _ {} \;
find public -type f \( -name '*.jpg' -o -name '*.jpeg' \) -exec jpegoptim --max=90 --strip-all --threshold=5 -q {} \;
echo "site size after optimization: $(du -sh public | cut -f1)"
- name: Upload site artifact
uses: actions/upload-artifact@v4
with:
name: site
path: public/
# ------------------------------------------------------------ test
# Validates the built site locally. Never touches production. Deploy
# physically cannot run unless this job passes.
test:
needs: build
runs-on: ubuntu-latest
steps:
- name: Download site artifact
uses: actions/download-artifact@v4
with:
name: site
path: public
- name: Structural checks
run: |
test -f public/index.html
test "$(find public -type f | wc -l)" -gt 100
test -f public/css/index.css
test -f public/js/utils.js
grep -q "<title>" public/index.html
- name: Serving smoke test
run: |
(cd public && python3 -m http.server 8080 >/dev/null 2>&1 &)
sleep 1
curl -sf http://localhost:8080/ | grep -q "<title>"
POST="$(ls public/posts | head -1)"
curl -sf -o /dev/null "http://localhost:8080/posts/$POST/"
curl -sf -o /dev/null "http://localhost:8080/css/index.css"
echo "smoke test passed for / and posts/$POST/"
# ------------------------------------------------------------ deploy
# Ships the tested artifact to production over SSH with a key (the
# server rejects password auth). The IP is pinned on purpose: the
# domain's DNS is behind Cloudflare, which does not proxy SSH.
deploy:
needs: test
runs-on: ubuntu-latest
steps:
- name: Download site artifact
uses: actions/download-artifact@v4
with:
name: site
path: public
- name: Write deploy key from secret
env:
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
run: |
install -d -m 700 "$HOME/.ssh"
printf '%s\n' "$DEPLOY_KEY" > "$HOME/.ssh/deploy_key"
chmod 600 "$HOME/.ssh/deploy_key"
- name: Deploy via rsync over SSH
run: |
command -v rsync >/dev/null || { apt-get update -qq && apt-get install -y -qq rsync; }
rsync -avz --chmod=D755,F644 \
-e "ssh -i $HOME/.ssh/deploy_key -o StrictHostKeyChecking=accept-new" \
public/ root@69.164.253.99:/www/wwwroot/leonxu260.dev/