Skip to content

Instantly share code, notes, and snippets.

@sergekukharev
Created February 11, 2025 14:16
Show Gist options
  • Save sergekukharev/9c29a28aa4be62bd3a3392d1dfa2c06c to your computer and use it in GitHub Desktop.
Save sergekukharev/9c29a28aa4be62bd3a3392d1dfa2c06c to your computer and use it in GitHub Desktop.
GitHub action to report metric to grafana
name: Deployment Metrics Reporter
on:
workflow_call:
inputs:
service_name:
required: true
type: string
description: 'Name of the service being deployed'
environment:
required: true
type: string
description: 'Environment being deployed to (e.g., production, staging)'
grafana_host:
required: true
type: string
description: 'Grafana host URL'
jobs:
report-metrics:
runs-on: ubuntu-latest
steps:
- name: Report Deployment Metric
env:
GRAFANA_API_KEY: ${{ secrets.GRAFANA_API_KEY }}
run: |
current_time=$(date +%s%N | cut -b1-13) # Current time in milliseconds
# Prepare the metrics payload
metrics_json=$(cat <<EOF
{
"metrics": [
{
"name": "deployment_count",
"value": 1,
"timestamp": $current_time,
"tags": {
"service": "${{ inputs.service_name }}",
"environment": "${{ inputs.environment }}",
"repository": "${{ github.repository }}",
"workflow": "${{ github.workflow }}"
}
}
]
}
EOF
)
# Send metrics to Grafana
curl -X POST "${{ inputs.grafana_host }}/api/v1/push/metrics" \
-H "Authorization: Bearer $GRAFANA_API_KEY" \
-H "Content-Type: application/json" \
-d "$metrics_json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment