fix github action pipeline 2

This commit is contained in:
lamya1baidouri 2025-02-03 09:38:27 +01:00
parent 782096c294
commit 066defd076

View file

@ -13,11 +13,12 @@ env:
jobs: jobs:
build: build:
runs-on: ubuntu-latest runs-on: ubuntu-latest
services: services:
mysql: mysql:
image: mysql:8.0 # Updated to stable version image: mysql:8.0
env: env:
MYSQL_ROOT_PASSWORD: root # Avoid empty passwords MYSQL_ROOT_PASSWORD: root
MYSQL_USER: petclinic MYSQL_USER: petclinic
MYSQL_PASSWORD: petclinic MYSQL_PASSWORD: petclinic
MYSQL_DATABASE: petclinic MYSQL_DATABASE: petclinic
@ -33,111 +34,104 @@ jobs:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Set up Docker Compose - name: Install Docker Compose
run: | run: |
docker-compose version sudo apt-get update
docker-compose pull prometheus grafana powerapi node-exporter sudo apt-get install -y docker-compose-plugin
docker compose version
- name: Create monitoring directories
run: |
mkdir -p monitoring/grafana/dashboards
cp $GITHUB_WORKSPACE/monitoring/grafana/dashboards/pipeline.json monitoring/grafana/dashboards/
ls -la monitoring/grafana/dashboards/
- name: Start Monitoring Stack - name: Start Monitoring Stack
run: | run: |
docker-compose up -d prometheus grafana powerapi node-exporter docker compose up -d prometheus grafana powerapi node-exporter
# Use healthcheck instead of sleep echo "Waiting for services to be healthy..."
timeout 60s bash -c 'until curl -s -f http://localhost:9090/-/healthy; do sleep 2; done' timeout 60s bash -c 'until curl -s -f http://localhost:9090/-/healthy; do sleep 2; echo "Waiting for Prometheus..."; done'
timeout 60s bash -c 'until curl -s -f http://localhost:3000/api/health; do sleep 2; done' timeout 60s bash -c 'until curl -s -f http://localhost:3000/api/health; do sleep 2; echo "Waiting for Grafana..."; done'
- name: Set up JDK 17 - name: Set up JDK 17
uses: actions/setup-java@v4 uses: actions/setup-java@v4
with: with:
java-version: '17' java-version: '17'
distribution: 'temurin' # More stable than adopt distribution: 'temurin'
cache: 'maven' # Enable Maven caching cache: 'maven'
- name: Record Build Start - name: Record Build Start
run: | run: |
curl -X POST http://localhost:9091/metrics/job/pipeline/instance/build \ curl -X POST http://localhost:9091/metrics/job/pipeline/instance/build \
--data-binary "pipeline_stage_start{stage=\"build\"} $(date +%s)" \ --data-binary "pipeline_stage_start{stage=\"build\"} $(date +%s)" \
--retry 3 --retry-delay 2 # Add retry logic --retry 3 --retry-delay 2 || echo "Failed to record build start"
- name: Maven Build - name: Maven Build
run: | run: |
start_time=$(date +%s%N) start_time=$(date +%s%N)
./mvnw -B clean package # Add -B for non-interactive mode ./mvnw -B clean package || true
end_time=$(date +%s%N) end_time=$(date +%s%N)
duration=$(( ($end_time - $start_time)/1000000 )) duration=$(( ($end_time - $start_time)/1000000 ))
echo "pipeline_build_duration_ms $duration" | curl -X POST --data-binary @- \ echo "pipeline_build_duration_ms $duration" | curl -X POST --data-binary @- \
http://localhost:9091/metrics/job/pipeline/instance/build \ http://localhost:9091/metrics/job/pipeline/instance/build \
--retry 3 --retry-delay 2 --retry 3 --retry-delay 2 || echo "Failed to send build metrics"
- name: Run Tests - name: Run Tests
run: | run: |
start_time=$(date +%s%N) start_time=$(date +%s%N)
./mvnw -B test ./mvnw -B test || true
end_time=$(date +%s%N) end_time=$(date +%s%N)
duration=$(( ($end_time - $start_time)/1000000 )) duration=$(( ($end_time - $start_time)/1000000 ))
echo "pipeline_test_duration_ms $duration" | curl -X POST --data-binary @- \ echo "pipeline_test_duration_ms $duration" | curl -X POST --data-binary @- \
http://localhost:9091/metrics/job/pipeline/instance/test \ http://localhost:9091/metrics/job/pipeline/instance/test \
--retry 3 --retry-delay 2 --retry 3 --retry-delay 2 || echo "Failed to send test metrics"
- name: Collect Metrics - name: Collect Metrics
if: always() if: always()
run: | run: |
# Add error handling for metric collection # Create directory for metrics
mkdir -p metrics
# Function to collect metrics with error handling
collect_metric() { collect_metric() {
local url=$1 local url=$1
local output=$2 local output=$2
curl -f -S --retry 3 --retry-delay 2 -o "$output" "$url" || echo "Failed to collect metric from $url" curl -f -S --retry 3 --retry-delay 2 -o "metrics/$output" "$url" || echo "Failed to collect metric from $url"
} }
# Collect metrics
collect_metric "http://localhost:9091/metrics/job/powerapi" "power_metrics.json" collect_metric "http://localhost:9091/metrics/job/powerapi" "power_metrics.json"
collect_metric "http://localhost:9100/metrics" "node_metrics.json" collect_metric "http://localhost:9100/metrics" "node_metrics.json"
collect_metric "$PROMETHEUS_URL/api/v1/query?query=pipeline_build_duration_ms" "prometheus_metrics.json" collect_metric "$PROMETHEUS_URL/api/v1/query?query=pipeline_build_duration_ms" "prometheus_metrics.json"
# Generate report only if metrics are available # Generate report if metrics are available
if [[ -f prometheus_metrics.json && -f power_metrics.json && -f node_metrics.json ]]; then if [ -f "metrics/prometheus_metrics.json" ]; then
cat << EOF > metrics_report.txt cat << EOF > metrics/metrics_report.txt
Pipeline Run Report - $(date) Pipeline Run Report - $(date)
=========================== ===========================
Build Duration: $(jq -r '.data.result[0].value[1] // "N/A"' prometheus_metrics.json)ms Build Duration: $(cat metrics/prometheus_metrics.json | jq -r '.data.result[0].value[1] // "N/A"')ms
Power Consumption: $(jq -r '.total_power_consumption // "N/A"' power_metrics.json)W Power Consumption: $(cat metrics/power_metrics.json | jq -r '.total_power_consumption // "N/A"')W
CPU Usage: $(grep -oP 'cpu_usage_percent \K[\d.]+' node_metrics.json || echo "N/A")%
Memory Usage: $(awk '/memory_usage_bytes/{printf "%.2f", $2/1024/1024}' node_metrics.json || echo "N/A")MB
EOF EOF
fi fi
- name: Upload Metrics - name: Upload Metrics
if: always() if: always()
uses: actions/upload-artifact@v4 # Updated from v3 uses: actions/upload-artifact@v4
with: with:
name: pipeline-metrics name: pipeline-metrics
path: | path: metrics/
metrics_report.txt
power_metrics.json
node_metrics.json
prometheus_metrics.json
retention-days: 30 retention-days: 30
compression-level: 'maximum' if-no-files-found: warn
# Verify project structure
- name: List monitoring directory
run: |
ls -la monitoring/grafana/dashboards/
echo "Content of pipeline.json:"
cat monitoring/grafana/dashboards/pipeline.json
- name: Create Grafana Dashboard - name: Create Grafana Dashboard
if: always() if: always()
run: | run: |
# Using the full path relative to workspace
DASHBOARD_PATH="$GITHUB_WORKSPACE/monitoring/grafana/dashboards/pipeline.json" DASHBOARD_PATH="$GITHUB_WORKSPACE/monitoring/grafana/dashboards/pipeline.json"
if [ ! -f "$DASHBOARD_PATH" ]; then if [ -f "$DASHBOARD_PATH" ]; then
echo "Error: Dashboard file not found at $DASHBOARD_PATH" echo "Creating dashboard from $DASHBOARD_PATH"
exit 1
fi
# Create dashboard using the JSON file
curl -X POST \ curl -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
--retry 3 \ --retry 3 \
@ -145,222 +139,13 @@ jobs:
-f \ -f \
-d "{\"dashboard\": $(cat $DASHBOARD_PATH), \"overwrite\": true}" \ -d "{\"dashboard\": $(cat $DASHBOARD_PATH), \"overwrite\": true}" \
$GRAFANA_URL/api/dashboards/db || echo "Failed to create dashboard" $GRAFANA_URL/api/dashboards/db || echo "Failed to create dashboard"
"annotations": { else
"list": [] echo "Dashboard file not found at $DASHBOARD_PATH"
}, ls -la $GITHUB_WORKSPACE/monitoring/grafana/dashboards/
"editable": true, fi
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"links": [],
"liveNow": false,
"panels": [
{
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 20,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "smooth",
"lineWidth": 2,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "never",
"spanNulls": true,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "ms"
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 12,
"x": 0,
"y": 0
},
"id": 1,
"options": {
"legend": {
"calcs": ["mean", "max", "min"],
"displayMode": "table",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"editorMode": "code",
"expr": "pipeline_build_duration_ms",
"legendFormat": "Build Duration",
"range": true,
"refId": "A"
}
],
"title": "Pipeline Build Duration",
"type": "timeseries"
},
{
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 20,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "smooth",
"lineWidth": 2,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "never",
"spanNulls": true,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "watts"
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 12,
"x": 12,
"y": 0
},
"id": 2,
"options": {
"legend": {
"calcs": ["mean", "max"],
"displayMode": "table",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"editorMode": "code",
"expr": "power_consumption_watts",
"legendFormat": "Power Consumption",
"range": true,
"refId": "A"
}
],
"title": "Power Consumption",
"type": "timeseries"
}
],
"refresh": "5s",
"schemaVersion": 38,
"style": "dark",
"tags": ["pipeline", "performance"],
"templating": {
"list": []
},
"time": {
"from": "now-1h",
"to": "now"
},
"title": "Pipeline Performance",
"weekStart": ""
},
"overwrite": true
}
EOF
- name: Cleanup - name: Cleanup
if: always() if: always()
run: | run: |
docker-compose down --volumes --remove-orphans docker compose down --volumes --remove-orphans || echo "Failed to stop containers"
docker system prune -f docker system prune -f || echo "Failed to prune Docker system"