求助:Jenkins中值不符合条件时触发任务失败的Shell脚本及插件推荐
Hey there! Let's tackle your two Jenkins + Shell scripting questions one by one—they’re super common scenarios, so I’ve got you covered:
1. Shell Script to Fail Jenkins Job When a Value Doesn’t Match
The core thing to remember here is: Jenkins marks a job as failed if any step returns a non-zero exit code. So we just need to add logic that checks for your value mismatch, prints a clear message to the logs, then exits with a non-zero code.
Here’s a practical example:
#!/bin/bash # Replace these with your actual value retrieval logic and expected value CHECK_VALUE=$(curl -s https://your-api-endpoint | jq '.status') # Example: Get value from API EXPECTED_VALUE="success" if [ "$CHECK_VALUE" != "$EXPECTED_VALUE" ]; then echo "❌ ERROR: Value mismatch! Received '$CHECK_VALUE', but expected '$EXPECTED_VALUE'" exit 1 # Non-zero exit code triggers Jenkins job failure fi # If we reach here, the value matches—proceed with other steps echo "✅ Value matches successfully. Continuing job execution..."
- The
exit 1command is critical here—Jenkins will pick up this non-zero code and flag the job as failed immediately. - The error message will show up directly in the Jenkins console logs, so you can quickly diagnose the issue.
2. Fail Job When Value Is Not Greater Than a Reference Value from Console Logs
This requires two parts: extracting the reference value from your existing console logs, then comparing it to your target value. Let’s break it down:
Step 1: Extract Reference Value from Logs
First, you need to parse the console log to grab your reference number. For example, if your logs have a line like Performance threshold: 200, use grep and awk to extract the number:
#!/bin/bash # Extract reference value from console logs (adjust grep pattern to match your log format) # If logs are saved to a file: REFERENCE_VALUE=$(grep "Performance threshold:" jenkins_build_log.txt | awk '{print $3}') # Or if you’re capturing output directly from a previous command: # REFERENCE_VALUE=$(run_performance_test.sh | grep "Performance threshold:" | awk '{print $3}') # Get your current value to compare (replace with your actual value source) CURRENT_VALUE=$(get_current_performance_score.sh)
Step 2: Numeric Comparison & Job Failure Logic
Use numeric comparison operators (for integers) or bc (for floating-point numbers) to check if the current value is not greater than the reference:
# For integer comparisons if [ "$CURRENT_VALUE" -le "$REFERENCE_VALUE" ]; then echo "❌ ERROR: Current value ($CURRENT_VALUE) is not greater than reference threshold ($REFERENCE_VALUE)" exit 1 fi # For floating-point numbers (use bc to handle decimal values) # if (( $(echo "$CURRENT_VALUE <= $REFERENCE_VALUE" | bc -l) )); then # echo "❌ ERROR: Current value ($CURRENT_VALUE) is not greater than reference threshold ($REFERENCE_VALUE)" # exit 1 # fi
Recommended Jenkins Plugins
If you want to simplify this without writing full Shell scripts, these plugins are a huge help:
- Pipeline Utility Steps Plugin: Adds handy steps to Pipeline scripts, like
readFileto access logs, orgrepto extract values directly in Jenkinsfile syntax—no messy Shell parsing needed. - Conditional BuildStep Plugin: Perfect for freestyle projects. Lets you add conditional steps via the Jenkins UI (e.g., "If this variable is ≤ reference value, mark job as failed") without writing Shell code.
- Text Finder Plugin: Scans console logs for specific patterns or values. You can configure it to mark the job as failed if it detects your error message, or even set up numeric threshold checks directly through the plugin settings.
内容的提问来源于stack exchange,提问作者Bobby

