@@ -3,10 +3,6 @@ name: grafana annotation
33on :
44 workflow_call :
55 inputs :
6- environment :
7- description : Environment
8- required : false
9- type : string
106 grafanaAnnotationTags :
117 description : Custom annotation tags
128 required : false
2824 outputs :
2925 annotation_id :
3026 description : Annotation Id
31- value : ${{ jobs.grafana.outputs.annotation_id }}
27+ value : ${{ jobs.grafana.outputs.annotation_id }}
3228
3329jobs :
3430 grafana :
3531 runs-on : ubuntu-latest
3632 outputs :
3733 annotation_id : ${{ steps.grafana.outputs.annotation-id }}
3834 steps :
39- - name : add Grafana annotation
35+ - name : Add grafana annotation
4036 id : grafana
41- uses : hexionas/grafana-annotation-action@v1.0.1
42- with :
43- grafanaHost : " https://grafana.apify.dev"
44- grafanaToken : ${{ secrets.grafanaApiToken }}
45- grafanaText : ${{ inputs.grafanaAnnotationText }}
46- grafanaTags : ${{ inputs.grafanaAnnotationTags }}
47- grafanaAnnotationID : ${{ inputs.grafanaAnnotationId }}
37+ env :
38+ GRAFANA_ANNOTATION_ID : ${{ inputs.grafanaAnnotationId }}
39+ GRAFANA_ANNOTATION_TAGS : ${{ inputs.grafanaAnnotationTags }}
40+ GRAFANA_ANNOTATION_TEXT : ${{ inputs.grafanaAnnotationText }}
41+ GRAFANA_TOKEN : ${{ secrets.grafanaApiToken }}
42+ GRAFANA_HOST : " https://grafana.apify.dev"
43+ run : |
44+ set -euo pipefail
45+
46+ endpoint="$GRAFANA_HOST/api/annotations"
47+
48+ # Convert newline-separated tags into a JSON array, dropping empty lines.
49+ tags_json=$(printf '%s' "$GRAFANA_ANNOTATION_TAGS" \
50+ | jq -Rn '[inputs | select(. != "")]')
51+
52+ # Determine whether to create (POST) or close out (PATCH) the annotation.
53+ # PATCH mirrors the archived action: it only updates the annotation's
54+ # end time to "now" (milliseconds since epoch).
55+ if [[ -n "${GRAFANA_ANNOTATION_ID:-}" ]]; then
56+ method='PATCH'
57+ endpoint="$endpoint/$GRAFANA_ANNOTATION_ID"
58+ body=$(jq -n --argjson timeEnd "$(date +%s%3N)" \
59+ '{timeEnd: $timeEnd}')
60+ else
61+ method='POST'
62+ body=$(jq -n \
63+ --arg text "$GRAFANA_ANNOTATION_TEXT" \
64+ --argjson tags "$tags_json" \
65+ '{tags: $tags, text: $text}')
66+ fi
67+
68+ response=$(curl --fail --retry 3 --silent --show-error \
69+ -X "$method" "$endpoint" \
70+ --header "Authorization: Bearer $GRAFANA_TOKEN" \
71+ --header 'Content-Type: application/json' \
72+ --data "$body")
73+
74+ if [[ "$method" == 'POST' ]]; then
75+ id=$(jq -r '.id' <<< "$response")
76+ echo "annotation-id=$id" >> "$GITHUB_OUTPUT"
77+ else
78+ echo "annotation-id=$GRAFANA_ANNOTATION_ID" >> "$GITHUB_OUTPUT"
79+ fi
0 commit comments