name: Push Grafana Dashboard on: workflow_call: inputs: dashboard_file: description: 'Path to the dashboard JSON file in the repository' required: true type: string folder_uid: description: 'The UID of the folder where the dashboard should be placed' required: false type: string default: '' overwrite: description: 'Whether to overwrite an existing dashboard' required: false type: boolean default: true secrets: grafana_token: description: 'Grafana API Token or Service Account token' required: true jobs: push-dashboard: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: Validate dashboard file exists run: | if [ ! -f "${{ inputs.dashboard_file }}" ]; then echo "Error: Dashboard file '${{ inputs.dashboard_file }}' not found." exit 1 fi - name: Push dashboard to Grafana run: | DASHBOARD_JSON=$(cat "${{ inputs.dashboard_file }}") # Construct the payload using jq PAYLOAD=$(echo "$DASHBOARD_JSON" | jq -n \ --argjson dash "$DASHBOARD_JSON" \ --arg folder_uid "${{ inputs.folder_uid }}" \ --argjson overwrite "${{ inputs.overwrite }}" \ '{ dashboard: $dash, folderUid: $folder_uid, overwrite: $overwrite }') # Push to Grafana API curl -s -X POST \ -H "Authorization: Bearer ${{ secrets.grafana_token }}" \ -H "Content-Type: application/json" \ -d "$PAYLOAD" \ "http://grafana.local.chromart.cc/api/dashboards/db" | jq .