203 lines
7.6 KiB
YAML
203 lines
7.6 KiB
YAML
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 JSON artifact.
|
|
|
|
Requires `nix`, `jq`, `curl`, `sed`, `gunzip`, `tar` and `coreutils` to be in
|
|
the runner's 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'
|
|
# If you want to enable comparisons set this to true
|
|
do-comparison: 'false'
|
|
job-name: '' # required if do-comparison is true
|
|
# This is the branch that will be compared against
|
|
base-branch: ${{ github.base_ref }} # or default branch if missing
|
|
```
|
|
inputs:
|
|
comment-on-pr:
|
|
description: |
|
|
Comment the report on the PR associated with the current branch.
|
|
|
|
This is a no-op in case no PR is associated with the current branch.
|
|
default: 'true'
|
|
# Generate workflow artifact
|
|
generate-artifact:
|
|
description: Export the generated markdown document as a workflow artifact.
|
|
default: 'false'
|
|
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: |
|
|
When commenting on the PR compare the results with those in the base branch.
|
|
|
|
This is a no-op when `comment-on-pr: false`.
|
|
|
|
It requires `job-name` to be set.
|
|
default: 'false'
|
|
job-name:
|
|
description: |
|
|
The name of the job running this action. If not set, no comparisons can
|
|
be made.
|
|
default: ''
|
|
base-branch:
|
|
description: |
|
|
The name of the base branch, defaults to `github.base_ref` if present
|
|
(when triggered by a `pull_request`). Otherwise defaults to the repo's
|
|
default branch. Set in case you want to override this behaviour.
|
|
|
|
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 }}
|
|
outputs:
|
|
runs:
|
|
using: 'composite'
|
|
steps:
|
|
- name: Find PR (if it exists)
|
|
id: pr-number
|
|
if: inputs.comment-on-pr == 'true'
|
|
run: |
|
|
. "$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 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
|
|
with:
|
|
issue-number: ${{ steps.pr-number.outputs.pr-number }}
|
|
direction: first
|
|
body-includes: "<!-- AUTOGENERATED by nix-flake-outputs-size action -->"
|
|
- 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:
|
|
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 [ -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
|
|
exit 0
|
|
else
|
|
error 'Failed to do comparison, fallback to posting the report without them'
|
|
fi
|
|
fi
|
|
|
|
# Just report values
|
|
log 'Reporting on sizes'
|
|
"$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'
|
|
with:
|
|
path: report.json
|
|
name: ${{ inputs.artifact-name }}
|