name: 'Nix Flake Outputs Size Report' author: Jalil David Salamé Messina description: | Use `nix path-info` to query the size of flake outputs and produce a markdown report. You can post this report as a comment to the PR associated with the current branch and/or export the report as a markdown artifact. Requires `nix`, `jq`, `curl`, `sed` and `coreutils` to be in path. Usage: ```yaml - name: Generate size report uses: https://git.salame.cl/jalil/nix-flake-outputs-size@main with: # Default values comment-on-pr: 'true' generate-artifact: 'false' artifact-name: 'size-report.md' base-branch: ${{ github.base_ref }} # set to e.g. main if not triggered by a pull_request job-name: '' # set to the name of this job if you want to enable comparisons ``` inputs: comment-on-pr: description: Comment the report on the PR associated with the current branch. default: 'true' generate-artifact: description: Export the generated markdown document as an artifact. default: 'false' artifact-name: description: The name of the generated artifact. default: report.json base-branch: description: | The name of the base branch, defaults to github.base_ref which is only available in pull_request triggered workflows, for other workflows specify it manually. It will try to download the generated artifact from this branch so make sure `generate-artifacte: true` when the workflow is running on that branch. default: ${{ github.base_ref }} job-name: description: | The name of the job running this action. If not set, no comparisons can be made. default: '' outputs: runs: using: 'composite' steps: - name: Create report run: | "$GITHUB_ACTION_PATH/create-report.sh" > report.json - name: Upload Artifact uses: https://code.forgejo.org/forgejo/upload-artifact@v4 if: inputs.generate-artifact == 'true' with: path: report.json name: ${{ inputs.artifact-name }} - name: Comment Report if: inputs.comment-on-pr == 'true' env: ARTIFACT_NAME: ${{ inputs.artifact-name }} HEAD_BRANCH: ${{ inputs.base-branch }} JOB_NAME: ${{ inputs.job-name }} run: | . "${GITHUB_ACTION_PATH}/utils.sh" # 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 "$HEAD_BRANCH" \ '[.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}' \ -H "Authorization: token $GITHUB_TOKEN" \ "$1") log "Got code $http_code for $url" test "$http_code" = '200' } # If we have a previous report compare against it if [ "$JOB_NAME" ] && [ "$HEAD_BRANCH" ]; then url=$(base_report_url) 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' \ -H "Authorization: token $GITHUB_TOKEN" \ "$url/artifacts/$ARTIFACT_NAME" | gunzip > old-report.json log "Reporting on sizes and comparing to sizes in $HEAD_BRANCH" "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json old-report.json exit 0 else log "Failed to find previous report, expected at: $report_url" fi fi log 'Reporting on sizes' "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json