Eric Bower
·
2026-05-31
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
15set -euo pipefail
16
17ARTIFACT_DIR="${ARTIFACT_DIR:-/tmp/pici-artifacts}"
18PGS_USERNAME="${PGS_USERNAME:-}"
19RECIPIENT="${RECIPIENT:-${MSMTP_RECIPIENT:-}}"
20ACCOUNT="${MSMTP_ACCOUNT:-fastmail}"
21
22if [ -z "$PGS_USERNAME" ]; then
23 echo "ERROR: set PGS_USERNAME env var" >&2
24 exit 1
25fi
26
27if [ -z "$RECIPIENT" ]; then
28 echo "ERROR: set RECIPIENT or MSMTP_RECIPIENT env var" >&2
29 exit 1
30fi
31
32while 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"
75done