continuwuity/.forgejo/actions/update-results-ledger/action.yml

161 lines
5.5 KiB
YAML

---
name: "Update Results Ledger"
description: "Appends test results to runs.jsonl and updates badge-main.json on the orphan results branch"
inputs:
target_branch:
description: "Orphan branch to store results on"
default: "_metadata/badges"
github_token:
description: "Token for authenticated push (GITHUB_TOKEN or equivalent)"
required: true
pass_count:
required: true
fail_count:
required: true
skip_count:
required: true
version_string:
required: true
actor:
required: true
binary_path:
description: "Path to the tested binary"
default: "target/debug/conduwuit"
features:
description: "Space-separated list of cargo features used for this build"
default: ""
runs:
using: "composite"
steps:
- name: Append results and update badge
shell: bash
run: |
set -euo pipefail
set -x # echo commands for debugging
PASS="${{ inputs.pass_count }}"
FAIL="${{ inputs.fail_count }}"
SKIP="${{ inputs.skip_count }}"
VERSION_STRING="${{ inputs.version_string }}"
ACTOR="${{ inputs.actor }}"
BINARY_PATH="${{ inputs.binary_path }}"
FEATURES="${{ inputs.features }}"
# Extract commit hash from version string, fall back to env
BIN_VER_HASH=$(echo "$VERSION_STRING" | sed -n 's/.*~\([0-9a-f]\{7,\}\).*/\1/p')
COMMIT_HASH="${BIN_VER_HASH:-${GITHUB_SHA:-unknown}}"
RUN_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
RUN_ID="${COMMIT_HASH}-${RUN_DATE}"
PROVIDER=$([ -n "${FORGEJO_URL:-}" ] && echo "forgejo" || echo "github")
BRANCH_NAME=$(echo "${GITHUB_HEAD_REF:-${GITHUB_REF_NAME:-main}}" | sed 's/[^a-zA-Z0-9.\-_]/_/g')
[ -z "$BRANCH_NAME" ] && BRANCH_NAME="main"
# SHA256 of the tested binary
BIN_SHA="unknown"
[ -f "$BINARY_PATH" ] && BIN_SHA=$(sha256sum "$BINARY_PATH" | awk '{print $1}')
# --- Work on the orphan branch via worktree ---
LEDGER_DIR="$(mktemp -d)"
git config --global user.name "CI Runner"
git config --global user.email "runner@ci.local"
# Configure token-based auth for push
REPO_URL=$(git remote get-url origin)
if [[ "$REPO_URL" == https://* ]]; then
# Insert token into HTTPS URL for authenticated push
AUTHED_URL=$(echo "$REPO_URL" | sed "s|https://|https://token:${{ inputs.github_token }}@|")
git remote set-url origin "$AUTHED_URL"
fi
TARGET_BRANCH="${{ inputs.target_branch }}"
# Fetch the orphan branch using the full refspec
if git fetch origin "refs/heads/${TARGET_BRANCH}:refs/remotes/origin/${TARGET_BRANCH}" 2>/dev/null; then
git worktree add "$LEDGER_DIR" "origin/${TARGET_BRANCH}"
else
# Branch doesn't exist yet — create it as a new orphan
git worktree add --detach "$LEDGER_DIR"
cd "$LEDGER_DIR"
git checkout --orphan "${TARGET_BRANCH}"
git rm -rf . 2>/dev/null || true
echo "ledger.db" > .gitignore
cd -
fi
cd "$LEDGER_DIR"
# Ensure files exist
touch runs.jsonl
# Append run summary
jq -n -c \
--arg run_id "$RUN_ID" \
--arg run_date "$RUN_DATE" \
--arg commit_hash "$COMMIT_HASH" \
--arg branch "$BRANCH_NAME" \
--arg author_name "$ACTOR" \
--arg provider "$PROVIDER" \
--arg binary_sha256 "$BIN_SHA" \
--arg version_string "$VERSION_STRING" \
--arg features "$FEATURES" \
--arg pass "$PASS" \
--arg skip "$SKIP" \
--arg fail "$FAIL" \
'{
run_id: $run_id,
run_date: $run_date,
commit_hash: $commit_hash,
branch: $branch,
author_name: $author_name,
provider: $provider,
version_string: $version_string,
features: $features,
binary_sha256: $binary_sha256,
passed_count: ($pass|tonumber),
skipped_count: ($skip|tonumber),
failed_count: ($fail|tonumber)
}' >> runs.jsonl
# Generate badge JSON (shields.io endpoint schema)
if [ "$FAIL" -gt 0 ]; then COLOR="red"
elif [ "$SKIP" -gt 0 ]; then COLOR="yellow"
else COLOR="green"
fi
# shields.io compatible schema
jq -n \
--arg pass "$PASS" \
--arg skip "$SKIP" \
--arg fail "$FAIL" \
--arg color "$COLOR" \
--arg provider_name "$PROVIDER" \
'{
schemaVersion: 1,
label: "Complement Tests (\($provider_name))",
message: "\($pass) pass, \($skip) skip, \($fail) fail",
color: $color
}' > badge-main.json
# Commit and push, retrying
git add runs.jsonl badge-main.json
if git commit -m "Results for ${COMMIT_HASH} (${PROVIDER}) [skip ci]"; then
MAX_RETRIES=5
RETRY_COUNT=0
until git push origin "HEAD:refs/heads/${TARGET_BRANCH}"; do
RETRY_COUNT=$((RETRY_COUNT + 1))
if [ "$RETRY_COUNT" -ge "$MAX_RETRIES" ]; then
echo "Failed to push after $MAX_RETRIES attempts."
exit 1
fi
echo "Push failed, retrying ($RETRY_COUNT/$MAX_RETRIES)..."
sleep $(( (RANDOM % 5) + 1 ))
git pull --rebase origin "refs/heads/${TARGET_BRANCH}"
done
fi
# Cleanup
cd -
git worktree remove "$LEDGER_DIR" --force 2>/dev/null || true
rm -rf "$LEDGER_DIR"