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

寻求DevOps协助:Docker部署Jenkins的Spring Boot Pipeline配置

Hey there! Let's walk through setting this up step by step. You've got the right approach using Docker for Jenkins and spinning up dependent services during your pipeline—here's how to make it all work smoothly.

1. Prepare Your Jenkins Docker Environment

First, you need to run Jenkins in a Docker container that can access the host's Docker daemon (so it can start Postgres/RabbitMQ containers). Use this command to launch Jenkins:

docker run -d \
  --name jenkins \
  -p 8080:8080 \
  -p 50000:50000 \
  -v jenkins_home:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v $(which docker):/usr/bin/docker \
  jenkins/jenkins:lts-jdk17
  • Mounting /var/run/docker.sock lets the Jenkins container use the host's Docker engine to spawn other containers.
  • We also mount the host's Docker binary so Jenkins can run docker and docker-compose commands.

Next, install these essential plugins in Jenkins:

  • Docker Pipeline: For working with Docker in your pipeline
  • Git: To pull your Spring Boot code
  • Maven/Gradle: Depending on your build tool (we'll use Maven in examples below)
2. Jenkinsfile with Postgres & RabbitMQ for Testing

We'll use docker-compose to manage the dependent services—it's cleaner than running individual docker run commands and lets you define health checks to ensure services are ready before tests run.

Step 2.1: Add a docker-compose.yml to Your Repo

Create this file in your Spring Boot project root to define Postgres and RabbitMQ:

version: '3.8'
services:
  postgres:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: spring_boot_test_db
      POSTGRES_USER: test_user
      POSTGRES_PASSWORD: test_pass
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U test_user -d spring_boot_test_db"]
      interval: 5s
      timeout: 5s
      retries: 5

  rabbitmq:
    image: rabbitmq:3-management-alpine
    ports:
      - "5672:5672"
    healthcheck:
      test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5

Step 2.2: The Jenkinsfile

Here's the complete pipeline that checks out code, starts dependencies, runs tests, builds the app, and cleans up:

pipeline {
    agent any

    stages {
        stage('Checkout Code') {
            steps {
                git url: 'https://your-git-repo-url/your-spring-boot-app.git', branch: 'main'
            }
        }

        stage('Start Dependent Services') {
            steps {
                script {
                    // Start Postgres and RabbitMQ in detached mode
                    sh 'docker-compose up -d postgres rabbitmq'
                    // Wait for services to be healthy (requires docker-compose 1.29+)
                    sh 'docker-compose wait postgres rabbitmq'
                }
            }
        }

        stage('Run Integration Tests') {
            environment {
                // Pass connection details to Spring Boot via environment variables
                SPRING_DATASOURCE_URL = 'jdbc:postgresql://localhost:5432/spring_boot_test_db'
                SPRING_DATASOURCE_USERNAME = 'test_user'
                SPRING_DATASOURCE_PASSWORD = 'test_pass'
                SPRING_RABBITMQ_HOST = 'localhost'
                SPRING_RABBITMQ_PORT = '5672'
            }
            steps {
                // Run tests with Maven (adjust for Gradle if needed)
                sh './mvnw test'
            }
        }

        stage('Build Production JAR') {
            steps {
                // Build the JAR, skipping tests since we already ran them
                sh './mvnw clean package -DskipTests'
            }
            post {
                success {
                    // Archive the built JAR for later use
                    archiveArtifacts artifacts: 'target/*.jar', fingerprint: true
                }
            }
        }
    }

    post {
        always {
            // Clean up containers regardless of pipeline success/failure
            sh 'docker-compose down'
        }
    }
}
3. Key Notes for Reliability
  • Health Checks Over Sleep: Using docker-compose wait ensures we don't run tests until Postgres/RabbitMQ are fully ready—way more reliable than a hardcoded sleep.
  • Environment Variables: Make sure your Spring Boot application-test.properties uses these environment variables (e.g., spring.datasource.url=${SPRING_DATASOURCE_URL}) so tests connect to the right services.
  • Permissions: If Jenkins can't run Docker commands, adjust the host's Docker socket permissions temporarily with sudo chmod 666 /var/run/docker.sock (for testing) or add the Jenkins user to the Docker group for a more secure setup.
  • Cleanup: The post { always {} } block ensures containers are stopped and removed even if the pipeline fails, preventing leftover resources.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:55:05