// Jenkins pipeline for the Hexo site. // Stage flow: dependencies (cached) -> check -> lint -> build -> test -> deploy. // yarn/npm caches live on the agent; the cache is deleted if unused for 7 days. pipeline { agent { label 'linux' } options { timestamps() disableConcurrentBuilds(abortPrevious: true) timeout(time: 30, unit: 'MINUTES') buildDiscarder(logRotator(numToKeepStr: '20')) } environment { SITE_CACHE = '/home/jenkins/hexo-cache' } stages { stage('Dependencies (cached, 7-day rule)') { steps { sh ''' mkdir -p "$SITE_CACHE" docker run --rm -v "$PWD:/site" -w /site -v "$SITE_CACHE:/cache" \\ -e YARN_CACHE_FOLDER=/cache/yarn \\ -e npm_config_prefix=/cache/npm-global \\ -e npm_config_cache=/cache/npm-cache \\ node:22 bash -c ' set -eu # 7-day rule: reset the cache if the last build using it is over a week old if [ -f /cache/last-used ] && [ -z "$(find /cache/last-used -mtime +7 2>/dev/null)" ]; then echo "site cache fresh (last used $(cat /cache/last-used))" else if [ -f /cache/last-used ]; then echo "site cache unused for 7+ days -- resetting" else echo "first run -- creating site cache" fi rm -rf /cache/yarn /cache/npm-global /cache/npm-cache mkdir -p /cache/yarn /cache/npm-global /cache/npm-cache fi date -u +%Y-%m-%dT%H:%M:%SZ > /cache/last-used export PATH=/cache/npm-global/bin:$PATH command -v hexo > /dev/null || npm install -g hexo-cli >/dev/null yarn install --frozen-lockfile echo "dependencies ready (node_modules installed from cache)" ' ''' } } stage('Check config files') { steps { sh ''' docker run --rm -v "$PWD:/site" -w /site -v "$SITE_CACHE:/cache" \\ -e npm_config_prefix=/cache/npm-global \\ -e npm_config_cache=/cache/npm-cache \\ node:22 bash -c ' set -eu export PATH=/cache/npm-global/bin:$PATH for f in _config.yml _config.butterfly.yml; do if [ -f "$f" ]; then npx --no-install js-yaml "$f" > /dev/null echo "$f: valid YAML" fi done ' ''' } } stage('Lint markdown') { steps { sh ''' docker run --rm -v "$PWD:/site" -w /site -v "$SITE_CACHE:/cache" \\ -e npm_config_prefix=/cache/npm-global \\ -e npm_config_cache=/cache/npm-cache \\ node:22 bash -c ' set -eu export PATH=/cache/npm-global/bin:$PATH npx --no-install markdownlint-cli2 echo "markdown lint passed" ' ''' } } stage('Build site') { steps { sh ''' docker run --rm -v "$PWD:/site" -w /site -v "$SITE_CACHE:/cache" \\ -e npm_config_prefix=/cache/npm-global \\ -e npm_config_cache=/cache/npm-cache \\ node:22 bash -c ' set -eu export PATH=/cache/npm-global/bin:$PATH hexo generate ' # Optimizer image with the tools baked in -- built only when missing, # instead of apt-installing on every build cat > /tmp/Dockerfile.optimize <<'EOS' FROM node:22 RUN apt-get update -qq && apt-get install -y -qq pngquant jpegoptim \\ && rm -rf /var/lib/apt/lists/* EOS docker image inspect hexo-opt:local > /dev/null 2>&1 \\ || docker build -q -t hexo-opt:local -f /tmp/Dockerfile.optimize /tmp cat > /tmp/optimize.sh <<'EOS' #!/bin/bash set -eu 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)" EOS docker run --rm -v "$PWD:/site" -w /site -v /tmp/optimize.sh:/optimize.sh hexo-opt:local bash /optimize.sh ''' } } stage('Test site') { steps { sh ''' cat > /tmp/smoke.py <<'EOS' import http.server, socketserver, os, threading, urllib.request import html.parser import urllib.parse os.chdir("public") with socketserver.TCPServer(("127.0.0.1", 8080), http.server.SimpleHTTPRequestHandler) as httpd: threading.Thread(target=httpd.serve_forever, daemon=True).start() def get(path): with urllib.request.urlopen(f"http://127.0.0.1:8080{path}") as r: return r.read().decode() # structural checks assert os.path.isfile("index.html"), "missing index.html" n = sum(len(files) for _, _, files in os.walk(".")) assert n > 100, f"too few files: {n}" assert os.path.isfile("css/index.css"), "missing css/index.css" assert os.path.isfile("js/utils.js"), "missing js/utils.js" assert "