ci: 网关流水线改为拉代码+构建+部署,数据库连接默认值写入配置

- 新增 Jenkinsfile 阶段:拉取 main → mvn package → 本机重启,不再依赖服务器上手工打好的旧 jar
- 敏感密钥(GATEWAY_*_SECRET)放服务器 /data/iot/gateway/gateway.env,启动前 source,不进仓库
- application*.yml 写入 PostgreSQL 账号默认值(内网测试环境),后端地址默认指向同机 127.0.0.1:8080
- 部署后健康检查失败时让流水线报错,而不是静默通过

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
wjh
2026-10-10 13:39:23 +08:00
co-authored by Claude Sonnet 5
parent 96de9298ba
commit 33269da935
3 changed files with 41 additions and 13 deletions
Vendored
+37 -9
View File
@@ -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 { pipeline {
agent any agent any
@@ -10,19 +17,37 @@ pipeline {
DEPLOY_PORT = '18082' DEPLOY_PORT = '18082'
APP_NAME = 'iot-open-gateway' APP_NAME = 'iot-open-gateway'
DEPLOY_DIR = '/data/iot/gateway' DEPLOY_DIR = '/data/iot/gateway'
// 防止 Jenkins 在任务结束时自动杀掉后台 nohup 进程
JENKINS_NODE_COOKIE = 'dontKillMe' JENKINS_NODE_COOKIE = 'dontKillMe'
BUILD_ID = 'dontKillMe' BUILD_ID = 'dontKillMe'
} }
stages { 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 { steps {
sh ''' sh '''
echo "=== 启动 IOT 开放网关微服务 ===" set -e
echo "=== 部署 IOT 开放网关 ==="
mkdir -p "${DEPLOY_DIR}" mkdir -p "${DEPLOY_DIR}"
JAR=$(ls target/iot-open-gateway-*.jar | head -1)
if [ -f "/data/iot/gateway/source/target/iot-open-gateway-1.0.0.jar" ]; then cp -f "$JAR" "${DEPLOY_DIR}/${APP_NAME}.jar"
cp -f "/data/iot/gateway/source/target/iot-open-gateway-1.0.0.jar" "${DEPLOY_DIR}/${APP_NAME}.jar"
if [ ! -f "${DEPLOY_DIR}/gateway.env" ]; then
echo "缺少 ${DEPLOY_DIR}/gateway.env(存放 GATEWAY_*_SECRET 等),中止" >&2
exit 1
fi fi
PID=$(lsof -ti:${DEPLOY_PORT} || true) PID=$(lsof -ti:${DEPLOY_PORT} || true)
@@ -34,13 +59,16 @@ pipeline {
export JENKINS_NODE_COOKIE=dontKillMe export JENKINS_NODE_COOKIE=dontKillMe
export BUILD_ID=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 & nohup java -jar -Dspring.profiles.active=prod "${DEPLOY_DIR}/${APP_NAME}.jar" > "${DEPLOY_DIR}/${APP_NAME}.log" 2>&1 &
echo "后台启动中..." echo "后台启动中,等待 12 秒检测..."
sleep 6 sleep 12
lsof -i:${DEPLOY_PORT} || true lsof -i:${DEPLOY_PORT} || true
echo "=== 网关健康检查 ===" 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 "" echo ""
''' '''
} }
+2 -2
View File
@@ -5,13 +5,13 @@ spring:
datasource: datasource:
url: ${DB_URL:jdbc:postgresql://127.0.0.1:5432/iot_platform} url: ${DB_URL:jdbc:postgresql://127.0.0.1:5432/iot_platform}
username: ${DB_USERNAME:root} username: ${DB_USERNAME:root}
password: ${DB_PASSWORD:} password: ${DB_PASSWORD:Dzaiot123456}
gateway: gateway:
oauth: oauth:
jwt-secret: ${GATEWAY_JWT_SECRET:} jwt-secret: ${GATEWAY_JWT_SECRET:}
backend: 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:} internal-secret: ${GATEWAY_INTERNAL_SECRET:}
admin: admin:
secret: ${GATEWAY_ADMIN_SECRET:} secret: ${GATEWAY_ADMIN_SECRET:}
+2 -2
View File
@@ -9,7 +9,7 @@ spring:
driver-class-name: org.postgresql.Driver driver-class-name: org.postgresql.Driver
url: ${DB_URL:jdbc:postgresql://127.0.0.1:5432/iot_platform} url: ${DB_URL:jdbc:postgresql://127.0.0.1:5432/iot_platform}
username: ${DB_USERNAME:root} username: ${DB_USERNAME:root}
password: ${DB_PASSWORD:} password: ${DB_PASSWORD:Dzaiot123456}
mybatis-plus: mybatis-plus:
configuration: configuration:
@@ -28,7 +28,7 @@ gateway:
token-expire-seconds: ${GATEWAY_TOKEN_EXPIRE:7200} token-expire-seconds: ${GATEWAY_TOKEN_EXPIRE:7200}
# 核心物联后端内网地址与专线通信密钥 # 核心物联后端内网地址与专线通信密钥
backend: 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:} internal-secret: ${GATEWAY_INTERNAL_SECRET:}
# 管理员运维密钥通道 # 管理员运维密钥通道
admin: admin: