refactor: move scripts to scripts folder
This is a bit tidier.
This commit is contained in:
parent
95718c754d
commit
9364b5d352
7 changed files with 94 additions and 98 deletions
184
scripts/comment_on_pr.sh
Executable file
184
scripts/comment_on_pr.sh
Executable file
|
@ -0,0 +1,184 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
util_path="${GITHUB_ACTION_PATH:-.}/scripts/utils.sh"
|
||||
|
||||
# shellcheck source=scripts/utils.sh
|
||||
. "${util_path}"
|
||||
|
||||
# USAGE: json_to_md_rows <FIELD> [JSON_FILE]
|
||||
#
|
||||
# JSON_FILE can be piped from stdin
|
||||
json_to_md_rows() {
|
||||
jq --raw-output \
|
||||
".$1[]"' | "| `\(.name)` | \(.size) | \(.narSize) |"' "$2" |
|
||||
numfmt --suffix=B --to=iec-i --field=4,6
|
||||
}
|
||||
|
||||
# USAGE: json_to_md_rows <FIELD> [JSON_FILE]
|
||||
#
|
||||
# JSON_FILE can be piped from stdin
|
||||
json_to_md_rows_and_change() {
|
||||
jq --raw-output \
|
||||
".$1[]"' | "| `\(.name)` | \(.size) | \(.sizeChange) | \(.narSize) | \(.narSizeChange) |"' |
|
||||
numfmt --suffix=B --to=iec-i --field=4,6,8,10
|
||||
}
|
||||
|
||||
# USAGE: has_elements <FIELD> <JSON_FILE>
|
||||
has_elements() {
|
||||
if [ "${2+set}" = 'set' ]; then
|
||||
[ "$(jq ".$1 != []" "$2")" = 'true' ]
|
||||
else
|
||||
[ "$(jq ".$1 != []")" = 'true' ]
|
||||
fi
|
||||
}
|
||||
|
||||
# USAGE: markdown_from_report <REPORT> [BASE_REPORT]
|
||||
#
|
||||
# If BASE_REPORT is provided, a comparison will be made
|
||||
markdown_from_report() {
|
||||
cat <<-"EOF"
|
||||
<!-- AUTOGENERATED by nix-flake-outputs-size action -->
|
||||
|
||||
## Flake output sizes
|
||||
|
||||
<details><summary><b>Definitions</b></summary>
|
||||
|
||||
- `Name`: the name of the package/configuration.
|
||||
- `Size`: the closure size (size on disk/NAR size + all transitive dependencies).
|
||||
- `NAR Size`: the size of the build output (package without the dependencies).
|
||||
EOF
|
||||
if [ "${2+set}" = "set" ]; then
|
||||
cat <<-"EOF"
|
||||
- `[NAR] Size Change`: the amount changed compared to the main branch.
|
||||
|
||||
</details>
|
||||
EOF
|
||||
else
|
||||
cat <<-"EOF"
|
||||
</details>
|
||||
EOF
|
||||
fi
|
||||
cat <<-"EOF"
|
||||
|
||||
<details><summary><b>Tips on reading this data</b></summary>
|
||||
|
||||
- For NixOS/Home-Manager configurations you generally care only about the `Size` (closure size/size on disk).
|
||||
- Reduce the `Size` by disabling unneeded services/default packages.
|
||||
- For Packages you care about both the `Size` and the `NAR Size`.
|
||||
- Reduce the `NAR Size` by reducing the size of the build outputs, e.g. don't copy unnecessary data to the $out dir, optimize binaries for size, etc.
|
||||
- Reduce the `Size` by reducing the dependencies (e.g. `buildInputs`).
|
||||
- Don't worry too much about size, some dependencies are deduplicated, e.g. `glibc` adds ~40MiB to the `Size`, but is generally shared by ~every binary on the system, so, chances are, you are already including it from somewhere else and statically linking with e.g. `musl` is not gonna improve things.
|
||||
|
||||
</details>
|
||||
|
||||
EOF
|
||||
|
||||
if [ "${2+set}" = "set" ]; then
|
||||
compare=$(jq --slurp --from-file "${GITHUB_ACTION_PATH:-.}/compare.jq" "$1" "$2")
|
||||
if echo "$compare" | has_elements 'nixosConfigurations'; then
|
||||
cat <<-"EOF"
|
||||
## NixOS Configurations
|
||||
|
||||
| Name | Size | Size Change | NAR Size | NAR Size Change |
|
||||
|------|-----:|------------:|---------:|----------------:|
|
||||
EOF
|
||||
echo "$compare" | json_to_md_rows_and_change "nixosConfigurations"
|
||||
echo
|
||||
fi
|
||||
if echo "$compare" | has_elements 'homeConfigurations'; then
|
||||
cat <<-"EOF"
|
||||
## Home Manager Configurations
|
||||
|
||||
| Name | Size | Size Change | NAR Size | NAR Size Change |
|
||||
|------|-----:|------------:|---------:|----------------:|
|
||||
EOF
|
||||
echo "$compare" | json_to_md_rows_and_change "homeConfigurations"
|
||||
echo
|
||||
fi
|
||||
if echo "$compare" | has_elements 'packages'; then
|
||||
cat <<-"EOF"
|
||||
## Packages
|
||||
|
||||
| Name | Size | Size Change | NAR Size | NAR Size Change |
|
||||
|------|-----:|------------:|---------:|----------------:|
|
||||
EOF
|
||||
echo "$compare" | json_to_md_rows_and_change "packages"
|
||||
echo
|
||||
fi
|
||||
else
|
||||
if has_elements 'nixosConfigurations' "$1"; then
|
||||
cat <<-"EOF"
|
||||
## NixOS Configurations
|
||||
|
||||
| Name | Size | NAR Size |
|
||||
|------|-----:|---------:|
|
||||
EOF
|
||||
json_to_md_rows "nixosConfigurations" "$1"
|
||||
echo
|
||||
fi
|
||||
if has_elements 'homeConfigurations' "$1"; then
|
||||
cat <<-"EOF"
|
||||
## Home Manger Configurations
|
||||
|
||||
| Name | Size | NAR Size |
|
||||
|------|-----:|---------:|
|
||||
EOF
|
||||
json_to_md_rows "homeConfigurations" "$1"
|
||||
echo
|
||||
fi
|
||||
if has_elements 'packages' "$1"; then
|
||||
cat <<-"EOF"
|
||||
## Packages
|
||||
|
||||
| Name | Size | NAR Size |
|
||||
|------|-----:|---------:|
|
||||
EOF
|
||||
json_to_md_rows "packages" "$1"
|
||||
echo
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Test outside CI
|
||||
if [ "${CI:-false}" != 'true' ]; then
|
||||
markdown_from_report "$@"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Protect against running before a PR is made or if it is triggered on the main branch
|
||||
if [ -z "$PR_ID" ]; then
|
||||
warn "No PR created for this commit"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
log 'Generating comment body'
|
||||
comment=$(markdown_from_report "$@")
|
||||
|
||||
group 'Comment Data'
|
||||
log "$comment"
|
||||
endgroup
|
||||
|
||||
data=$(echo '{}' | jq --arg comment "$comment" '.body=$comment')
|
||||
group 'Request data'
|
||||
log "$data"
|
||||
endgroup
|
||||
|
||||
if [ -z "$COMMENT_ID" ]; then
|
||||
log 'Posting new comment'
|
||||
curl -o - -X 'POST' \
|
||||
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$PR_ID/comments" \
|
||||
-H 'Accept: application/json' \
|
||||
-H "Authorization: token $GITHUB_TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "$data"
|
||||
else
|
||||
log "Editing comment $COMMENT_ID"
|
||||
curl -o - -X 'PATCH' \
|
||||
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/comments/$COMMENT_ID" \
|
||||
-H 'Accept: application/json' \
|
||||
-H "Authorization: token $GITHUB_TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "$data"
|
||||
fi
|
81
scripts/create-report.sh
Executable file
81
scripts/create-report.sh
Executable file
|
@ -0,0 +1,81 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
util_path="${GITHUB_ACTION_PATH:-.}/scripts/utils.sh"
|
||||
|
||||
# shellcheck source=scripts/utils.sh
|
||||
. "${util_path}"
|
||||
|
||||
group 'Retrieving Flake information'
|
||||
flake_info=$(nix flake show --json --quiet --quiet)
|
||||
endgroup
|
||||
|
||||
system=$(nix eval --impure --json --expr 'builtins.currentSystem')
|
||||
|
||||
group 'Show Packages'
|
||||
packages=$(echo "$flake_info" | jq --raw-output --argjson system "$system" 'getpath(["packages", $system]) | select(. != null) | keys[]')
|
||||
[ -z "$packages" ] || log "$packages"
|
||||
endgroup
|
||||
|
||||
group 'Show Home Manager Configurations'
|
||||
hmConfigs=$(echo "$flake_info" | jq --raw-output '.homeConfigurations | select(. != null) | keys[]')
|
||||
[ -z "$hmConfigs" ] || log "$hmConfigs"
|
||||
endgroup
|
||||
|
||||
group 'Show NixOS Configurations'
|
||||
nixosConfigs=$(echo "$flake_info" | jq --raw-output '.nixosConfigurations | select(. != null) | keys[]')
|
||||
[ -z "$nixosConfigs" ] || log "$nixosConfigs"
|
||||
endgroup
|
||||
|
||||
closure_size() {
|
||||
name=$1
|
||||
path=$2
|
||||
log "Calculating size of $name at $path"
|
||||
path_info=$(nix path-info --closure-size --json "$path")
|
||||
echo "$path_info" |
|
||||
jq --compact-output \
|
||||
--arg pkg "$name" \
|
||||
--arg path "$path" \
|
||||
'.[] | {"name": $pkg, "path": $path, "size": .closureSize, "narSize": .narSize, "raw": .}'
|
||||
}
|
||||
|
||||
pkgs_json() {
|
||||
for package in $packages; do
|
||||
log "Building $package"
|
||||
path=$(nix build --print-out-paths ".#$package")
|
||||
closure_size "$package" "$path"
|
||||
done
|
||||
}
|
||||
|
||||
hm_configs_json() {
|
||||
for config in $hmConfigs; do
|
||||
log "Building $config"
|
||||
path=$(nix build --print-out-paths ".#homeConfigurations.$config.activationPackages")
|
||||
closure_size "$config" "$path"
|
||||
done
|
||||
}
|
||||
|
||||
nixos_configs_json() {
|
||||
for config in $nixosConfigs; do
|
||||
log "Building $config"
|
||||
path=$(nix build --print-out-paths ".#nixosConfigurations.$config.config.system.build.toplevel")
|
||||
closure_size "$config" "$path"
|
||||
done
|
||||
}
|
||||
|
||||
group 'Building packages'
|
||||
pkgs=$(pkgs_json | jq --slurp .)
|
||||
endgroup
|
||||
group 'Building Home Manager configurations'
|
||||
hmConfigs=$(hm_configs_json | jq --slurp .)
|
||||
endgroup
|
||||
group 'Building NixOS configurations'
|
||||
nixosConfigs=$(nixos_configs_json | jq --slurp .)
|
||||
endgroup
|
||||
|
||||
echo "{}" | jq \
|
||||
--argjson pkgs "$pkgs" \
|
||||
--argjson hmConfigs "$hmConfigs" \
|
||||
--argjson nixosConfigs "$nixosConfigs" \
|
||||
'{"packages": $pkgs, "nixosConfigurations": $nixosConfigs, "homeConfigurations": $hmConfigs}' >"${1:-/dev/stdout}"
|
79
scripts/retrieve-old-report.sh
Executable file
79
scripts/retrieve-old-report.sh
Executable file
|
@ -0,0 +1,79 @@
|
|||
#!/bin/sh
|
||||
|
||||
. "${GITHUB_ACTION_PATH}/scripts/utils.sh"
|
||||
|
||||
repo_info() {
|
||||
curl -X GET \
|
||||
-H "Authorization: token $GITHUB_TOKEN" \
|
||||
-H 'Accept: application/json' \
|
||||
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY"
|
||||
}
|
||||
|
||||
in_private_repo() {
|
||||
test "$(repo_info | jq --raw-output '.private')" = 'true'
|
||||
}
|
||||
|
||||
default_branch() {
|
||||
repo_info | jq --raw-output '.default_branch'
|
||||
}
|
||||
|
||||
# USAGE: base_report_url <BASE_BRANCH>
|
||||
base_report_url() {
|
||||
curl -X 'GET' \
|
||||
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/tasks" \
|
||||
-H "Authorization: token $GITHUB_TOKEN" \
|
||||
-H 'Accept: application/json' |
|
||||
jq --raw-output \
|
||||
--arg name "$JOB_NAME" \
|
||||
--arg head_branch "$1" \
|
||||
'[.workflow_runs[] | select(.name == $name and .head_branch == $head_branch)] | first | .url'
|
||||
}
|
||||
|
||||
# USAGE: has_report <REPORT_URL>
|
||||
has_report() {
|
||||
http_code=$(curl -X 'GET' -o /dev/null --silent -Iw '%{http_code}' \
|
||||
"$1" -H "Authorization: token $GITHUB_TOKEN")
|
||||
log "Got code $http_code for $1"
|
||||
test "$http_code" = '200'
|
||||
}
|
||||
|
||||
# If a base branch is not provided, use the default branch
|
||||
base_branch=${BASE_BRANCH:-$(default_branch)}
|
||||
|
||||
if [ "$(in_private_repo)" != 'true' ] && [ "$JOB_NAME" ]; then
|
||||
url=$(base_report_url "$base_branch")
|
||||
|
||||
log "Found previous run at: $url"
|
||||
report_url="$url/artifacts/$ARTIFACT_NAME"
|
||||
|
||||
if has_report "$report_url"; then
|
||||
log 'Found previous report, downloading...'
|
||||
curl -X 'GET' \
|
||||
"$report_url" \
|
||||
-H "Authorization: token $GITHUB_TOKEN" |
|
||||
gunzip >old-report.json
|
||||
log "Reporting on sizes and comparing to sizes in $base_branch"
|
||||
exit 0
|
||||
fi
|
||||
error "Failed to find previous report, expected at: $report_url"
|
||||
fi
|
||||
|
||||
warn "Couldn't retrieve old report:"
|
||||
warn ' This usuially happens when running on private repos'
|
||||
warn ' or when job-name is not set.'
|
||||
warn
|
||||
warn ' See the README for more details'
|
||||
|
||||
error "Falling back to slow method (checkout $base_branch and generate the report)"
|
||||
|
||||
old=$(mktemp -d)
|
||||
group "Download files from $base_branch"
|
||||
curl -X 'GET' \
|
||||
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/archive/$base_branch.tar.gz" \
|
||||
-H "Authorization: token $GITHUB_TOKEN" |
|
||||
tar -zvx --strip-components=1 -C "$old"
|
||||
endgroup
|
||||
|
||||
(cd "$old" && "$GITHUB_ACTION_PATH/create-report.sh" old-report.json)
|
||||
|
||||
exit 0
|
21
scripts/utils.sh
Executable file
21
scripts/utils.sh
Executable file
|
@ -0,0 +1,21 @@
|
|||
#!/bin/sh
|
||||
|
||||
log() {
|
||||
echo "$@" >&2
|
||||
}
|
||||
|
||||
warn() {
|
||||
log "\e[0;33m[ERROR]:" "$@" "\e[0m"
|
||||
}
|
||||
|
||||
error() {
|
||||
log "\e[0;31m[WARN]:" "$@" "\e[0m"
|
||||
}
|
||||
|
||||
group() {
|
||||
echo "::group::$1"
|
||||
}
|
||||
|
||||
endgroup() {
|
||||
echo '::endgroup::'
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue