From 17cf80bed3ac9ee3ef64dccf92b64133055cde88 Mon Sep 17 00:00:00 2001 From: kurihada Date: Tue, 3 Mar 2026 15:04:59 +0800 Subject: [PATCH] =?UTF-8?q?Jenkins=20=E5=A2=9E=E5=8A=A0=20Node=20=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E6=97=B6=E8=87=AA=E6=A3=80=E4=B8=8E=E5=9B=9E=E9=80=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 71 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 21 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index d15a157..5b1b3aa 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,9 +2,7 @@ pipeline { agent any environment { - APP_NAME = 'no-whatever' - AMAP_KEY = credentials('amap-api-key') - DEEPSEEK_KEY = credentials('deepseek-api-key') + APP_NAME = 'no-whatever' } triggers { @@ -18,6 +16,24 @@ pipeline { } } + stage('Node Runtime') { + steps { + script { + if (sh(returnStatus: true, script: 'command -v npm >/dev/null 2>&1') != 0) { + echo "System npm not found, trying Jenkins NodeJS tool 'node20'" + try { + def nodeHome = tool name: 'node20', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation' + env.PATH = "${nodeHome}/bin:${env.PATH}" + } catch (err) { + error "npm not found and Jenkins NodeJS tool 'node20' is unavailable. Please install NodeJS plugin and configure tool name 'node20'. Root cause: ${err}" + } + } + } + sh 'node -v' + sh 'npm -v' + } + } + stage('Install Dependencies') { steps { sh 'npm ci' @@ -42,32 +58,46 @@ pipeline { } sh 'npm run test:e2e' } + post { + always { + archiveArtifacts artifacts: 'playwright-report/**,test-results/**', allowEmptyArchive: true + } + } } stage('Build Docker Image') { steps { - sh "docker build --build-arg NEXT_PUBLIC_AMAP_API_KEY=${AMAP_KEY} -t ${APP_NAME}:${BUILD_NUMBER} -t ${APP_NAME}:latest ." + withCredentials([ + string(credentialsId: 'amap-api-key', variable: 'AMAP_KEY') + ]) { + sh "docker build --build-arg NEXT_PUBLIC_AMAP_API_KEY=${AMAP_KEY} -t ${APP_NAME}:${BUILD_NUMBER} -t ${APP_NAME}:latest ." + } } } stage('Deploy') { steps { - sh """ - docker stop ${APP_NAME} || true - docker rm ${APP_NAME} || true - mkdir -p /data/${APP_NAME} - chown 1001:1001 /data/${APP_NAME} - docker run -d \ - --name ${APP_NAME} \ - --network nginx \ - -p 3721:3721 \ - -v /data/${APP_NAME}:/app/data \ - -e DATABASE_URL=file:/app/data/prod.db \ - -e AMAP_API_KEY=${AMAP_KEY} \ - -e DEEPSEEK_API_KEY=${DEEPSEEK_KEY} \ - --restart unless-stopped \ - ${APP_NAME}:latest - """ + withCredentials([ + string(credentialsId: 'amap-api-key', variable: 'AMAP_KEY'), + string(credentialsId: 'deepseek-api-key', variable: 'DEEPSEEK_KEY') + ]) { + sh """ + docker stop ${APP_NAME} || true + docker rm ${APP_NAME} || true + mkdir -p /data/${APP_NAME} + chown 1001:1001 /data/${APP_NAME} + docker run -d \ + --name ${APP_NAME} \ + --network nginx \ + -p 3721:3721 \ + -v /data/${APP_NAME}:/app/data \ + -e DATABASE_URL=file:/app/data/prod.db \ + -e AMAP_API_KEY=${AMAP_KEY} \ + -e DEEPSEEK_API_KEY=${DEEPSEEK_KEY} \ + --restart unless-stopped \ + ${APP_NAME}:latest + """ + } } } } @@ -78,7 +108,6 @@ pipeline { } failure { echo "Build #${BUILD_NUMBER} failed" - archiveArtifacts artifacts: 'playwright-report/**,test-results/**', allowEmptyArchive: true } } }