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

如何在Jenkins脚本化流水线中以主机为执行环境部署Docker化Solr+MySQL测试环境

Solution: Jenkins Scripted Pipeline with Dockerized Solr/MySQL (Using Jenkins Host as Execution Env)

Got it, let's break this down. Since the official multi-container approach uses a container as the main execution environment (which lacks PHP/Ant for your tests), we'll instead use the Jenkins host (or agent) as the runtime for your test tools, and spin up Solr/MySQL as temporary Docker containers to serve as your test dependencies.

Prerequisites First

Before diving into the Jenkinsfile, make sure these are set up:

  • The Jenkins node (master or agent) running your pipeline has Docker installed
  • The jenkins user on that node has permission to run Docker commands (add it to the docker group)
  • PHP and Ant are already installed on the Jenkins node (since that's your execution environment)

Full Jenkinsfile Example

Here's a scripted pipeline that handles spinning up the services, running tests, and cleaning up properly:

node {
    // Define container names and configs for easy modification
    def mysqlContainerName = "test-mysql-${BUILD_NUMBER}"
    def solrContainerName = "test-solr-${BUILD_NUMBER}"
    def mysqlRootPassword = "testpass123"
    def testDbName = "mytestdb"

    try {
        stage('Checkout Code') {
            checkout scm // Replace with your repo checkout logic if needed
        }

        stage('Start MySQL Container') {
            sh """
                docker run -d \
                  --name ${mysqlContainerName} \
                  -p 3306:3306 \
                  -e MYSQL_ROOT_PASSWORD=${mysqlRootPassword} \
                  -e MYSQL_DATABASE=${testDbName} \
                  mysql:8.0
            """
            // Wait for MySQL to be ready (critical—don't skip this!)
            sh """
                until mysql -h localhost -u root -p${mysqlRootPassword} -e "SELECT 1"; do
                    echo "Waiting for MySQL to start..."
                    sleep 5
                done
            """
        }

        stage('Start Solr Container') {
            sh """
                docker run -d \
                  --name ${solrContainerName} \
                  -p 8983:8983 \
                  solr:9.0 \
                  solr-create -c mytestcore
            """
            // Wait for Solr core to be available
            sh """
                until curl -s http://localhost:8983/solr/mytestcore/admin/ping | grep -q "OK"; do
                    echo "Waiting for Solr core to load..."
                    sleep 5
                done
            """
        }

        stage('Run Tests with Ant/PHP') {
            // Pass connection details to your test suite (env vars or config file)
            withEnv([
                "DB_HOST=localhost",
                "DB_USER=root",
                "DB_PASS=${mysqlRootPassword}",
                "DB_NAME=${testDbName}",
                "SOLR_URL=http://localhost:8983/solr/mytestcore"
            ]) {
                sh "ant test" // Replace with your actual test command
            }
        }
    } finally {
        stage('Clean Up Docker Containers') {
            // Stop and remove containers regardless of test success/failure
            sh "docker stop ${mysqlContainerName} ${solrContainerName} || true"
            sh "docker rm ${mysqlContainerName} ${solrContainerName} || true"
            // Optional: Clean up unused images if needed
            // sh "docker system prune -f"
        }
    }
}

Key Details to Note

  • Container Naming: We append ${BUILD_NUMBER} to container names to avoid conflicts between concurrent builds.
  • Service Readiness Checks: The until loops ensure we don't run tests before MySQL/Solr are fully operational—this prevents flaky test failures.
  • Environment Variables: We pass connection details to the test suite using withEnv() so your PHP tests can pick them up easily.
  • Cleanup: The finally block guarantees containers are stopped and removed even if the pipeline fails (no leftover containers cluttering your node).

Why This Works Instead of the Official Multi-Container Approach

The official docs focus on using a container as the main execution environment, but since you need PHP/Ant (which aren't present in Solr/MySQL images), running Docker commands directly from the Jenkins node lets you use the node's pre-installed tools while still getting isolated test dependencies via containers.

内容的提问来源于stack exchange,提问作者M.Salomon

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:32:14