|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +stage="${1:-unknown}" |
| 5 | +output="${2:-results/ci_timing_context.json}" |
| 6 | + |
| 7 | +timestamp_to_epoch() { |
| 8 | + local value="$1" |
| 9 | + [ -n "$value" ] || return 1 |
| 10 | + date -u -d "$value" +%s 2>/dev/null |
| 11 | +} |
| 12 | + |
| 13 | +value_with_source() { |
| 14 | + local name="$1" |
| 15 | + local custom_name="CUSTOM_ENV_${name}" |
| 16 | + if [ -n "${!name:-}" ]; then |
| 17 | + printf '%s\t%s\n' "${!name}" "$name" |
| 18 | + return 0 |
| 19 | + fi |
| 20 | + if [ -n "${!custom_name:-}" ]; then |
| 21 | + printf '%s\t%s\n' "${!custom_name}" "$custom_name" |
| 22 | + return 0 |
| 23 | + fi |
| 24 | + printf '\t\n' |
| 25 | +} |
| 26 | + |
| 27 | +if ! command -v jq >/dev/null 2>&1; then |
| 28 | + echo "CI timing context not recorded: jq not found" |
| 29 | + exit 0 |
| 30 | +fi |
| 31 | + |
| 32 | +mkdir -p "$(dirname "$output")" |
| 33 | + |
| 34 | +ci_job_started_at="" |
| 35 | +ci_job_started_at_source="" |
| 36 | +ci_pipeline_created_at="" |
| 37 | +ci_pipeline_created_at_source="" |
| 38 | +parent_pipeline_created_at="" |
| 39 | +parent_pipeline_created_at_source="" |
| 40 | + |
| 41 | +IFS=$'\t' read -r ci_job_started_at ci_job_started_at_source < <(value_with_source "CI_JOB_STARTED_AT") |
| 42 | +IFS=$'\t' read -r ci_pipeline_created_at ci_pipeline_created_at_source < <(value_with_source "CI_PIPELINE_CREATED_AT") |
| 43 | +IFS=$'\t' read -r parent_pipeline_created_at parent_pipeline_created_at_source < <(value_with_source "PARENT_PIPELINE_CREATED_AT") |
| 44 | + |
| 45 | +ci_job_started_epoch="" |
| 46 | +if [ -n "$ci_job_started_at" ]; then |
| 47 | + ci_job_started_epoch=$(timestamp_to_epoch "$ci_job_started_at" || true) |
| 48 | +fi |
| 49 | + |
| 50 | +jq -n \ |
| 51 | + --arg stage "$stage" \ |
| 52 | + --arg collected_at "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ |
| 53 | + --arg ci_job_started_at "$ci_job_started_at" \ |
| 54 | + --arg ci_job_started_at_source "$ci_job_started_at_source" \ |
| 55 | + --arg ci_job_started_epoch "$ci_job_started_epoch" \ |
| 56 | + --arg ci_pipeline_created_at "$ci_pipeline_created_at" \ |
| 57 | + --arg ci_pipeline_created_at_source "$ci_pipeline_created_at_source" \ |
| 58 | + --arg parent_pipeline_created_at "$parent_pipeline_created_at" \ |
| 59 | + --arg parent_pipeline_created_at_source "$parent_pipeline_created_at_source" \ |
| 60 | + ' |
| 61 | + {schema_version: 1, stage: $stage, collected_at: $collected_at} |
| 62 | + + (if $ci_job_started_at != "" then { |
| 63 | + ci_job_started_at: $ci_job_started_at, |
| 64 | + ci_job_started_at_source: $ci_job_started_at_source |
| 65 | + } else {} end) |
| 66 | + + (if $ci_job_started_epoch != "" then { |
| 67 | + ci_job_started_epoch: ($ci_job_started_epoch | tonumber) |
| 68 | + } else {} end) |
| 69 | + + (if $ci_pipeline_created_at != "" then { |
| 70 | + ci_pipeline_created_at: $ci_pipeline_created_at, |
| 71 | + ci_pipeline_created_at_source: $ci_pipeline_created_at_source |
| 72 | + } else {} end) |
| 73 | + + (if $parent_pipeline_created_at != "" then { |
| 74 | + parent_pipeline_created_at: $parent_pipeline_created_at, |
| 75 | + parent_pipeline_created_at_source: $parent_pipeline_created_at_source |
| 76 | + } else {} end) |
| 77 | + ' > "$output" |
| 78 | + |
| 79 | +echo "Recorded CI timing context: stage=${stage} ci_job_started_at=${ci_job_started_at:-not_available}" |
0 commit comments