Compare commits

..

1 commit

Author SHA1 Message Date
efe08fcf0d
feat: use download-artifact instead of manually downloading it
Some checks failed
/ check (treefmt) (push) Successful in 3s
/ report-size (push) Failing after 2s
/ report-download-check (push) Has been skipped
This should hopefully work on private repos too!
2025-05-14 22:55:28 +02:00
13 changed files with 194 additions and 327 deletions

View file

@ -24,22 +24,27 @@ jobs:
uses: ./
with:
# Create a comment on the associated PR
comment-on-pr: 'false'
comment-on-pr: ${{ github.ref_name != 'main' }}
# Generate artifacts on main (to speed up comparisons)
# generate-artifact: ${{ github.ref_name == 'main' }}
# Always generate artifacts for testing purposes
generate-artifact: 'true'
# This job's name (so we can find the previous artifacts)
# Generate comparisons to main
do-comparison: 'true'
# This job's name (so we can find the artifacts)
job-name: report-size
# A token is required to download a previous run's artifact
github-token: ${{ secrets.FULL_PERM_TEST }}
report-download-check:
runs-on: nixos
needs: report-size-push
if: github.event_name == 'push'
needs: report-size
steps:
- name: Download previous report
uses: https://git.salame.cl/actions/download-artifact@d8d0a99033603453ad2255e58720b460a0555e1e # v4
with:
name: report.json
github-token: ${{ secrets.ARTIFACT_TOKEN }}
run-id: 15
- name: Verify report exists
run: |
cat report.json

View file

@ -1,33 +0,0 @@
on:
pull_request:
jobs:
report-size-pr:
runs-on: nixos
steps:
- uses: "https://git.salame.cl/actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683" # v4
- run: nix --version
- name: Create Size Report
uses: ./
with:
# Create a comment on the associated PR
comment-on-pr: 'true'
# Generate artifacts on main (to speed up comparisons)
# generate-artifact: ${{ github.ref_name == 'main' }}
# Always generate artifacts for testing purposes
generate-artifact: 'true'
# Generate comparisons to main
do-comparison: 'true'
# Get the previous artifacts from report-size-push (since those run on main)
job-name: report-size
artifact-name: report.json
report-download-check-pr:
runs-on: nixos
needs: report-size-pr
steps:
- name: Download previous report
uses: https://git.salame.cl/actions/download-artifact@d8d0a99033603453ad2255e58720b460a0555e1e # v4
with:
name: report.json
- name: Verify report exists
run: |
cat report.json

1
.gitignore vendored
View file

@ -1 +0,0 @@
result*

View file

@ -56,9 +56,9 @@ For more details see the [action.yaml](./action.yml) file.
| Name | Size | Size Change | NAR Size | NAR Size Change |
|------|-----:|------------:|---------:|----------------:|
| `gemini` | 11Gi | -2.4Mi | 28Ki | 0 |
| `leo` | 1.6Gi | 0 | 25Ki | 0 |
| `libra` | 9.4Gi | -2.4Mi | 28Ki | 0 |
| `taurus` | 7.6Gi | 0 | 34Ki | 0 |
| `gemini` | 11Gi | -2.4Mi | 28Ki | 0 |
| `leo` | 1.6Gi | 0 | 25Ki | 0 |
| `libra` | 9.4Gi | -2.4Mi | 28Ki | 0 |
| `taurus` | 7.6Gi | 0 | 34Ki | 0 |
</details>

View file

@ -39,6 +39,8 @@ inputs:
artifact-name:
description: The name of the generated artifact.
default: report.json
github-token:
description: A github token with read permissions for this repository, required if do-comparison is 'true'
# Comparison Report (comment only)
do-comparison:
description: |
@ -67,18 +69,42 @@ outputs:
runs:
using: 'composite'
steps:
- name: Run
- name: Find PR (if it exists)
id: pr-number
env:
BASE_BRANCH: ${{ inputs.base-branch }}
COMMENT: ${{ inputs.comment-on-pr }}
DO_COMPARISON: ${{ inputs.do-comparison }}
GENERATE_ARTIFACT: ${{ inputs.generate-artifact }}
JOB_NAME: ${{ inputs.job-name }}
if: inputs.comment-on-pr == 'true'
run: |
"$GITHUB_ACTION_PATH/scripts/run.sh"
. "$GITHUB_ACTION_PATH/utils.sh"
log 'Determine head_ref'
# For push & tag events it'll bet GITHUB_REF_NAME, for pull_request events it'll be GITHUB_HEAD_REF
head_ref=${GITHUB_REF_NAME:-$GITHUB_HEAD_REF}
log "Get PR number for $head_ref"
prs=$(curl -X 'GET' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls?state=open&sort=recentupdate" \
-H "Authorization: token $GITHUB_TOKEN" \
-H 'Accept: application/json')
pr_number=$(echo "$prs" |
jq --arg head_ref "$head_ref" '.[] | select(.head.ref == $head_ref) | .number')
# This seems to create the file???
log "GITHUB_OUTPUT=$GITHUB_OUTPUT"
log "$(ls -l "$GITHUB_OUTPUT")"
# Protect against running before a PR is made or if it is triggered on the main branch
if [ -z "$pr_number" ]; then
warn "No PR created for this commit"
echo "pr-number=" >> "$GIHUB_OUTPUT"
exit 0
fi
log "Retrieved index: $pr_number"
log "Expected PR URL: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pulls/$pr_number"
echo "pr-number=$pr_number" >> "$GITHUB_OUTPUT"
- name: Find previous comment (if present)
# We want to generate a comment and we we able to find the PR number
# We want to generate a comment, and we we able to fin the PR number
if: inputs.comment-on-pr == 'true' && steps.pr-number.outputs.pr-number != ''
id: find-comment
uses: https://github.com/peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3
@ -86,25 +112,80 @@ runs:
issue-number: ${{ steps.pr-number.outputs.pr-number }}
direction: first
body-includes: "<!-- AUTOGENERATED by nix-flake-outputs-size action -->"
- name: Create report and comment on PR
# We want to generate a comment and we we able to find the PR number
if: inputs.comment-on-pr == 'true' && steps.pr-number.outputs.pr-number != ''
- name: Find previous run on base-branch
# Only run when doing comparisons
if: inputs.do-comparison == 'true'
id: previous-report
continue-on-error: true
env:
ARTIFACT_NAME: ${{ inputs.artifact-name }}
BASE_BRANCH: ${{ inputs.base-branch }}
COMMENT_ID: ${{ steps.find-comment.outputs.comment-id }}
DO_COMPARISON: ${{ inputs.do-comparison }}
JOB_NAME: ${{ inputs.job-name }}
PR_ID: ${{ steps.pr-number.outputs.pr-number }}
ARTIFACT_NAME: ${{ inputs.artifact-name }}
run: |
. "$GITHUB_ACTION_PATH/scripts/utils.sh"
. "${GITHUB_ACTION_PATH}/utils.sh"
# Determine the default branch of this repo
default_branch() {
curl -X GET \
-H "Authorization: token $GITHUB_TOKEN" \
-H 'Accept: application/json' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY" |
jq --raw-output '.default_branch'
}
run_id=$(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 "${BASE_BRANCH:-$(default_branch)}" \
'[.workflow_runs[] | select(.name == $name and .head_branch == $head_branch)] | first | .run_number')
echo "run-id=$run_id" >> "$GITHUB_OUTPUT"
log Would try to download "$ARTIFACT_NAME" from "$run_id"
- name: Download previous report
uses: https://git.salame.cl/actions/download-artifact@d8d0a99033603453ad2255e58720b460a0555e1e # v4
# Ensure the previous step succeeded
if: steps.previous-report.outcome == 'success'
with:
name: ${{ inputs.artifact-name }}
# github-token: ${{ inputs.github-token }}
# run-id: ${{ steps.previous-report.outputs.run-id }}
- name: Create report
if: inputs.comment-on-pr == 'true' || inputs.generate-artifact == 'true'
env:
PR_ID: ${{ steps.pr-number.outputs.pr-number }}
COMMENT: ${{ inputs.comment-on-pr }}
COMMENT_ID: ${{ steps.find-comment.outputs.comment-id }}
ARTIFACT_NAME: ${{ inputs.artifact-name }}
DO_COMPARISON: ${{ inputs.do-comparison }}
BASE_BRANCH: ${{ inputs.base-branch }}
JOB_NAME: ${{ inputs.job-name }}
run: |
. "$GITHUB_ACTION_PATH/utils.sh"
# Input validation
if [ "$DO_COMPARISON" = 'true' ] && [ -z "$JOB_NAME" ]; then
error 'job-name should be set if you want to generate a comparison report'
exit 1
fi
# move old report so it doesn't clash with the new report
[ -f report.json ] && mv report.json old-report.json
# Create Size Report
"$GITHUB_ACTION_PATH/create-report.sh" > report.json
# Nothing else to do
if [ "$COMMENT" != 'true' ]; then exit 0; fi
# Try to do a comparison report
if [ "$DO_COMPARISON" = 'true' ]; then
if "$GITHUB_ACTION_PATH/scripts/retrieve-old-report.sh" && [ -f old-report.json ]; then
log "Reporting on sizes and comparing to sizes in $BASE_BRANCH"
if [ -f old-report.json ]; then
log "Reporting on sizes and comparing to sizes in $HEAD_BRANCH"
"$GITHUB_ACTION_PATH/scripts/comment_on_pr.sh" report.json old-report.json
"$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json old-report.json
exit 0
else
error 'Failed to do comparison, fallback to posting the report without them'
@ -113,7 +194,7 @@ runs:
# Just report values
log 'Reporting on sizes'
"$GITHUB_ACTION_PATH/scripts/comment_on_pr.sh" report.json
"$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json
- name: Upload Artifact
uses: https://git.salame.cl/actions/upload-artifact@v4
if: inputs.generate-artifact == 'true'

View file

@ -2,9 +2,9 @@
set -eu
util_path="${GITHUB_ACTION_PATH:-.}/scripts/utils.sh"
util_path="${GITHUB_ACTION_PATH:-.}/utils.sh"
# shellcheck source=scripts/utils.sh
# shellcheck source=utils.sh
. "${util_path}"
# USAGE: json_to_md_rows <FIELD> [JSON_FILE]
@ -64,7 +64,7 @@ markdown_from_report() {
<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).
- For NixOS 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.
@ -87,16 +87,6 @@ markdown_from_report() {
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
@ -118,16 +108,6 @@ markdown_from_report() {
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

63
create-report.sh Executable file
View file

@ -0,0 +1,63 @@
#!/usr/bin/env bash
set -eu
util_path="${GITHUB_ACTION_PATH:-.}/utils.sh"
# shellcheck source=utils.sh
. "${util_path}"
group 'Retrieving Flake information'
flake_info=$(nix flake show --json)
endgroup
group 'Show Packages'
packages=$(
jq --raw-output '.packages."x86_64-linux" | select(. != null) | keys[]' <<-EOF
$flake_info
EOF
)
log "$packages"
endgroup
group 'Show NixOS Configurations'
configurations=$(
jq --raw-output '.nixosConfigurations | select(. != null) | keys[]' <<-EOF
$flake_info
EOF
)
log "$configurations"
endgroup
pkgs_json() {
group 'Building packages'
trap endgroup RETURN
for package in $packages; do
log "Building $package"
path=$(nix build --print-out-paths ".#$package")
log "Calculating size of $package"
nix path-info --closure-size --json "$path" |
jq --compact-output --arg pkg "$package" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}'
done
endgroup
}
configs_json() {
group 'Building NixOS configurations'
trap endgroup RETURN
for config in $configurations; do
log "Building $config"
path=$(nix build --print-out-paths ".#nixosConfigurations.$config.config.system.build.toplevel")
log "Calculating size of $config"
nix path-info --closure-size --json "$path" |
jq --compact-output --arg pkg "$config" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}'
done
}
pkgs=$(pkgs_json | jq --slurp '.')
configs=$(configs_json | jq --slurp '.')
echo "{}" | jq \
--argjson pkgs "$pkgs" \
--argjson configs "$configs" \
'{"packages": $pkgs, "nixosConfigurations": $configs}'

17
flake.lock generated
View file

@ -20,15 +20,18 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1752076035,
"narHash": "sha256-ROMKbpfgrbogAWXNXJxUu+BoPyZdOq/MQ3UNYVP863o=",
"rev": "9807714d6944a957c2e036f84b0ff8caf9930bc0",
"type": "tarball",
"url": "https://releases.nixos.org/nixos/unstable/nixos-25.11pre826938.9807714d6944/nixexprs.tar.xz?rev=9807714d6944a957c2e036f84b0ff8caf9930bc0"
"lastModified": 1741513245,
"narHash": "sha256-7rTAMNTY1xoBwz0h7ZMtEcd8LELk9R5TzBPoHuhNSCk=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "e3e32b642a31e6714ec1b712de8c91a3352ce7e1",
"type": "github"
},
"original": {
"type": "tarball",
"url": "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz"
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-lib": {

View file

@ -3,7 +3,7 @@
inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";

View file

@ -1,82 +0,0 @@
#!/bin/sh
set -eu
util_path="${GITHUB_ACTION_PATH:-.}/scripts/utils.sh"
# shellcheck source=scripts/utils.sh
. "${util_path}"
system=$(nix eval --impure --json --expr 'builtins.currentSystem')
# Extract the names of a flake attrset
get_names() {
nix eval --json --apply builtins.attrNames "$1" 2>/dev/null | jq --raw-output '.[]'
}
group "Show Packages for $system"
packages=$(get_names .#packages."$system")
[ -z "$packages" ] || log "$packages"
endgroup
group 'Show Home Manager Configurations'
hmConfigs=$(get_names .#homeConfigurations)
[ -z "$hmConfigs" ] || log "$hmConfigs"
endgroup
group 'Show NixOS Configurations'
nixosConfigs=$(get_names .#nixosConfigurations)
[ -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.config.home.activationPackage")
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}"

View file

@ -1,83 +0,0 @@
#!/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; then
warn 'Detected that this is a private repo cannot retrieve old report'
elif [ "$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"
else
panic 'job-name is missing, therefore we cannot find the previous report'
fi
warn "Couldn't retrieve old report:
note: This usually happens when running on private repos or when job-name is not set.
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
repo_dir=$PWD
(cd "$old" && "$GITHUB_ACTION_PATH/scripts/create-report.sh" "$repo_dir"/old-report.json)
exit 0

View file

@ -1,61 +0,0 @@
#!/bin/sh
. "$GITHUB_ACTION_PATH/scripts/utils.sh"
# Input validation
if [ "$COMMENT" != "true" ] && [ "$GENERATE_ARTIFACT" != "true" ]; then
panic 'Neither comment-on-pr nor generate-artifact is set
note: this looks like an error; if it isn'"'"'t disable this action with "step.if"'
fi
if [ "$DO_COMPARISON" = 'true' ] && [ -z "$JOB_NAME" ]; then
panic 'Requested a comparison report but job-name wasn'"'"'t set'
fi
# Create Size Report (will be uploaded by the upload-artifact action)
"$GITHUB_ACTION_PATH/scripts/create-report.sh" report.json
# Nothing else to do
if [ "$COMMENT" != "true" ]; then exit 0; fi
# Find the PR for this commit so we can post a comment on it
pr_number=
case "$GITHUB_EVENT_NAME" in
"pull_request")
pr_number=$(jq .number "$GITHUB_EVENT_PATH")
log "Triggered by a pull request with index: $pr_number"
;;
"push")
log "Triggered by a push to $GITHUB_REF_NAME autodetecting PR number"
log "Get PR number for $GITHUB_REF_NAME"
prs=$(curl -X 'GET' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls?state=open&sort=recentupdate" \
-H "Authorization: token $GITHUB_TOKEN" \
-H 'Accept: application/json')
log "Found these open PRs: $(echo "$prs" | jq '[.[] | .number]')"
pr_number=$(echo "$prs" |
jq --arg ref "$GITHUB_REF_NAME" '.[] | select(.head.ref == $ref) | .number')
# Protect against running before a PR is made or if it is triggered on the main branch
if [ -z "$pr_number" ]; then
warn "No PR created for this commit"
exit 0
fi
log "The PR we found for $GITHUB_REF_NAME is $pr_number"
;;
*)
panic "Unexpected event $GITHUB_EVENT_NAME for commenting on a PR, expected push or pull_request"
;;
esac
log "Expected PR URL: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pulls/$pr_number"
# This seems to create the file???
log "GITHUB_OUTPUT=$GITHUB_OUTPUT"
log "$(ls -l "$GITHUB_OUTPUT")"
echo "pr-number=$pr_number" >>"$GITHUB_OUTPUT"

View file

@ -12,15 +12,10 @@ error() {
log "\e[0;31m[WARN]:" "$@" "\e[0m"
}
panic() {
error "$@"
exit 1
}
group() {
echo "::group::$1"
log "::group::$1"
}
endgroup() {
echo '::endgroup::'
log '::endgroup::'
}