Jenkins Pipeline全局变量配置及测试阶段并发构建控制问询
Solution for Jenkins Pipeline Pre-Build Test Completion Check
Got it, let's tackle this Jenkins Pipeline problem. The core goal here is to avoid wasting resources building unnecessary binaries by making sure the third-party test stage from the last build has either finished or exceeded its maximum 3-hour runtime before starting a new build. Here's a practical, maintainable solution using Jenkins Pipeline's built-in variables and your existing global configuration:
Key Approach
We'll add a pre-build check stage as the first step in your pipeline. This stage will:
- Fetch the most recent previous build
- Locate the third-party test stage in that build
- Verify if the test has completed (regardless of success/failure) or has run longer than the 3-hour maximum
- Abort the current build if the test is still running and within the allowed time window
Pipeline Code Implementation
pipeline { agent any stages { // Pre-build check to validate previous test completion stage('Pre-Build: Validate Previous Test Status') { steps { script { def previousBuild = currentBuild.previousBuild def testStageName = "Automated testing with the third party application" // Use your global variable here instead of hardcoding, e.g., globalVars.maxTestDurationHours def maxTestDurationHours = 3 // Handle first build scenario (no previous build exists) if (!previousBuild) { echo "✅ No previous build found, proceeding with current build." return } // Locate the test stage in the previous build def testStage = previousBuild.stageResults.find { stage -> stage.name == testStageName } if (!testStage) { echo "⚠️ Previous build doesn't contain the test stage, proceeding with current build." return } // Check if test stage has completed if (testStage.status.isCompleted()) { echo "✅ Previous test stage completed with status: ${testStage.status}. Proceeding." } else { // Calculate elapsed time since test started (convert ms to hours) def elapsedHours = (System.currentTimeMillis() - testStage.startTimeInMillis) / (1000 * 60 * 60) def roundedElapsed = elapsedHours.round(2) if (roundedElapsed >= maxTestDurationHours) { echo "⏰ Previous test has exceeded max duration (${roundedElapsed}h / ${maxTestDurationHours}h). Assuming completion, proceeding." } else { echo "❌ Previous test is still running (elapsed: ${roundedElapsed}h). Aborting current build to avoid unnecessary binaries." error "Aborted: Previous third-party test is in progress and within allowed duration." } } } } } // Your existing stages below stage('Build') { steps { echo "Executing build steps..." // Add your build commands here, e.g., `npm run build` or `mvn clean package` } } stage('Binaries Deployment to server') { steps { echo "Deploying binaries to target server..." // Add your deployment logic here } } stage('Automated testing with the third party application') { steps { echo "Running third-party automated tests..." // Your test execution logic here (note: if this runs in the background, adjust the pre-build check to track external state) } } stage('Upload Binaries to JFrog') { steps { echo "Uploading binaries to JFrog Artifactory..." // Add your JFrog upload commands here } } } }
Important Notes
- Global Variable Integration: Replace the hardcoded
maxTestDurationHourswith your existing global variable (e.g.,globalVars.maxTestDuration) to keep config centralized. - Stage Identification: We use the stage name to locate the test stage instead of index numbers—this makes the pipeline more resilient if you ever reorder stages.
- Edge Case Handling: The code accounts for first-time builds and builds where the test stage is missing (e.g., older pipeline versions).
- Background Test Scenario: If your third-party test runs in the background (the stage finishes but the test continues), you'll need to track the test's runtime externally (e.g., store start time in a file/Redis) and adjust the pre-build check to reference that data instead of Jenkins' stage status.
内容的提问来源于stack exchange,提问作者Abhaya Ghatkar
相关产品推荐
相关产品推荐

