pipeline { agent { docker { image "cicd/ubuntu:22.04-docker-py312-jdk8-nodejs20-mvn389" args '-v /var/run/docker.sock:/var/run/docker.sock -v /home/jenkins:/home/jenkins -v /etc/docker:/etc/docker' registryUrl 'https://39.170.26.156:8443' registryCredentialsId '3a4a4463-784d-4e91-9457-9dfd64722ecb' } } environment { GIT_URL = 'https://wdxz-gitea.best-envision.com/qiaoxinjiu/effekt-interface-frontend.git' GIT_BRANCH = 'master' REPO_HOST = '39.170.26.156:8443' REGISTRY = '39.170.26.156:8443' HARBOR_PROJECT = 'effekt' IMAGE_REPO = 'effekt-interface-frontend' IMAGE_NAME = "${REGISTRY}/${HARBOR_PROJECT}/${IMAGE_REPO}" BUILD_IMAGE = '39.170.26.156:8443/cicd/ubuntu:22.04-docker-py312-jdk8-nodejs20-mvn389' DEPLOY_HOST = '124.220.32.45' DEPLOY_PORT = '22' DEPLOY_USER = 'user' CONTAINER_NAME = 'effekt-interface-frontend' HOST_PORT = '8080' CONTAINER_PORT = '80' } options { timestamps() disableConcurrentBuilds() buildDiscarder(logRotator(numToKeepStr: '20')) } stages { stage('Checkout Code') { steps { git branch: "${GIT_BRANCH}", credentialsId: 'ebe42f70-146f-4a4b-8090-eded24a77173', url: "${GIT_URL}" } } stage('Check Environment') { steps { sh ''' set -e echo "=== Build Container Environment ===" whoami docker --version node -v npm -v git --version echo "=== Workspace ===" pwd ls -la ''' } } stage('Build Frontend') { steps { sh ''' set -e npm install npm run build ''' } } stage('Build Docker Image') { steps { withDockerRegistry(registry: [credentialsId: '3a4a4463-784d-4e91-9457-9dfd64722ecb', url: 'https://39.170.26.156:8443']) { sh ''' set -e docker build --no-cache \ -t ${IMAGE_NAME}:${BUILD_NUMBER} \ -t ${IMAGE_NAME}:latest \ -f Dockerfile \ . ''' } } } stage('Push Image To Harbor') { steps { withDockerRegistry(registry: [credentialsId: '3a4a4463-784d-4e91-9457-9dfd64722ecb', url: 'https://39.170.26.156:8443']) { sh ''' set -e docker push ${IMAGE_NAME}:${BUILD_NUMBER} docker push ${IMAGE_NAME}:latest ''' } } } stage('Deploy To Server') { steps { script { deploy_node_by_password( remote_host: env.DEPLOY_HOST, remote_port: env.DEPLOY_PORT, credentials_id: '82d14a67-250f-46b0-8b13-50e874a11933' ) } } } } post { success { echo '流水线执行成功:代码拉取、构建、推送、部署已完成' } failure { echo '流水线执行失败:请查看对应阶段日志' } always { sh ''' docker image prune -f || true ''' } } } def deploy_node_by_password(Map args) { withCredentials([ usernamePassword( credentialsId: args.credentials_id, passwordVariable: 'DEPLOY_PASS', usernameVariable: 'DEPLOY_USER_CREDENTIAL' ), usernamePassword( credentialsId: '3a4a4463-784d-4e91-9457-9dfd64722ecb', passwordVariable: 'HARBOR_PASS', usernameVariable: 'HARBOR_USER' ) ]) { def remote = [:] remote.name = args.remote_host remote.host = args.remote_host remote.port = Integer.valueOf(args.remote_port) remote.allowAnyHosts = true remote.user = DEPLOY_USER_CREDENTIAL remote.password = DEPLOY_PASS def deployCommand = """ echo '${HARBOR_PASS}' | docker login '${REGISTRY}' -u '${HARBOR_USER}' --password-stdin && docker pull '${IMAGE_NAME}:latest' && docker rm -f '${CONTAINER_NAME}' || true && docker run -d \ --name '${CONTAINER_NAME}' \ --restart always \ -p '${HOST_PORT}:${CONTAINER_PORT}' \ '${IMAGE_NAME}:latest' && docker logout '${REGISTRY}' || true """ sshCommand remote: remote, sudo: false, command: deployCommand } }