diff --git a/Jenkinsfile b/Jenkinsfile index a4cb55a..9907f5d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,3 +1,10 @@ +// IOT 开放网关流水线:拉代码 → 构建 → 部署重启。 +// 由 Jenkins 的 iot-open-gateway 任务以「Pipeline script from SCM」读取本文件。 +// +// 运行方式:网关 jar 与 Jenkins 在同一台服务器(192.168.114.100)上,直接在本机重启,不走 SSH。 +// 敏感配置(GATEWAY_JWT_SECRET / GATEWAY_INTERNAL_SECRET / GATEWAY_ADMIN_SECRET 等)放在服务器上的 +// /data/iot/gateway/gateway.env(权限 600),启动前 source 进来,不进仓库。 +// 数据库账号密码、后端地址已作为默认值写在 application*.yml 里(内网测试环境)。 pipeline { agent any @@ -10,19 +17,37 @@ pipeline { DEPLOY_PORT = '18082' APP_NAME = 'iot-open-gateway' DEPLOY_DIR = '/data/iot/gateway' + // 防止 Jenkins 在任务结束时自动杀掉后台 nohup 进程 JENKINS_NODE_COOKIE = 'dontKillMe' BUILD_ID = 'dontKillMe' } stages { - stage('Deploy & Run') { + stage('拉代码') { + steps { + git branch: 'main', url: 'https://git.tengwangyun.com/wjh/IoT-OpenApi.git' + } + } + + stage('构建') { + steps { + // 网关没有单测,跳过测试加快构建 + sh 'mvn -B clean package -DskipTests' + } + } + + stage('部署') { steps { sh ''' - echo "=== 启动 IOT 开放网关微服务 ===" + set -e + echo "=== 部署 IOT 开放网关 ===" mkdir -p "${DEPLOY_DIR}" - - if [ -f "/data/iot/gateway/source/target/iot-open-gateway-1.0.0.jar" ]; then - cp -f "/data/iot/gateway/source/target/iot-open-gateway-1.0.0.jar" "${DEPLOY_DIR}/${APP_NAME}.jar" + JAR=$(ls target/iot-open-gateway-*.jar | head -1) + cp -f "$JAR" "${DEPLOY_DIR}/${APP_NAME}.jar" + + if [ ! -f "${DEPLOY_DIR}/gateway.env" ]; then + echo "缺少 ${DEPLOY_DIR}/gateway.env(存放 GATEWAY_*_SECRET 等),中止" >&2 + exit 1 fi PID=$(lsof -ti:${DEPLOY_PORT} || true) @@ -34,13 +59,16 @@ pipeline { export JENKINS_NODE_COOKIE=dontKillMe export BUILD_ID=dontKillMe + set -a + . "${DEPLOY_DIR}/gateway.env" + set +a nohup java -jar -Dspring.profiles.active=prod "${DEPLOY_DIR}/${APP_NAME}.jar" > "${DEPLOY_DIR}/${APP_NAME}.log" 2>&1 & - - echo "后台启动中..." - sleep 6 + + echo "后台启动中,等待 12 秒检测..." + sleep 12 lsof -i:${DEPLOY_PORT} || true echo "=== 网关健康检查 ===" - curl -s http://127.0.0.1:${DEPLOY_PORT}/open/gateway/health || true + curl -sf http://127.0.0.1:${DEPLOY_PORT}/open/gateway/health echo "" ''' } diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 93e57b5..c428811 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -5,13 +5,13 @@ spring: datasource: url: ${DB_URL:jdbc:postgresql://127.0.0.1:5432/iot_platform} username: ${DB_USERNAME:root} - password: ${DB_PASSWORD:} + password: ${DB_PASSWORD:Dzaiot123456} gateway: oauth: jwt-secret: ${GATEWAY_JWT_SECRET:} backend: - base-url: ${GATEWAY_BACKEND_URL:http://127.0.0.1:18086} + base-url: ${GATEWAY_BACKEND_URL:http://127.0.0.1:8080} internal-secret: ${GATEWAY_INTERNAL_SECRET:} admin: secret: ${GATEWAY_ADMIN_SECRET:} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 8b5aaab..e2cfa81 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -9,7 +9,7 @@ spring: driver-class-name: org.postgresql.Driver url: ${DB_URL:jdbc:postgresql://127.0.0.1:5432/iot_platform} username: ${DB_USERNAME:root} - password: ${DB_PASSWORD:} + password: ${DB_PASSWORD:Dzaiot123456} mybatis-plus: configuration: @@ -28,7 +28,7 @@ gateway: token-expire-seconds: ${GATEWAY_TOKEN_EXPIRE:7200} # 核心物联后端内网地址与专线通信密钥 backend: - base-url: ${GATEWAY_BACKEND_URL:http://localhost:8080} + base-url: ${GATEWAY_BACKEND_URL:http://127.0.0.1:8080} internal-secret: ${GATEWAY_INTERNAL_SECRET:} # 管理员运维密钥通道 admin: