From 7109c21c0ee778af26fc92cc4f20255a03262c72 Mon Sep 17 00:00:00 2001 From: qiaoxinjiu Date: Tue, 14 Apr 2026 18:22:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9jenkins=E6=B5=81=E6=B0=B4?= =?UTF-8?q?=E7=BA=BF=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 17 ++++++ Jenkinsfile | 133 +++++++++++++++++++++++++++++++++++++++++++++++ const.py | 2 +- gunicorn.conf.py | 2 +- 4 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 Jenkinsfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..75c3919 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM 39.170.26.156:8443/library/python:3.9-slim + +WORKDIR /app + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 + +RUN mkdir -p /app/logs + +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +EXPOSE 5010 + +CMD ["gunicorn", "--config=gunicorn.conf.py", "manage:app"] diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..9bec76a --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,133 @@ +def dockerImage + +pipeline { + agent { + docker { + image 'cicd/ubuntu:22.04-docker-py312-jdk8-nodejs20-mvn389' + args '-u root:root -v /var/run/docker.sock:/var/run/docker.sock' + } + } + + environment { + GIT_REPO = 'https://wdxz-gitea.best-envision.com/qiaoxinjiu/effekt-interface.git' + GIT_CREDENTIALS_ID = 'ebe42f70-146f-4a4b-8090-eded24a77173' + + HARBOR_URL = '39.170.26.156:8443' + HARBOR_PROJECT = 'effekt' + IMAGE_NAME = 'effekt-interface' + IMAGE = "${HARBOR_URL}/${HARBOR_PROJECT}/${IMAGE_NAME}" + IMAGE_TAG = 'latest' + HARBOR_CREDENTIALS_ID = '3a4a4463-784d-4e91-9457-9dfd64722ecb' + + APP_NAME = 'effekt-interface' + HOST_PORT = '5010' + CONTAINER_PORT = '5010' + + DEPLOY_HOST = '39.170.26.156' + DEPLOY_PORT = '22' + DEPLOY_USER = 'root' + DEPLOY_CREDENTIALS_ID = '82d14a67-250f-46b0-8b13-50e874a11933' + } + + options { + timestamps() + disableConcurrentBuilds() + buildDiscarder(logRotator(numToKeepStr: '20')) + } + + stages { + stage('Checkout') { + steps { + checkout([ + $class: 'GitSCM', + branches: [[name: '*/master']], + userRemoteConfigs: [[ + url: env.GIT_REPO, + credentialsId: env.GIT_CREDENTIALS_ID + ]] + ]) + } + } + + stage('Build Image') { + steps { + script { + dockerImage = docker.build("${env.IMAGE}:${env.IMAGE_TAG}") + } + } + } + + stage('Push Image To Harbor') { + steps { + script { + docker.withRegistry("https://${env.HARBOR_URL}", env.HARBOR_CREDENTIALS_ID) { + dockerImage.push(env.IMAGE_TAG) + } + } + } + } + + stage('Deploy') { + steps { + script { + def remote = [ + name: 'deploy-server', + host: env.DEPLOY_HOST, + port: env.DEPLOY_PORT as int, + user: env.DEPLOY_USER, + allowAnyHosts: true, + credentialsId: env.DEPLOY_CREDENTIALS_ID + ] + + withCredentials([usernamePassword(credentialsId: env.HARBOR_CREDENTIALS_ID, usernameVariable: 'HARBOR_USER', passwordVariable: 'HARBOR_PASS')]) { + sshCommand remote: remote, command: """ + set -e + docker login ${env.HARBOR_URL} -u ${HARBOR_USER} -p '${HARBOR_PASS}' + docker pull ${env.IMAGE}:${env.IMAGE_TAG} + docker rm -f ${env.APP_NAME} || true + docker run -d \\ + --name ${env.APP_NAME} \\ + --restart always \\ + -p ${env.HOST_PORT}:${env.CONTAINER_PORT} \\ + ${env.IMAGE}:${env.IMAGE_TAG} + docker ps --filter name=${env.APP_NAME} + """, sudo: false + } + } + } + } + + stage('Verify') { + steps { + script { + def remote = [ + name: 'deploy-server', + host: env.DEPLOY_HOST, + port: env.DEPLOY_PORT as int, + user: env.DEPLOY_USER, + allowAnyHosts: true, + credentialsId: env.DEPLOY_CREDENTIALS_ID + ] + + sshCommand remote: remote, command: """ + set -e + docker ps --filter name=${env.APP_NAME} + curl -I http://127.0.0.1:${env.HOST_PORT} + """, sudo: false + } + } + } + } + + post { + always { + sh 'docker image prune -f || true' + } + success { + echo 'Pipeline finished successfully.' + } + failure { + echo 'Pipeline failed.' + } + } +} diff --git a/const.py b/const.py index 756a37e..1b65393 100644 --- a/const.py +++ b/const.py @@ -6,7 +6,7 @@ from urllib.parse import quote # dev环境 # BE_URL = '127.0.0.1:6080' # online环境 -BE_URL = '0.0.0.0:6080' +BE_URL = '0.0.0.0:5010' BASEDIR = os.path.dirname(os.path.abspath(__file__)) # PROJDIR = os.path.dirname(BASEDIR) diff --git a/gunicorn.conf.py b/gunicorn.conf.py index 3942580..64da5e6 100644 --- a/gunicorn.conf.py +++ b/gunicorn.conf.py @@ -12,4 +12,4 @@ pidfile = "logs/gunicorn.pid" accesslog = "logs/access.log" # 每个接口调用会展示在access.log中 errorlog = "logs/debug.log" # 未处理的报错会在debug.log日志中展示 timeout = 300 # 每个接口的超时时间 -daemon = True # 是否开启守护进程。不开启可直接在idea或命令行中查看日志,在服务器上需要修改为True来开启守护进程 +daemon = False # 容器内以前台方式运行,避免主进程退出