Created
February 11, 2025 14:16
-
-
Save sergekukharev/9c29a28aa4be62bd3a3392d1dfa2c06c to your computer and use it in GitHub Desktop.
GitHub action to report metric to grafana
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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