From efe08fcf0df5a126511fcf15b41cd64c0ac6d324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Tue, 13 May 2025 20:01:37 +0200 Subject: [PATCH 01/10] feat: use download-artifact instead of manually downloading it This should hopefully work on private repos too! --- .forgejo/workflows/check.yml | 4 ++ action.yml | 47 ++++++++++++++++++++- retrieve-old-report.sh | 79 ------------------------------------ 3 files changed, 50 insertions(+), 80 deletions(-) delete mode 100755 retrieve-old-report.sh diff --git a/.forgejo/workflows/check.yml b/.forgejo/workflows/check.yml index 14a13b1..66e66a8 100644 --- a/.forgejo/workflows/check.yml +++ b/.forgejo/workflows/check.yml @@ -33,6 +33,8 @@ jobs: 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 @@ -41,6 +43,8 @@ jobs: 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 diff --git a/action.yml b/action.yml index 46431b7..5bb60d9 100644 --- a/action.yml +++ b/action.yml @@ -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: | @@ -110,6 +112,46 @@ runs: issue-number: ${{ steps.pr-number.outputs.pr-number }} direction: first body-includes: "" + - 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: + BASE_BRANCH: ${{ inputs.base-branch }} + JOB_NAME: ${{ inputs.job-name }} + ARTIFACT_NAME: ${{ inputs.artifact-name }} + run: | + . "${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: @@ -129,6 +171,9 @@ runs: 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 @@ -137,7 +182,7 @@ runs: # Try to do a comparison report if [ "$DO_COMPARISON" = 'true' ]; then - if "$GITHUB_ACTION_PATH/retrieve-old-report.sh" && [ -f old-report.json ]; then + if [ -f old-report.json ]; then log "Reporting on sizes and comparing to sizes in $HEAD_BRANCH" "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json old-report.json diff --git a/retrieve-old-report.sh b/retrieve-old-report.sh deleted file mode 100755 index 14a48db..0000000 --- a/retrieve-old-report.sh +++ /dev/null @@ -1,79 +0,0 @@ -#!/bin/sh - -. "${GITHUB_ACTION_PATH}/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_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 -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 From 838f2050208b41c339803a1111608d7182bbda3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Mon, 2 Jun 2025 18:30:23 +0200 Subject: [PATCH 02/10] feat: add support for homeConfigurations This enables support for `homeConfigurations` (the Home Manager standalone configurations). --- comment_on_pr.sh | 22 +++++++++++++++++++++- create-report.sh | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/comment_on_pr.sh b/comment_on_pr.sh index 3a6d7a0..9122a22 100755 --- a/comment_on_pr.sh +++ b/comment_on_pr.sh @@ -64,7 +64,7 @@ markdown_from_report() {
Tips on reading this data -- For NixOS configurations you generally care only about the `Size` (closure size/size on disk). +- 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. @@ -87,6 +87,16 @@ 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 @@ -108,6 +118,16 @@ 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 diff --git a/create-report.sh b/create-report.sh index bd478aa..a463eb7 100755 --- a/create-report.sh +++ b/create-report.sh @@ -20,13 +20,22 @@ packages=$( log "$packages" endgroup +group 'Show Home Manager Configurations' +hmConfigs=$( + jq --raw-output '.homeConfigurations | select(. != null) | keys[]' <<-EOF + $flake_info + EOF +) +log "$hmConfigs" +endgroup + group 'Show NixOS Configurations' -configurations=$( +nixosConfigs=$( jq --raw-output '.nixosConfigurations | select(. != null) | keys[]' <<-EOF $flake_info EOF ) -log "$configurations" +log "$nixosConfigs" endgroup pkgs_json() { @@ -42,10 +51,22 @@ pkgs_json() { endgroup } -configs_json() { +hm_configs_json() { + group 'Building Home Manager configurations' + trap endgroup RETURN + for config in $hmConfigs; do + log "Building $config" + path=$(nix build --print-out-paths ".#homeConfigurations.$config.activationPackages") + 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 +} + +nixos_configs_json() { group 'Building NixOS configurations' trap endgroup RETURN - for config in $configurations; do + for config in $nixosConfigs; do log "Building $config" path=$(nix build --print-out-paths ".#nixosConfigurations.$config.config.system.build.toplevel") log "Calculating size of $config" @@ -55,9 +76,11 @@ configs_json() { } pkgs=$(pkgs_json | jq --slurp '.') -configs=$(configs_json | jq --slurp '.') +hmConfigs=$(hm_configs_json | jq --slurp '.') +nixosConfigs=$(nixos_configs_json | jq --slurp '.') echo "{}" | jq \ --argjson pkgs "$pkgs" \ - --argjson configs "$configs" \ - '{"packages": $pkgs, "nixosConfigurations": $configs}' + --argjson nixosConfigs "$nixosConfigs" \ + --argjson hmConfigs "$hmConfigs" \ + '{"packages": $pkgs, "nixosConfigurations": $nixosConfigs, "homeConfigurations": $hmConfigs}' From 95718c754dc32db2fe7d00a8218dc41199f58e8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Thu, 10 Jul 2025 20:29:55 +0200 Subject: [PATCH 03/10] feat(nix): use tarball instead of github ref This should make the downloads faster. --- README.md | 8 ++++---- flake.lock | 17 +++++++---------- flake.nix | 2 +- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index d3bceb9..6e9e21c 100644 --- a/README.md +++ b/README.md @@ -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 |
diff --git a/flake.lock b/flake.lock index 7e05325..a6376f4 100644 --- a/flake.lock +++ b/flake.lock @@ -20,18 +20,15 @@ }, "nixpkgs": { "locked": { - "lastModified": 1741513245, - "narHash": "sha256-7rTAMNTY1xoBwz0h7ZMtEcd8LELk9R5TzBPoHuhNSCk=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "e3e32b642a31e6714ec1b712de8c91a3352ce7e1", - "type": "github" + "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" }, "original": { - "owner": "NixOS", - "ref": "nixos-unstable", - "repo": "nixpkgs", - "type": "github" + "type": "tarball", + "url": "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz" } }, "nixpkgs-lib": { diff --git a/flake.nix b/flake.nix index 46e3e7b..35db44f 100644 --- a/flake.nix +++ b/flake.nix @@ -3,7 +3,7 @@ inputs = { flake-parts.url = "github:hercules-ci/flake-parts"; - nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + nixpkgs.url = "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz"; treefmt-nix = { url = "github:numtide/treefmt-nix"; inputs.nixpkgs.follows = "nixpkgs"; From 9364b5d35273788b61c056b2286ac43386abb3fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Thu, 10 Jul 2025 20:26:19 +0200 Subject: [PATCH 04/10] refactor: move scripts to scripts folder This is a bit tidier. --- .gitignore | 1 + action.yml | 12 +-- create-report.sh | 86 ------------------- comment_on_pr.sh => scripts/comment_on_pr.sh | 4 +- scripts/create-report.sh | 81 +++++++++++++++++ .../retrieve-old-report.sh | 4 +- utils.sh => scripts/utils.sh | 4 +- 7 files changed, 94 insertions(+), 98 deletions(-) create mode 100644 .gitignore delete mode 100755 create-report.sh rename comment_on_pr.sh => scripts/comment_on_pr.sh (98%) create mode 100755 scripts/create-report.sh rename retrieve-old-report.sh => scripts/retrieve-old-report.sh (95%) rename utils.sh => scripts/utils.sh (79%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fcfc4a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +result* diff --git a/action.yml b/action.yml index 46431b7..8efa9a5 100644 --- a/action.yml +++ b/action.yml @@ -71,7 +71,7 @@ runs: id: pr-number if: inputs.comment-on-pr == 'true' run: | - . "$GITHUB_ACTION_PATH/utils.sh" + . "$GITHUB_ACTION_PATH/scripts/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 @@ -121,7 +121,7 @@ runs: BASE_BRANCH: ${{ inputs.base-branch }} JOB_NAME: ${{ inputs.job-name }} run: | - . "$GITHUB_ACTION_PATH/utils.sh" + . "$GITHUB_ACTION_PATH/scripts/utils.sh" # Input validation if [ "$DO_COMPARISON" = 'true' ] && [ -z "$JOB_NAME" ]; then @@ -130,17 +130,17 @@ runs: fi # Create Size Report - "$GITHUB_ACTION_PATH/create-report.sh" > report.json + "$GITHUB_ACTION_PATH/scripts/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/retrieve-old-report.sh" && [ -f old-report.json ]; then + if "$GITHUB_ACTION_PATH/scripts/retrieve-old-report.sh" && [ -f old-report.json ]; then log "Reporting on sizes and comparing to sizes in $HEAD_BRANCH" - "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json old-report.json + "$GITHUB_ACTION_PATH/scripts/comment_on_pr.sh" report.json old-report.json exit 0 else error 'Failed to do comparison, fallback to posting the report without them' @@ -149,7 +149,7 @@ runs: # Just report values log 'Reporting on sizes' - "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json + "$GITHUB_ACTION_PATH/scripts/comment_on_pr.sh" report.json - name: Upload Artifact uses: https://git.salame.cl/actions/upload-artifact@v4 if: inputs.generate-artifact == 'true' diff --git a/create-report.sh b/create-report.sh deleted file mode 100755 index a463eb7..0000000 --- a/create-report.sh +++ /dev/null @@ -1,86 +0,0 @@ -#!/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 Home Manager Configurations' -hmConfigs=$( - jq --raw-output '.homeConfigurations | select(. != null) | keys[]' <<-EOF - $flake_info - EOF -) -log "$hmConfigs" -endgroup - -group 'Show NixOS Configurations' -nixosConfigs=$( - jq --raw-output '.nixosConfigurations | select(. != null) | keys[]' <<-EOF - $flake_info - EOF -) -log "$nixosConfigs" -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 -} - -hm_configs_json() { - group 'Building Home Manager configurations' - trap endgroup RETURN - for config in $hmConfigs; do - log "Building $config" - path=$(nix build --print-out-paths ".#homeConfigurations.$config.activationPackages") - 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 -} - -nixos_configs_json() { - group 'Building NixOS configurations' - trap endgroup RETURN - for config in $nixosConfigs; 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 '.') -hmConfigs=$(hm_configs_json | jq --slurp '.') -nixosConfigs=$(nixos_configs_json | jq --slurp '.') - -echo "{}" | jq \ - --argjson pkgs "$pkgs" \ - --argjson nixosConfigs "$nixosConfigs" \ - --argjson hmConfigs "$hmConfigs" \ - '{"packages": $pkgs, "nixosConfigurations": $nixosConfigs, "homeConfigurations": $hmConfigs}' diff --git a/comment_on_pr.sh b/scripts/comment_on_pr.sh similarity index 98% rename from comment_on_pr.sh rename to scripts/comment_on_pr.sh index 9122a22..b927400 100755 --- a/comment_on_pr.sh +++ b/scripts/comment_on_pr.sh @@ -2,9 +2,9 @@ set -eu -util_path="${GITHUB_ACTION_PATH:-.}/utils.sh" +util_path="${GITHUB_ACTION_PATH:-.}/scripts/utils.sh" -# shellcheck source=utils.sh +# shellcheck source=scripts/utils.sh . "${util_path}" # USAGE: json_to_md_rows [JSON_FILE] diff --git a/scripts/create-report.sh b/scripts/create-report.sh new file mode 100755 index 0000000..cfe4209 --- /dev/null +++ b/scripts/create-report.sh @@ -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}" diff --git a/retrieve-old-report.sh b/scripts/retrieve-old-report.sh similarity index 95% rename from retrieve-old-report.sh rename to scripts/retrieve-old-report.sh index 14a48db..9c50afe 100755 --- a/retrieve-old-report.sh +++ b/scripts/retrieve-old-report.sh @@ -1,6 +1,6 @@ #!/bin/sh -. "${GITHUB_ACTION_PATH}/utils.sh" +. "${GITHUB_ACTION_PATH}/scripts/utils.sh" repo_info() { curl -X GET \ @@ -74,6 +74,6 @@ curl -X 'GET' \ tar -zvx --strip-components=1 -C "$old" endgroup -(cd "$old" && "$GITHUB_ACTION_PATH/create-report.sh") >old-report.json +(cd "$old" && "$GITHUB_ACTION_PATH/create-report.sh" old-report.json) exit 0 diff --git a/utils.sh b/scripts/utils.sh similarity index 79% rename from utils.sh rename to scripts/utils.sh index e61c2a3..74c31a3 100755 --- a/utils.sh +++ b/scripts/utils.sh @@ -13,9 +13,9 @@ error() { } group() { - log "::group::$1" + echo "::group::$1" } endgroup() { - log '::endgroup::' + echo '::endgroup::' } From a26909cb8c574260745cb42c06c5eda32bca4395 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Thu, 10 Jul 2025 21:50:24 +0200 Subject: [PATCH 05/10] fix(create-report): failing on private repos Forgot to update the GITHUB_ACTION_PATH --- scripts/retrieve-old-report.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/retrieve-old-report.sh b/scripts/retrieve-old-report.sh index 9c50afe..ac20654 100755 --- a/scripts/retrieve-old-report.sh +++ b/scripts/retrieve-old-report.sh @@ -74,6 +74,6 @@ curl -X 'GET' \ tar -zvx --strip-components=1 -C "$old" endgroup -(cd "$old" && "$GITHUB_ACTION_PATH/create-report.sh" old-report.json) +(cd "$old" && "$GITHUB_ACTION_PATH/scripts/create-report.sh" old-report.json) exit 0 From 1b14f63169bc6721bb65ba6a43cc5e5379f46b66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Fri, 11 Jul 2025 17:47:27 +0200 Subject: [PATCH 06/10] fix(comparison): create the old report in the PWD We were creating it in a temp dir instead. --- scripts/retrieve-old-report.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/retrieve-old-report.sh b/scripts/retrieve-old-report.sh index ac20654..ee83f08 100755 --- a/scripts/retrieve-old-report.sh +++ b/scripts/retrieve-old-report.sh @@ -74,6 +74,7 @@ curl -X 'GET' \ tar -zvx --strip-components=1 -C "$old" endgroup -(cd "$old" && "$GITHUB_ACTION_PATH/scripts/create-report.sh" old-report.json) +repo_dir=$PWD +(cd "$old" && "$GITHUB_ACTION_PATH/scripts/create-report.sh" "$repo_dir"/old-report.json) exit 0 From ca72db2a3b1cb4f2957d1e8981112be1db11645e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Fri, 11 Jul 2025 18:23:06 +0200 Subject: [PATCH 07/10] refactor: try to condense the action in fewer steps This should make it easier to debug/maintain. --- action.yml | 64 ++++++++++-------------------------------------- scripts/run.sh | 61 +++++++++++++++++++++++++++++++++++++++++++++ scripts/utils.sh | 5 ++++ 3 files changed, 79 insertions(+), 51 deletions(-) create mode 100755 scripts/run.sh diff --git a/action.yml b/action.yml index 8efa9a5..f27253b 100644 --- a/action.yml +++ b/action.yml @@ -67,42 +67,18 @@ outputs: runs: using: 'composite' steps: - - name: Find PR (if it exists) + - name: Run id: pr-number - if: inputs.comment-on-pr == 'true' + 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 }} run: | - . "$GITHUB_ACTION_PATH/scripts/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" + "$GITHUB_ACTION_PATH/scripts/run.sh" - name: Find previous comment (if present) - # We want to generate a comment, and we we able to fin the PR number + # 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 != '' id: find-comment uses: https://github.com/peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3 @@ -110,35 +86,21 @@ runs: issue-number: ${{ steps.pr-number.outputs.pr-number }} direction: first body-includes: "" - - name: Create report - if: inputs.comment-on-pr == 'true' || inputs.generate-artifact == 'true' + - 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 != '' 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/scripts/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 - - # Create Size Report - "$GITHUB_ACTION_PATH/scripts/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 $HEAD_BRANCH" + log "Reporting on sizes and comparing to sizes in $BASE_BRANCH" "$GITHUB_ACTION_PATH/scripts/comment_on_pr.sh" report.json old-report.json exit 0 diff --git a/scripts/run.sh b/scripts/run.sh new file mode 100755 index 0000000..33e3068 --- /dev/null +++ b/scripts/run.sh @@ -0,0 +1,61 @@ +#!/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" diff --git a/scripts/utils.sh b/scripts/utils.sh index 74c31a3..a46bc1f 100755 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -12,6 +12,11 @@ error() { log "\e[0;31m[WARN]:" "$@" "\e[0m" } +panic() { + error "$@" + exit 1 +} + group() { echo "::group::$1" } From 7d7a576f501aac897f0c3d8ea74791a4c7001cf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Thu, 10 Jul 2025 19:39:36 +0200 Subject: [PATCH 08/10] ci: test both push and pull_request events I think we have a bug with pull_request events so... --- .forgejo/workflows/check.yml | 9 ++++----- .forgejo/workflows/test-pr.yml | 33 +++++++++++++++++++++++++++++++++ action.yml | 6 ++++-- scripts/retrieve-old-report.sh | 15 +++++++++------ 4 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 .forgejo/workflows/test-pr.yml diff --git a/.forgejo/workflows/check.yml b/.forgejo/workflows/check.yml index 14a13b1..1c90cbe 100644 --- a/.forgejo/workflows/check.yml +++ b/.forgejo/workflows/check.yml @@ -24,18 +24,17 @@ jobs: uses: ./ with: # Create a comment on the associated PR - comment-on-pr: ${{ github.ref_name != 'main' }} + comment-on-pr: 'false' # 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' - # This job's name (so we can find the artifacts) + # This job's name (so we can find the previous artifacts) job-name: report-size report-download-check: runs-on: nixos - needs: report-size + needs: report-size-push + if: github.event_name == 'push' steps: - name: Download previous report uses: https://git.salame.cl/actions/download-artifact@d8d0a99033603453ad2255e58720b460a0555e1e # v4 diff --git a/.forgejo/workflows/test-pr.yml b/.forgejo/workflows/test-pr.yml new file mode 100644 index 0000000..43fb880 --- /dev/null +++ b/.forgejo/workflows/test-pr.yml @@ -0,0 +1,33 @@ +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 diff --git a/action.yml b/action.yml index f27253b..db929d2 100644 --- a/action.yml +++ b/action.yml @@ -90,10 +90,12 @@ runs: # 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 != '' env: - PR_ID: ${{ steps.pr-number.outputs.pr-number }} + ARTIFACT_NAME: ${{ inputs.artifact-name }} + BASE_BRANCH: ${{ inputs.base-branch }} COMMENT_ID: ${{ steps.find-comment.outputs.comment-id }} DO_COMPARISON: ${{ inputs.do-comparison }} - BASE_BRANCH: ${{ inputs.base-branch }} + JOB_NAME: ${{ inputs.job-name }} + PR_ID: ${{ steps.pr-number.outputs.pr-number }} run: | . "$GITHUB_ACTION_PATH/scripts/utils.sh" diff --git a/scripts/retrieve-old-report.sh b/scripts/retrieve-old-report.sh index ee83f08..06b13bf 100755 --- a/scripts/retrieve-old-report.sh +++ b/scripts/retrieve-old-report.sh @@ -40,7 +40,9 @@ has_report() { # 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 +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" @@ -56,13 +58,14 @@ if [ "$(in_private_repo)" != 'true' ] && [ "$JOB_NAME" ]; then 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:" -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' +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)" From 6b0b78e4962d7aae6243b4601ba8257be99e3a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Sun, 13 Jul 2025 15:02:37 +0200 Subject: [PATCH 09/10] fix(report): building the wrong derivation for home-manager Small typo that was breaking this script T-T: ```bash # Wrong: nix build --print-out-paths ".#homeConfigurations.$config.activationPackages" # Correct: nix build --print-out-paths ".#homeConfigurations.$config.config.home.activationPackage" ``` --- scripts/create-report.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/create-report.sh b/scripts/create-report.sh index cfe4209..cab2166 100755 --- a/scripts/create-report.sh +++ b/scripts/create-report.sh @@ -51,7 +51,7 @@ pkgs_json() { hm_configs_json() { for config in $hmConfigs; do log "Building $config" - path=$(nix build --print-out-paths ".#homeConfigurations.$config.activationPackages") + path=$(nix build --print-out-paths ".#homeConfigurations.$config.config.home.activationPackage") closure_size "$config" "$path" done } From e5d1a0751adb4963c0a4982503806ae5f19f52da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Sun, 13 Jul 2025 18:39:23 +0200 Subject: [PATCH 10/10] fix(report): use nix eval to list the components This is less error prone than parsing the information from `nix flake show`. --- scripts/create-report.sh | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/scripts/create-report.sh b/scripts/create-report.sh index cab2166..6e04b9f 100755 --- a/scripts/create-report.sh +++ b/scripts/create-report.sh @@ -7,24 +7,25 @@ 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[]') +# 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=$(echo "$flake_info" | jq --raw-output '.homeConfigurations | select(. != null) | keys[]') +hmConfigs=$(get_names .#homeConfigurations) [ -z "$hmConfigs" ] || log "$hmConfigs" endgroup group 'Show NixOS Configurations' -nixosConfigs=$(echo "$flake_info" | jq --raw-output '.nixosConfigurations | select(. != null) | keys[]') +nixosConfigs=$(get_names .#nixosConfigurations) [ -z "$nixosConfigs" ] || log "$nixosConfigs" endgroup