nix-flake-outputs-size/action.yml

81 lines
2.7 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 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'
```
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: 'size-report.md'
outputs:
runs:
using: 'composite'
steps:
- name: Create report
run: |
"$GITHUB_ACTION_PATH/create-report.sh"
- name: Upload Artifact
uses: https://code.forgejo.org/forgejo/upload-artifact@v4
if: inputs.generate-artifact == 'true'
with:
path: size-report.md
name: ${{ inputs.artifact-name }}
- name: Upload Report
if: inputs.comment-on-pr == 'true'
run: |
set -eu
# 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}
pr_number=$(curl -X 'GET' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls?state=open&sort=recentupdate" \
-H 'accept: application/json' |
jq --arg head_ref "$head_ref" '.[] | select(.head.ref == $head_ref) | .number')
# Protect against running before a PR is made or if it is triggered on the main branch
if [ -z "$pr_number" ]; then
echo "No PR created for this commit"
exit 0
fi
echo "Retrieved index: $pr_number" >&2
echo "Expected PR URL: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pulls/$pr_number" >&2
echo 'Generating comment body' >&2
comment=$(cat size-report.md)
echo 'Posting comment:' >&2
echo "$comment" >&2
echo 'Request data:' >&2
data=$(echo '{}' | jq --arg comment "$comment" '.body=$comment')
echo "$data" >&2
curl -o - -X 'POST' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number/comments" \
-H 'accept: application/json' \
-H "Authorization: token $GITHUB_TOKEN" \
-H 'Content-Type: application/json' \
-d "$data"