Baked optimizer image, link check, safe rsync --delete, skip redundant installs, no concurrent builds, workspace cleanup
Hexo-Butterfly/pipeline/head There was a failure building this commit
Hexo-Butterfly/pipeline/head There was a failure building this commit
This commit is contained in:
1 parent
6832dffa97
commit
d7f38da5ee
1 file changed
+65
-7
Vendored
+65
-7
@@ -1,11 +1,15 @@
|
||||
// Jenkins pipeline for the Hexo site.
|
||||
// Stage flow: dependencies (cached) -> check -> lint -> build -> test -> deploy.
|
||||
// yarn/npm caches live on the agent and are reused across builds; the cache
|
||||
// is deleted if no build has used it for 7 days, and rebuilt on the next build.
|
||||
// yarn/npm caches live on the agent; the cache is deleted if unused for 7 days.
|
||||
pipeline {
|
||||
agent { label 'linux' }
|
||||
|
||||
options { timestamps() }
|
||||
options {
|
||||
timestamps()
|
||||
disableConcurrentBuilds(abortPrevious: true)
|
||||
timeout(time: 30, unit: 'MINUTES')
|
||||
buildDiscarder(logRotator(numToKeepStr: '20'))
|
||||
}
|
||||
|
||||
environment {
|
||||
SITE_CACHE = '/home/jenkins/hexo-cache'
|
||||
@@ -37,7 +41,7 @@ pipeline {
|
||||
date -u +%Y-%m-%dT%H:%M:%SZ > /cache/last-used
|
||||
|
||||
export PATH=/cache/npm-global/bin:$PATH
|
||||
npm install -g hexo-cli >/dev/null
|
||||
command -v hexo > /dev/null || npm install -g hexo-cli >/dev/null
|
||||
yarn install --frozen-lockfile
|
||||
echo "dependencies ready (node_modules installed from cache)"
|
||||
'
|
||||
@@ -92,15 +96,23 @@ pipeline {
|
||||
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
|
||||
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)"
|
||||
EOS
|
||||
docker run --rm -v "$PWD:/site" -w /site -v /tmp/optimize.sh:/optimize.sh node:22 bash /optimize.sh
|
||||
docker run --rm -v "$PWD:/site" -w /site -v /tmp/optimize.sh:/optimize.sh hexo-opt:local bash /optimize.sh
|
||||
'''
|
||||
}
|
||||
}
|
||||
@@ -110,12 +122,16 @@ EOS
|
||||
sh '''
|
||||
cat > /tmp/smoke.py <<'EOS'
|
||||
import http.server, socketserver, os, threading, urllib.request
|
||||
import html.parser
|
||||
|
||||
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}"
|
||||
@@ -126,6 +142,40 @@ with socketserver.TCPServer(("127.0.0.1", 8080), http.server.SimpleHTTPRequestHa
|
||||
assert posts, "no posts"
|
||||
get(f"/posts/{posts[0]}/")
|
||||
get("/css/index.css")
|
||||
|
||||
# full internal link check: every <a href> in every page must resolve
|
||||
class LinkCollector(html.parser.HTMLParser):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.links = []
|
||||
def handle_starttag(self, tag, attrs):
|
||||
if tag == "a":
|
||||
for k, v in attrs:
|
||||
if k == "href" and v:
|
||||
self.links.append(v)
|
||||
|
||||
pages = [os.path.join(r, f) for r, _, fs in os.walk(".") for f in fs if f.endswith(".html")]
|
||||
broken = []
|
||||
for page in pages:
|
||||
collector = LinkCollector()
|
||||
with open(page, encoding="utf-8", errors="replace") as fh:
|
||||
collector.feed(fh.read())
|
||||
for href in collector.links:
|
||||
if href.startswith(("http://", "https://", "mailto:", "javascript:")):
|
||||
continue
|
||||
target = href.split("#", 1)[0].split("?", 1)[0]
|
||||
if not target:
|
||||
continue
|
||||
if target.startswith("/"):
|
||||
resolved = target.lstrip("/")
|
||||
else:
|
||||
resolved = os.path.normpath(os.path.join(os.path.dirname(page), target))
|
||||
if os.path.isdir(resolved):
|
||||
resolved = os.path.join(resolved, "index.html")
|
||||
if not os.path.exists(resolved):
|
||||
broken.append(f"{page} -> {href}")
|
||||
assert not broken, f"broken internal links ({len(broken)}):\n" + "\n".join(broken[:10])
|
||||
print(f"link check passed: all internal links across {len(pages)} pages resolve")
|
||||
print(f"smoke test passed for / and posts/{posts[0]}/")
|
||||
EOS
|
||||
docker run --rm -v "$PWD:/site" -w /site -v /tmp/smoke.py:/smoke.py python:3-slim python3 /smoke.py
|
||||
@@ -138,7 +188,9 @@ EOS
|
||||
steps {
|
||||
withCredentials([sshUserPrivateKey(credentialsId: 'hexo_deploy_key', keyFileVariable: 'DEPLOY_KEY')]) {
|
||||
sh '''
|
||||
rsync -avz --chmod=D755,F644 \\
|
||||
rsync -avz --delete --chmod=D755,F644 \\
|
||||
--exclude=.well-known/ \\
|
||||
--exclude=.user.ini \\
|
||||
-e "ssh -i $DEPLOY_KEY -o StrictHostKeyChecking=accept-new" \\
|
||||
public/ root@69.164.253.99:/www/wwwroot/leonxu260.dev/
|
||||
'''
|
||||
@@ -146,4 +198,10 @@ EOS
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
always {
|
||||
cleanWs()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user