feat: use download-artifact instead of manually downloading it
This should hopefully work on private repos too!
This commit is contained in:
parent
b079370cb2
commit
78313cff98
2 changed files with 42 additions and 80 deletions
43
action.yml
43
action.yml
|
@ -110,6 +110,44 @@ runs:
|
|||
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 }}
|
||||
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 | .id')
|
||||
|
||||
echo "run_id=$run_id" >> "$GITHUB_OUTPUT"
|
||||
- name: Download previous report
|
||||
uses: https://git.salame.cl/actions/download-artifact@v4
|
||||
# Ensure the previous step succeeded
|
||||
if: inputs.do-comparison == 'true' && steps.previous_report.outcome == 'success'
|
||||
with:
|
||||
name: ${{ inputs.artifact-name }}
|
||||
github-token: ${{ github.token }}
|
||||
run-id: ${{ steps.previous_report.run_id }}
|
||||
- name: Create report
|
||||
if: inputs.comment-on-pr == 'true' || inputs.generate-artifact == 'true'
|
||||
env:
|
||||
|
@ -129,6 +167,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 +178,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
|
||||
|
|
|
@ -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_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)" != '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
|
Loading…
Add table
Add a link
Reference in a new issue