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