Skip to content

Workflow Monitor

Workflow Monitor #1009

name: Workflow Monitor
on:
workflow_run:
workflows:
- "Docker Build, Scan, Test"
- "build & test"
- "metadata-io"
types: [completed]
permissions:
actions: write # Required to retrigger workflows
contents: read
jobs:
monitor:
name: Monitor Workflow Completion
runs-on: ubuntu-latest
if: github.event.workflow_run.head_repository.full_name == github.repository
steps:
- name: Print workflow summary
env:
WORKFLOW_NAME: ${{ github.event.workflow_run.name }}
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
WORKFLOW_EVENT: ${{ github.event.workflow_run.event }}
CREATED_AT: ${{ github.event.workflow_run.created_at }}
CONCLUSION: ${{ github.event.workflow_run.conclusion }}
RUN_ATTEMPT: ${{ github.event.workflow_run.run_attempt }}
run: |
{
echo "## Workflow Summary"
echo ""
echo "| Metric | Value |"
echo "|--------|-------|"
echo "| **Workflow Name** | $WORKFLOW_NAME |"
echo "| **Branch** | $HEAD_BRANCH |"
echo "| **Event** | $WORKFLOW_EVENT |"
echo "| **Start Time** | $CREATED_AT |"
echo "| **Completion Status** | $CONCLUSION |"
echo "| **Run Number** | $RUN_ATTEMPT |"
} >> "$GITHUB_STEP_SUMMARY"
echo "Workflow '$WORKFLOW_NAME' completed"
echo " Status: $CONCLUSION"
echo " Branch: $HEAD_BRANCH"
echo " Event: $WORKFLOW_EVENT"
echo " Run Attempt: $RUN_ATTEMPT"
- name: Send workflow metrics to PostHog
if: always()
continue-on-error: true
env:
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
POSTHOG_HOST: ${{ secrets.POSTHOG_HOST }}
WORKFLOW_NAME: ${{ github.event.workflow_run.name }}
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
CREATED_AT: ${{ github.event.workflow_run.created_at }}
UPDATED_AT: ${{ github.event.workflow_run.updated_at }}
CONCLUSION: ${{ github.event.workflow_run.conclusion }}
REPOSITORY: ${{ github.repository }}
RUN_ID: ${{ github.event.workflow_run.id }}
RUN_ATTEMPT: ${{ github.event.workflow_run.run_attempt }}
run: |
if [ -z "$POSTHOG_API_KEY" ]; then
echo "⚠️ POSTHOG_API_KEY not configured, skipping metrics"
exit 0
fi
# Calculate duration in seconds
START_EPOCH=$(date -u -d "$CREATED_AT" +%s)
END_EPOCH=$(date -u -d "$UPDATED_AT" +%s)
DURATION=$((END_EPOCH - START_EPOCH))
echo "Sending workflow metrics to PostHog..."
echo " Duration: ${DURATION}s"
echo " Run Attempt: ${RUN_ATTEMPT}"
# Use POSTHOG_HOST if set, otherwise default to PostHog cloud
POSTHOG_ENDPOINT="${POSTHOG_HOST:-https://app.posthog.com}/capture/"
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$POSTHOG_ENDPOINT" \
-H "Content-Type: application/json" \
-d "{
\"api_key\": \"$POSTHOG_API_KEY\",
\"event\": \"workflow_completed\",
\"properties\": {
\"github_repository\": \"$REPOSITORY\",
\"workflow_name\": \"$WORKFLOW_NAME\",
\"head_branch\": \"$HEAD_BRANCH\",
\"created_at\": \"$CREATED_AT\",
\"duration\": $DURATION,
\"conclusion\": \"$CONCLUSION\",
\"run_id\": \"$RUN_ID\",
\"run_attempt\": $RUN_ATTEMPT
},
\"distinct_id\": \"github-actions-$REPOSITORY\"
}")
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
BODY=$(echo "$RESPONSE" | head -n-1)
if [ "$HTTP_CODE" -eq 200 ]; then
echo "✅ Successfully sent metrics to PostHog"
else
echo "⚠️ PostHog request returned HTTP $HTTP_CODE"
echo "Response: $BODY"
fi
- name: Retrigger docker-unified on first failure
if: |
github.event.workflow_run.name == 'Docker Build, Scan, Test' &&
github.event.workflow_run.conclusion == 'failure' &&
github.event.workflow_run.run_attempt == 1
continue-on-error: true
env:
GH_TOKEN: ${{ github.token }}
WORKFLOW_NAME: ${{ github.event.workflow_run.name }}
RUN_ID: ${{ github.event.workflow_run.id }}
REPOSITORY: ${{ github.repository }}
run: |
{
echo ""
echo "---"
echo ""
echo "## Retry Decision"
echo ""
echo "✅ **Retriggering workflow** (first failure detected)"
echo ""
} >> "$GITHUB_STEP_SUMMARY"
echo "Retriggering '${WORKFLOW_NAME}' (Run ID: ${RUN_ID})..."
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${REPOSITORY}/actions/runs/${RUN_ID}/rerun-failed-jobs"
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo "✅ Successfully triggered workflow retry" | tee -a "$GITHUB_STEP_SUMMARY"
else
echo "❌ Failed to retrigger workflow (exit code: $EXIT_CODE)" | tee -a "$GITHUB_STEP_SUMMARY"
fi