From 5b88971518a68ac10bd4198e25515390a75441b7 Mon Sep 17 00:00:00 2001 From: qiaoxinjiu Date: Mon, 13 Apr 2026 19:23:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=81=E6=B0=B4=E7=BA=BF?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 8 +++ Dockerfile | 12 ++++ Jenkins.txt | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++ nginx.conf | 11 ++++ 4 files changed, 187 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 Jenkins.txt create mode 100644 nginx.conf diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..37c5b06 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +node_modules +dist +.git +.gitignore +Dockerfile +Jenkins.txt +npm-debug.log* +yarn-error.log* diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6a5ba0d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM node:14-alpine AS build +WORKDIR /app +COPY package*.json ./ +RUN npm install --registry=https://registry.npmmirror.com +COPY . . +RUN npm run build + +FROM nginx:stable-alpine +COPY --from=build /app/dist /usr/share/nginx/html +COPY nginx.conf /etc/nginx/conf.d/default.conf +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] diff --git a/Jenkins.txt b/Jenkins.txt new file mode 100644 index 0000000..b8d50ce --- /dev/null +++ b/Jenkins.txt @@ -0,0 +1,156 @@ +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' + + REGISTRY = '39.170.26.156:8443' + HARBOR_PROJECT = 'effekt' + IMAGE_REPO = 'effekt-interface-frontend' + IMAGE_NAME = "${REGISTRY}/${HARBOR_PROJECT}/${IMAGE_REPO}" + + DEPLOY_HOST = '39.170.26.156' + DEPLOY_PORT = '8022' + 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: 'git-effekt-frontend', + url: "${GIT_URL}" + } + } + + stage('Check Environment') { + steps { + sh ''' + set -e + echo "=== whoami ===" + whoami + echo "=== Check Docker ===" + docker --version + echo "=== Check Node ===" + node -v + npm -v + echo "=== Workspace ===" + pwd + ls -la + ''' + } + } + + stage('Build Frontend') { + steps { + sh ''' + set -e + npm install + npm run build + ''' + } + } + + stage('Build Docker Image') { + steps { + sh ''' + set -e + docker build \ + -t ${IMAGE_NAME}:${BUILD_NUMBER} \ + -t ${IMAGE_NAME}:latest \ + . + ''' + } + } + + stage('Push Image To Harbor') { + steps { + withCredentials([ + usernamePassword( + credentialsId: 'harbor-effekt', + usernameVariable: 'HARBOR_USER', + passwordVariable: 'HARBOR_PASS' + ) + ]) { + 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} + ''' + } + } + } + + 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' + ) + ]) { + 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 + " + ''' + } + } + } + } + + post { + success { + echo '流水线执行成功:代码拉取、构建、推送、部署已完成' + } + failure { + echo '流水线执行失败:请查看对应阶段日志' + } + 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 + ''' + } + } +} diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..8700dee --- /dev/null +++ b/nginx.conf @@ -0,0 +1,11 @@ +server { + listen 80; + server_name localhost; + + root /usr/share/nginx/html; + index index.html; + + location / { + try_files $uri $uri/ /index.html; + } +}