From 40b6a3f4b9f6fef194bb6622959f7e9467e95f88 Mon Sep 17 00:00:00 2001 From: qiaoxinjiu Date: Mon, 13 Apr 2026 19:49:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9jenkins=E6=B5=81=E6=B0=B4?= =?UTF-8?q?=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkins.txt | 118 +++++++++++++++++++++++++++------------------------- 1 file changed, 62 insertions(+), 56 deletions(-) diff --git a/Jenkins.txt b/Jenkins.txt index b8d50ce..eefc74f 100644 --- a/Jenkins.txt +++ b/Jenkins.txt @@ -12,13 +12,16 @@ pipeline { 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 = '39.170.26.156' - DEPLOY_PORT = '8022' + DEPLOY_HOST = '124.220.32.45' + DEPLOY_PORT = '22' + DEPLOY_USER = 'user' CONTAINER_NAME = 'effekt-interface-frontend' HOST_PORT = '8080' CONTAINER_PORT = '80' @@ -34,7 +37,7 @@ pipeline { stage('Checkout Code') { steps { git branch: "${GIT_BRANCH}", - credentialsId: 'git-effekt-frontend', + credentialsId: 'ebe42f70-146f-4a4b-8090-eded24a77173', url: "${GIT_URL}" } } @@ -43,13 +46,12 @@ pipeline { steps { sh ''' set -e - echo "=== whoami ===" + echo "=== Build Container Environment ===" whoami - echo "=== Check Docker ===" docker --version - echo "=== Check Node ===" node -v npm -v + git --version echo "=== Workspace ===" pwd ls -la @@ -69,31 +71,26 @@ pipeline { stage('Build Docker Image') { steps { - sh ''' - set -e - docker build \ - -t ${IMAGE_NAME}:${BUILD_NUMBER} \ - -t ${IMAGE_NAME}:latest \ - . - ''' + 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 { - withCredentials([ - usernamePassword( - credentialsId: 'harbor-effekt', - usernameVariable: 'HARBOR_USER', - passwordVariable: 'HARBOR_PASS' - ) - ]) { + withDockerRegistry(registry: [credentialsId: '3a4a4463-784d-4e91-9457-9dfd64722ecb', url: 'https://39.170.26.156:8443']) { sh ''' set -e - echo "$HARBOR_PASS" | docker login ${REGISTRY} -u "$HARBOR_USER" --password-stdin docker push ${IMAGE_NAME}:${BUILD_NUMBER} docker push ${IMAGE_NAME}:latest - docker logout ${REGISTRY} ''' } } @@ -101,36 +98,12 @@ pipeline { stage('Deploy To Server') { steps { - withCredentials([ - usernamePassword( - credentialsId: 'docker-host-ssh', - usernameVariable: 'DEPLOY_USER', - passwordVariable: 'DEPLOY_PASS' - ), - usernamePassword( - credentialsId: 'harbor-effekt', - usernameVariable: 'HARBOR_USER', - passwordVariable: 'HARBOR_PASS' + script { + deploy_node_by_password( + remote_host: env.DEPLOY_HOST, + remote_port: env.DEPLOY_PORT, + credentials_id: '82d14a67-250f-46b0-8b13-50e874a11933' ) - ]) { - sh ''' - set -e - - sshpass -p "$DEPLOY_PASS" ssh \ - -o StrictHostKeyChecking=no \ - -p "$DEPLOY_PORT" \ - "$DEPLOY_USER@$DEPLOY_HOST" " - 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 - " - ''' } } } @@ -145,12 +118,45 @@ pipeline { } always { sh ''' - if command -v docker >/dev/null 2>&1; then - docker image prune -f || true - else - echo "docker command not found, skip image prune" - fi + 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 + } +}