Commit 8ad04da

Eric Bower  ·  2026-05-31 10:29:07 -0400 EDT
parent 423fd53
feat: email monitor script
1 files changed,  +75, -0
+75, -0
......@@ -0,0 +1,75 @@
1+#!/usr/bin/env bash
2+# pici monitor | monitor-email.sh
3+# Print job status, workspace, artifact_dest, and artifact URL for terminal jobs.
4+#
5+# Usage:
6+# pici monitor | ./scripts/monitor-email.sh
7+#
8+# Or with a time window:
9+# timeout 300 pici monitor | ./scripts/monitor-email.sh
10+#
11+# Env vars:
12+# PGS_USERNAME - username for the artifact URL (e.g. "erock")
13+# ARTIFACT_DIR - path to artifacts root (default: /tmp/pici-artifacts)
14+
15+set -euo pipefail
16+
17+ARTIFACT_DIR="${ARTIFACT_DIR:-/tmp/pici-artifacts}"
18+PGS_USERNAME="${PGS_USERNAME:-}"
19+RECIPIENT="${RECIPIENT:-${MSMTP_RECIPIENT:-}}"
20+ACCOUNT="${MSMTP_ACCOUNT:-fastmail}"
21+
22+if [ -z "$PGS_USERNAME" ]; then
23+ echo "ERROR: set PGS_USERNAME env var" >&2
24+ exit 1
25+fi
26+
27+if [ -z "$RECIPIENT" ]; then
28+ echo "ERROR: set RECIPIENT or MSMTP_RECIPIENT env var" >&2
29+ exit 1
30+fi
31+
32+while IFS= read -r line; do
33+ status=$(echo "$line" | jq -r '.status // empty')
34+ [ -z "$status" ] && continue
35+
36+ # Only terminal statuses
37+ [[ "$status" == "success" || "$status" == "failed" ]] || continue
38+
39+ name=$(echo "$line" | jq -r '.name')
40+ job_id=$(echo "$line" | jq -r '.job_id')
41+
42+ # Enrich with event.json source/dest
43+ event_file="${ARTIFACT_DIR}/${name}/${job_id}/artifacts/event.json"
44+ workspace=""
45+ artifact_dest=""
46+ if [ -f "$event_file" ]; then
47+ workspace=$(jq -r '.workspace // ""' "$event_file")
48+ artifact_dest=$(jq -r '.artifact_dest // ""' "$event_file")
49+ fi
50+
51+ artifact_url=""
52+ if [ -n "$artifact_dest" ]; then
53+ # artifact_dest is e.g. pgs.sh:/<project>/<folder>/
54+ # extract project and folder, reconstruct as https://<username>-<project>.pgs.sh/<folder>/
55+ path="${artifact_dest#pgs.sh:}"
56+ path="${path#/}" # strip leading slash
57+ project="${path%%/*}" # first component
58+ folder="${path#*/}" # remainder
59+ folder="${folder%/}" # strip trailing slash
60+ if [ -n "$project" ] && [ -n "$folder" ] && [ "$project" != "$path" ]; then
61+ artifact_url="https://${PGS_USERNAME}-${project}.pgs.sh/${folder}/"
62+ fi
63+ fi
64+
65+ echo "status: $status"
66+ echo "workspace: $workspace"
67+ echo "artifact_dest: $artifact_dest"
68+ [ -n "$artifact_url" ] && echo "artifacts: $artifact_url"
69+ echo "---"
70+
71+ # Send email
72+ body="status: $status\nworkspace: $workspace\nartifact_dest: $artifact_dest"
73+ [ -n "$artifact_url" ] && body="${body}\nartifacts: $artifact_url"
74+ echo -e "Subject: pici ${name} ${status}\n\n${body}" | msmtp -a "$ACCOUNT" "$RECIPIENT"
75+done