From 95113316a60892c55c5d1032ca60a8f21526b0c7 Mon Sep 17 00:00:00 2001 From: Discharge7443 Date: Fri, 4 Dec 2026 18:33:29 -0800 Subject: [PATCH] Cache yarn/npm/hexo-cli with 7-day staleness rule --- Jenkinsfile | 51 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index df9878d..554a053 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,19 +1,52 @@ -// Jenkins pipeline mirroring .gitea/workflows/deploy.yml -// Build + lint + optimize + test on every branch; deploy to production on master. +// Jenkins pipeline for the Hexo site. +// yarn/npm caches live on the agent and are reused across builds; +// if the cache goes unused for 7 days the next build resets it. pipeline { agent { label 'linux' } options { timestamps() } + environment { + SITE_CACHE = '/home/jenkins/hexo-cache' + } + stages { + stage('Site cache: refresh or reset (7-day rule)') { + steps { + sh ''' + mkdir -p "$SITE_CACHE" + docker run --rm -v "$SITE_CACHE:/cache" node:22 bash -c ' + set -eu + 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 + ' + ''' + } + } + stage('Build site') { steps { sh ''' - docker run --rm -v "$PWD:/site" -w /site node:22 bash -c ' - npm install -g hexo-cli >/dev/null && - yarn install --frozen-lockfile && - hexo generate && - npx markdownlint-cli2 + 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 ' + export PATH=/cache/npm-global/bin:$PATH + npm install -g hexo-cli >/dev/null && + yarn install --frozen-lockfile && + hexo generate && + npx markdownlint-cli2 ' ''' } @@ -68,8 +101,8 @@ EOS steps { withCredentials([sshUserPrivateKey(credentialsId: 'hexo_deploy_key', keyFileVariable: 'DEPLOY_KEY')]) { sh ''' - rsync -avz --chmod=D755,F644 \ - -e "ssh -i $DEPLOY_KEY -o StrictHostKeyChecking=accept-new" \ + rsync -avz --chmod=D755,F644 \\ + -e "ssh -i $DEPLOY_KEY -o StrictHostKeyChecking=accept-new" \\ public/ root@69.164.253.99:/www/wwwroot/leonxu260.dev/ ''' }