Repository navigation
Expand file tree
/
Copy pathaction.yml
More file actions
82 lines (82 loc) · 3.54 KB
/
Copy pathaction.yml
File metadata and controls
82 lines (82 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
name: codevitals
author: Mainmatter
branding:
icon: activity
color: purple
description: >-
Record the repository's history with codevitals and publish metrics.json as the only
commit of a branch, for a Grafana dashboard to read. Check the repository out
with its full history first (actions/checkout with fetch-depth: 0), and grant
the job `contents: write`.
inputs:
metrics:
description: Metrics to compute, separated by spaces (`cv metrics --help` lists them); all when empty.
default: ""
branch:
description: Branch that receives metrics.json; force-pushed, so it never grows.
default: codevitals-metrics
runs:
using: composite
steps:
- name: Install codevitals
shell: bash
env:
ACTION_PATH: ${{ github.action_path }}
run: |
# Run the release that the action's own ref belongs to, so the action and
# codevitals never drift apart. Read the version from Cargo.toml at that ref
# instead of from the ref's name: that works for tags (@v0.2.0), commit SHAs
# (@<sha>, as hardened workflows pin) and branches alike.
version="$(sed -n 's/^version = "\(.*\)"$/\1/p' "$ACTION_PATH/Cargo.toml" | head -n1)"
if [[ -z "$version" ]]; then
echo "::error::cannot read codevitals' version from $ACTION_PATH/Cargo.toml"
exit 1
fi
case "$RUNNER_OS-$RUNNER_ARCH" in
Linux-X64) target=x86_64-unknown-linux-gnu ;;
Linux-ARM64) target=aarch64-unknown-linux-gnu ;;
macOS-X64) target=x86_64-apple-darwin ;;
macOS-ARM64) target=aarch64-apple-darwin ;;
*)
echo "::error::codevitals has no binary for $RUNNER_OS on $RUNNER_ARCH; use a Linux or macOS runner"
exit 1
;;
esac
asset="codevitals-$target.tar.gz"
url="https://github.com/mainmatter/codevitals/releases/download/v$version/$asset"
dir="$RUNNER_TEMP/codevitals-bin"
mkdir -p "$dir"
# A plain download, not the API: no token, no API rate limit, no gh needed
# on self-hosted runners. --retry covers timeouts and 5xx, not a missing release.
if ! curl -fsSL --retry 5 "$url" | tar -xz -C "$dir"; then
echo "::error::cannot download $url; is codevitals v$version released (and not still a draft)?"
exit 1
fi
# Not added to GITHUB_PATH: the next step calls it by path, and the caller's
# later steps keep their PATH.
"$dir/cv" --version
- name: Scan and compute metrics
shell: bash
env:
METRICS: ${{ inputs.metrics }}
run: |
cv="$RUNNER_TEMP/codevitals-bin/cv"
"$cv" scan --db "$RUNNER_TEMP/codevitals.duckdb"
# METRICS is unquoted on purpose: one argument per name, without globbing.
set -f
"$cv" metrics --db "$RUNNER_TEMP/codevitals.duckdb" "$RUNNER_TEMP/metrics.json" $METRICS
- name: Publish metrics.json
shell: bash
env:
BRANCH: ${{ inputs.branch }}
TOKEN: ${{ github.token }}
run: |
out="$RUNNER_TEMP/codevitals-publish"
mkdir -p "$out" && cp "$RUNNER_TEMP/metrics.json" "$out/"
cd "$out"
git init -q && git checkout -q -b "$BRANCH"
git add metrics.json
git -c user.name="github-actions[bot]" \
-c user.email="41898282+github-actions[bot]@users.noreply.github.com" \
commit -qm "codevitals metrics for $GITHUB_SHA"
git push -qf "https://x-access-token:${TOKEN}@${GITHUB_SERVER_URL#https://}/${GITHUB_REPOSITORY}.git" "HEAD:refs/heads/$BRANCH"