Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 63 additions & 5 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ name: Docker Build and Test
on:
push:
paths:
- 'grafana/**'
- '.github/workflows/workflow.yml'
- "grafana/**"
- ".github/workflows/workflow.yml"
pull_request:
paths:
- 'grafana/**'
- '.github/workflows/workflow.yml'
- "grafana/**"
- ".github/workflows/workflow.yml"

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
Expand Down Expand Up @@ -49,3 +49,61 @@ jobs:
run: |
cd grafana
docker compose down -v

deploy:
runs-on: ubuntu-latest
needs: build
if: github.event_name == 'push' && github.ref == 'refs/heads/main'

steps:
- uses: actions/checkout@v4

- name: Deploy dashboards to Grafana Cloud
env:
GRAFANA_URL: https://softwarecsc65.grafana.net
GRAFANA_TOKEN: ${{ secrets.GRAFANA_SERVICE_ACCOUNT_TOKEN }}
run: |
datasources=$(curl -sf \
-H "Authorization: Bearer $GRAFANA_TOKEN" \
"$GRAFANA_URL/api/datasources") || { echo "ERROR: failed to fetch datasources from Grafana Cloud"; exit 1; }

for file in grafana/dashboards/*.json; do
echo "Deploying $file..."
dashboard=$(jq 'del(.id)' "$file")

# For each datasource plugin listed in __inputs, look up its uid on
# Grafana Cloud by matching on type, then rewrite every panel object
# whose .type matches so its .uid points to the Cloud datasource.
# This works regardless of whether the JSON was exported via
# "JSON Model" (hardcoded uids) or "Export for sharing externally"
# (${DS_VAR} placeholders), because we match on .type, not .uid.
while IFS= read -r plugin_id; do
cloud_uid=$(echo "$datasources" | jq -r --arg t "$plugin_id" \
'.[] | select(.type == $t) | .uid' | head -1)

if [ -z "$cloud_uid" ]; then
echo "ERROR: no datasource with type '$plugin_id' found in Grafana Cloud"
exit 1
fi

dashboard=$(echo "$dashboard" | jq --arg type "$plugin_id" --arg uid "$cloud_uid" \
'walk(if type == "object" and .type == $type and has("uid") then .uid = $uid else . end)')
done < <(echo "$dashboard" | jq -r '.__inputs[]? | .pluginId')

payload=$(jq -n \
--argjson dashboard "$dashboard" \
'{dashboard: $dashboard, overwrite: true, folderUid: ""}')

response=$(curl -s -o /tmp/gf_response.json -w "%{http_code}" \
-X POST "$GRAFANA_URL/api/dashboards/import" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GRAFANA_TOKEN" \
-d "$payload")

if [ "$response" != "200" ]; then
echo "Failed to deploy $file (HTTP $response)"
cat /tmp/gf_response.json
exit 1
fi
echo "Successfully deployed $file"
done
7 changes: 5 additions & 2 deletions grafana/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
MQTT_PASSWORD=yourpassword
MQTT_USERNAME=yourusername
MQTT_PASSWORD=yourpassword
MQTT_HOST=yourhost

POSTGRES_USERNAME=yourusername
POSTGRES_PASSWORD=yourpassword
POSTGRES_HOST=yourhost
POSTGRES_HOST=yourhost
POSTGRES_SSLMODE=disable
Loading
Loading