name: Lint, build, test and deploy on: push: branches: [master] workflow_dispatch: jobs: # ------------------------------------------------------------ style checks # Runs in parallel with build; both must pass before the site is tested. lint: 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 dependencies run: yarn install --frozen-lockfile - name: Lint Markdown run: npx markdownlint-cli2 # ------------------------------------------------------------ build # Produces the static site once and shares it as an artifact so the # test and deploy stages never rebuild. 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 # Best-effort image optimization at build time: originals in source/ # stay untouched, only the built artifact shrinks (~58% on this site). - name: Optimize images run: | apt-get update -qq && apt-get install -y -qq pngquant oxipng jpegoptim find public -type f -name '*.png' -exec sh -c 'pngquant --quality=80-95 --strip --force --skip-if-larger --output "$1" "$1"; oxipng -o max -q "$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: [lint, 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 "" 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/