You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置远程Quarkus PostgreSQL开发服务并实现自动化运行(含Jenkins集成)?

本地测试自动化实现

1. 配置免密SSH登录远程机器

先将本地SSH公钥同步到远程Podman机器,避免每次输入密码:

ssh-copy-id your-username@remote-podman-host

执行ssh your-username@remote-podman-host验证是否能免密登录。

2. 编写自动化脚本

创建run-quarkus-test.sh脚本,整合远程容器管理、端口转发和本地测试流程:

#!/bin/bash

# 配置参数
REMOTE_HOST="your-username@remote-podman-host"
CONTAINER_NAME="test-postgres"
REMOTE_PORT="5432"
LOCAL_PORT="5432"
DB_USER="testuser"
DB_PASS="testpass"
DB_NAME="testdb"

# 清理远程旧容器(避免冲突)
ssh $REMOTE_HOST "podman stop $CONTAINER_NAME || true && podman rm $CONTAINER_NAME || true"

# 远程启动PostgreSQL容器
ssh $REMOTE_HOST "podman run -d --name $CONTAINER_NAME \
  -p $REMOTE_PORT:$REMOTE_PORT \
  -e POSTGRES_USER=$DB_USER \
  -e POSTGRES_PASSWORD=$DB_PASS \
  -e POSTGRES_DB=$DB_NAME \
  postgres:latest"

# 等待容器初始化完成
sleep 10

# 建立SSH端口转发(后台运行)
ssh -L $LOCAL_PORT:localhost:$REMOTE_PORT $REMOTE_HOST -N &
SSH_PID=$!

# 运行本地Quarkus测试
./mvnw test -Dquarkus.datasource.jdbc.url=jdbc:postgresql://localhost:$LOCAL_PORT/$DB_NAME \
  -Dquarkus.datasource.username=$DB_USER \
  -Dquarkus.datasource.password=$DB_PASS

# 清理资源
kill $SSH_PID
ssh $REMOTE_HOST "podman stop $CONTAINER_NAME && podman rm $CONTAINER_NAME"

给脚本添加执行权限:chmod +x run-quarkus-test.sh,直接运行即可完成全流程自动化。


Jenkins流水线实现方案

1. 前置准备

  • 在Jenkins「凭证管理」中添加远程Podman机器的SSH凭证(用户名+私钥),凭证ID设为remote-podman-creds
  • 安装Jenkins插件SSH Pipeline Steps,用于远程执行命令

2. Jenkinsfile示例

pipeline {
  agent any

  environment {
    REMOTE_HOST = 'your-username@remote-podman-host'
    CONTAINER_NAME = 'jenkins-test-postgres'
    REMOTE_PORT = '5432'
    LOCAL_PORT = '5432'
    DB_USER = 'testuser'
    DB_PASS = 'testpass'
    DB_NAME = 'testdb'
  }

  stages {
    stage('初始化远程PostgreSQL') {
      steps {
        script {
          // 清理旧容器
          sshCommand(
            credentialsId: 'remote-podman-creds',
            host: REMOTE_HOST,
            command: "podman stop ${CONTAINER_NAME} || true && podman rm ${CONTAINER_NAME} || true"
          )
          // 启动新容器
          sshCommand(
            credentialsId: 'remote-podman-creds',
            host: REMOTE_HOST,
            command: "podman run -d --name ${CONTAINER_NAME} \
              -p ${REMOTE_PORT}:${REMOTE_PORT} \
              -e POSTGRES_USER=${DB_USER} \
              -e POSTGRES_PASSWORD=${DB_PASS} \
              -e POSTGRES_DB=${DB_NAME} \
              postgres:latest"
          )
          // 等待容器就绪
          sleep time: 10, unit: 'SECONDS'
        }
      }
    }

    stage('运行Quarkus测试') {
      steps {
        script {
          // 启动SSH端口转发
          sh "ssh -L ${LOCAL_PORT}:localhost:${REMOTE_PORT} ${REMOTE_HOST} -N &"
          def sshPid = sh(script: 'echo $!', returnStdout: true).trim()
          
          // 执行测试
          sh "./mvnw test -Dquarkus.datasource.jdbc.url=jdbc:postgresql://localhost:${LOCAL_PORT}/${DB_NAME} \
            -Dquarkus.datasource.username=${DB_USER} \
            -Dquarkus.datasource.password=${DB_PASS}"
          
          // 关闭端口转发
          sh "kill ${sshPid}"
        }
      }
    }
  }

  post {
    always {
      // 无论测试结果如何,强制清理远程容器
      sshCommand(
        credentialsId: 'remote-podman-creds',
        host: REMOTE_HOST,
        command: "podman stop ${CONTAINER_NAME} || true && podman rm ${CONTAINER_NAME} || true"
      )
    }
  }
}

内容的提问来源于stack exchange,提问作者Cristiano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 18:35:13