From 2faa41cf106675bb8f45ea7bfa5e76fda92d125e Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 12 Jan 2026 17:32:55 +0100 Subject: [PATCH 1/2] feat(ci): add automated Lighthouse audit pipeline --- .env.dist | 4 +++- .gitignore | 7 +++++++ .lighthouserc.cjs | 23 ++++++++++++++++++++++ .vscode/extensions.json | 4 ---- .vscode/launch.json | 11 ----------- Jenkinsfile | 43 +++++++++++++++++++++++++++++++++++++++-- docker-compose.yml | 18 +++++++++++++++++ local-lighthouse.sh | 14 ++++++++++++++ 8 files changed, 106 insertions(+), 18 deletions(-) create mode 100644 .lighthouserc.cjs delete mode 100644 .vscode/extensions.json delete mode 100644 .vscode/launch.json create mode 100755 local-lighthouse.sh diff --git a/.env.dist b/.env.dist index 94d81fc..ef9ae76 100644 --- a/.env.dist +++ b/.env.dist @@ -1,2 +1,4 @@ NODE_VERSION=24 -CADDY_VERSION=2.10.2 \ No newline at end of file +CADDY_VERSION=2.10.2 +LHCI_VERSION=0.15.x +ZENIKA_VERSION=124-with-node \ No newline at end of file diff --git a/.gitignore b/.gitignore index 16d54bb..e4866ae 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ dist/ # dependencies node_modules/ +.pnpm-store/ # logs npm-debug.log* @@ -22,3 +23,9 @@ pnpm-debug.log* # jetbrains setting folder .idea/ + +# vscode +.vscode/ + +# Lighthouse CI reports +.lighthouseci/ diff --git a/.lighthouserc.cjs b/.lighthouserc.cjs new file mode 100644 index 0000000..396880f --- /dev/null +++ b/.lighthouserc.cjs @@ -0,0 +1,23 @@ +module.exports = { + ci: { + collect: { + staticDistDir: '/app/dist', + settings: { + chromeFlags: '--no-sandbox --disable-setuid-sandbox --headless=new --disable-gpu --disable-dev-shm-usage', + }, + numberOfRuns: 1 + }, + assert: { + assertions: { + 'categories:performance': ['error', {minScore: 0.9}], + 'categories:accessibility': ['error', {minScore: 0.9}], + 'categories:best-practices': ['error', {minScore: 0.9}], + 'categories:seo': ['error', {minScore: 0.9}], + }, + }, + upload: { + target: 'filesystem', + outputDir: './.lighthouseci', // C'est ici que Jenkins ira chercher les fichiers + }, + }, +}; \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json deleted file mode 100644 index 56f043d..0000000 --- a/.vscode/extensions.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "recommendations": ["astro-build.astro-vscode", "unifiedjs.vscode-mdx"], - "unwantedRecommendations": [] -} diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index d642209..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "command": "./node_modules/.bin/astro dev", - "name": "Development server", - "request": "launch", - "type": "node-terminal" - } - ] -} diff --git a/Jenkinsfile b/Jenkinsfile index 2140067..fecf9c6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,6 +2,7 @@ pipeline { agent any environment { DOCKER_HOST = "unix:///var/run/docker.sock" + APP_VERSION = "${env.BRANCH_NAME}-${env.BUILD_NUMBER}" } stages { stage('Build') { @@ -9,12 +10,41 @@ pipeline { // Get Caddy version sh "cp .env.dist .env" // Push portfolio version - sh "echo '\nPORTFOLIO_VERSION=${env.BRANCH_NAME}-${env.BUILD_NUMBER}' >> .env" + sh "echo '\nPORTFOLIO_VERSION=${env.APP_VERSION}' >> .env" echo "Building new portfolio version..." sh "docker compose build" } } + stage('Lighthouse Audit') { + steps { + + // Create volumes but no run with safety + sh "docker rm -f audit-tmp lighthouse-audit || true" + sh "docker compose --profile audit create lighthouse-audit" + + // Extract astro data from image to tmp-dist + sh "docker create --name audit-tmp portfolio:${env.APP_VERSION}" + sh "mkdir -p ./tmp-dist" + sh "docker cp audit-tmp:/usr/share/caddy/. ./tmp-dist" + sh "docker rm -f audit-tmp" + + // Inject data from tmp-dist and config to audit container + sh "docker cp ./tmp-dist/. lighthouse-audit:/app/dist/" + sh "docker cp .lighthouserc.cjs lighthouse-audit:/app/dist/audit-config.cjs" + + // Now run + sh "docker compose --profile audit run lighthouse-audit" + + // Get reports + sh "mkdir -p ./.lighthouseci" + sh "docker cp lighthouse-audit:/app/.lighthouseci/. ./.lighthouseci/" + + // Clean + sh "docker rm -f lighthouse-audit" + sh "rm -rf ./tmp-dist" + } + } stage('Deploy') { // Deploy only master branch when { @@ -35,7 +65,16 @@ pipeline { } post { always { - echo "Clean unused image..." + echo "Publish Lighthouse audit..." + publishHTML([ + allowMissing: true, + alwaysLinkToLastBuild: true, + keepAll: false, + reportDir: '.lighthouseci', + reportFiles: '*.html', + reportName: 'Lighthouse Report' + ]) + echo "Clean unused image" sh "docker image prune -f" } success { echo "Success !" } diff --git a/docker-compose.yml b/docker-compose.yml index acdd627..b9428a8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,6 +11,24 @@ services: networks: - reverse-proxy + lighthouse-audit: + profiles: + - audit + image: zenika/alpine-chrome:${ZENIKA_VERSION} + container_name: lighthouse-audit + user: "0:0" + volumes: + - audit_dist:/app/dist + - audit_reports:/app/.lighthouseci + environment: + - CHROME_PATH=/usr/bin/chromium-browser + working_dir: /app + command: npx @lhci/cli@${LHCI_VERSION} collect --config=/app/dist/audit-config.cjs + +volumes: + audit_dist: + audit_reports: + networks: reverse-proxy: external: true \ No newline at end of file diff --git a/local-lighthouse.sh b/local-lighthouse.sh new file mode 100755 index 0000000..29b68df --- /dev/null +++ b/local-lighthouse.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +mkdir -p ./.lighthouseci + +docker run --rm \ +--user 0:0 \ +-v ./dist:/app/dist \ +-v ./.lighthouserc.cjs:/app/.lighthouserc.cjs \ +-v ./.lighthouseci:/app/.lighthouseci \ +-w /app \ +-e CHROME_PATH=/usr/bin/chromium-browser \ +zenika/alpine-chrome:with-node \ +npx @lhci/cli@0.15.x collect \ + --config=/app/.lighthouserc.cjs From 5409a067010747597f15c384e7a4be131f6acb03 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 12 Jan 2026 17:33:52 +0100 Subject: [PATCH 2/2] refactor(ci): optimize docker cache and fix jenkins reporting --- .lighthouserc.cjs | 1 + Jenkinsfile | 8 ++++---- docker-compose.yml | 4 +++- local-lighthouse.sh | 36 +++++++++++++++++++++++++----------- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/.lighthouserc.cjs b/.lighthouserc.cjs index 396880f..2e1b3a6 100644 --- a/.lighthouserc.cjs +++ b/.lighthouserc.cjs @@ -4,6 +4,7 @@ module.exports = { staticDistDir: '/app/dist', settings: { chromeFlags: '--no-sandbox --disable-setuid-sandbox --headless=new --disable-gpu --disable-dev-shm-usage', + targets: ['filesystem'], }, numberOfRuns: 1 }, diff --git a/Jenkinsfile b/Jenkinsfile index fecf9c6..0fcbb6b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -21,9 +21,9 @@ pipeline { // Create volumes but no run with safety sh "docker rm -f audit-tmp lighthouse-audit || true" - sh "docker compose --profile audit create lighthouse-audit" + sh "docker compose --profile audit create --no-build --remove-orphans lighthouse-audit" - // Extract astro data from image to tmp-dist + // Extract astro data from temp container to tmp-dist folder sh "docker create --name audit-tmp portfolio:${env.APP_VERSION}" sh "mkdir -p ./tmp-dist" sh "docker cp audit-tmp:/usr/share/caddy/. ./tmp-dist" @@ -32,9 +32,10 @@ pipeline { // Inject data from tmp-dist and config to audit container sh "docker cp ./tmp-dist/. lighthouse-audit:/app/dist/" sh "docker cp .lighthouserc.cjs lighthouse-audit:/app/dist/audit-config.cjs" + sh "rm -rf ./tmp-dist" // Now run - sh "docker compose --profile audit run lighthouse-audit" + sh "docker start -a lighthouse-audit" // Get reports sh "mkdir -p ./.lighthouseci" @@ -42,7 +43,6 @@ pipeline { // Clean sh "docker rm -f lighthouse-audit" - sh "rm -rf ./tmp-dist" } } stage('Deploy') { diff --git a/docker-compose.yml b/docker-compose.yml index b9428a8..899c0d2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,14 +20,16 @@ services: volumes: - audit_dist:/app/dist - audit_reports:/app/.lighthouseci + - npm_cache:/root/.npm environment: - CHROME_PATH=/usr/bin/chromium-browser working_dir: /app - command: npx @lhci/cli@${LHCI_VERSION} collect --config=/app/dist/audit-config.cjs + command: npx --prefer-offline @lhci/cli@${LHCI_VERSION} autorun --config=/app/dist/audit-config.cjs volumes: audit_dist: audit_reports: + npm_cache: networks: reverse-proxy: diff --git a/local-lighthouse.sh b/local-lighthouse.sh index 29b68df..a78f616 100755 --- a/local-lighthouse.sh +++ b/local-lighthouse.sh @@ -1,14 +1,28 @@ #!/bin/bash -mkdir -p ./.lighthouseci +# Clean old audits and containers +rm -rf ./.lighthouseci +docker rm -f lighthouse-audit || true -docker run --rm \ ---user 0:0 \ --v ./dist:/app/dist \ --v ./.lighthouserc.cjs:/app/.lighthouserc.cjs \ --v ./.lighthouseci:/app/.lighthouseci \ --w /app \ --e CHROME_PATH=/usr/bin/chromium-browser \ -zenika/alpine-chrome:with-node \ -npx @lhci/cli@0.15.x collect \ - --config=/app/.lighthouserc.cjs +# Load env file and avoid warning for unused portofolio version +if [ -f .env.dist ]; then + export $(grep -v '^#' .env.dist | xargs) +fi +export PORTFOLIO_VERSION="local-audit" + +# Create container but no start +docker compose --profile audit create --no-build --remove-orphans lighthouse-audit + +# Inject data and config to container +docker cp ./dist/. lighthouse-audit:/app/dist/ +docker cp .lighthouserc.cjs lighthouse-audit:/app/dist/audit-config.cjs + +# Now run +docker start -a lighthouse-audit + +# Get reports +mkdir -p ./.lighthouseci +docker cp lighthouse-audit:/app/.lighthouseci/. ./.lighthouseci/ + +# Clean container +docker rm -f lighthouse-audit \ No newline at end of file