fix(action): better logging
Be more clear about why it failed
This commit is contained in:
parent
7cc7a6f6f4
commit
08d9598c26
4 changed files with 62 additions and 24 deletions
18
action.yml
18
action.yml
|
@ -66,6 +66,8 @@ runs:
|
||||||
HEAD_BRANCH: ${{ inputs.base-branch }}
|
HEAD_BRANCH: ${{ inputs.base-branch }}
|
||||||
JOB_NAME: ${{ inputs.job-name }}
|
JOB_NAME: ${{ inputs.job-name }}
|
||||||
run: |
|
run: |
|
||||||
|
. "${GITHUB_ACTION_PATH}/utils.sh"
|
||||||
|
|
||||||
# USAGE: base_report_url
|
# USAGE: base_report_url
|
||||||
base_report_url() {
|
base_report_url() {
|
||||||
curl -X 'GET' \
|
curl -X 'GET' \
|
||||||
|
@ -77,27 +79,33 @@ runs:
|
||||||
--arg head_branch "$HEAD_BRANCH" \
|
--arg head_branch "$HEAD_BRANCH" \
|
||||||
'[.workflow_runs[] | select(.name == $name and .head_branch == $head_branch)] | first | .url'
|
'[.workflow_runs[] | select(.name == $name and .head_branch == $head_branch)] | first | .url'
|
||||||
}
|
}
|
||||||
# USAGE: has_report <RUN_URL>
|
# USAGE: has_report <REPORT_URL>
|
||||||
has_report() {
|
has_report() {
|
||||||
http_code=$(curl -X 'GET' -o /dev/null --silent -Iw '%{http_code}' \
|
http_code=$(curl -X 'GET' -o /dev/null --silent -Iw '%{http_code}' \
|
||||||
-H "Authorization: token $GITHUB_TOKEN" \
|
-H "Authorization: token $GITHUB_TOKEN" \
|
||||||
"$1/artifacts/$ARTIFACT_NAME")
|
"$1")
|
||||||
|
log "Got code $http_code for $url"
|
||||||
test "$http_code" = '200'
|
test "$http_code" = '200'
|
||||||
}
|
}
|
||||||
|
|
||||||
# If we have a previous report compare against it
|
# If we have a previous report compare against it
|
||||||
if [ "$JOB_NAME" ] && [ "$HEAD_BRANCH" ]; then
|
if [ "$JOB_NAME" ] && [ "$HEAD_BRANCH" ]; then
|
||||||
url=$(base_report_url)
|
url=$(base_report_url)
|
||||||
echo "Found previous run at: $url"
|
log "Found previous run at: $url"
|
||||||
if has_report "$url"; then
|
report_url="$url/artifacts/$ARTIFACT_NAME"
|
||||||
|
if has_report "$report_url"; then
|
||||||
|
log 'Found previous report, downloading...'
|
||||||
curl -X 'GET' \
|
curl -X 'GET' \
|
||||||
-H "Authorization: token $GITHUB_TOKEN" \
|
-H "Authorization: token $GITHUB_TOKEN" \
|
||||||
"$url/artifacts/$ARTIFACT_NAME" |
|
"$url/artifacts/$ARTIFACT_NAME" |
|
||||||
gunzip > old-report.json
|
gunzip > old-report.json
|
||||||
echo 'Found previous report'
|
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/comment_on_pr.sh" report.json old-report.json
|
||||||
exit 0
|
exit 0
|
||||||
|
else
|
||||||
|
log "Failed to find previous report, expected at: $report_url"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
log 'Reporting on sizes'
|
||||||
"$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json
|
"$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json
|
||||||
|
|
|
@ -2,6 +2,11 @@
|
||||||
|
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
|
util_path="${GITHUB_ACTION_PATH-.}/utils.sh"
|
||||||
|
|
||||||
|
# shellcheck source=utils.sh
|
||||||
|
. "${util_path}"
|
||||||
|
|
||||||
# USAGE: json_to_md_rows <FIELD> [JSON_FILE]
|
# USAGE: json_to_md_rows <FIELD> [JSON_FILE]
|
||||||
#
|
#
|
||||||
# JSON_FILE can be piped from stdin
|
# JSON_FILE can be piped from stdin
|
||||||
|
@ -96,11 +101,11 @@ if [ "${CI-false}" != 'true' ]; then
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo 'Determine head_ref'
|
log 'Determine head_ref'
|
||||||
# For push & tag events it'll bet GITHUB_REF_NAME, for pull_request events it'll be GITHUB_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}
|
head_ref=${GITHUB_REF_NAME-$GITHUB_HEAD_REF}
|
||||||
|
|
||||||
echo "Get PR number for $head_ref"
|
log "Get PR number for $head_ref"
|
||||||
prs=$(curl -X 'GET' \
|
prs=$(curl -X 'GET' \
|
||||||
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls?state=open&sort=recentupdate" \
|
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls?state=open&sort=recentupdate" \
|
||||||
-H "Authorization: token $GITHUB_TOKEN" \
|
-H "Authorization: token $GITHUB_TOKEN" \
|
||||||
|
@ -111,22 +116,25 @@ pr_number=$(echo "$prs" |
|
||||||
|
|
||||||
# Protect against running before a PR is made or if it is triggered on the main branch
|
# Protect against running before a PR is made or if it is triggered on the main branch
|
||||||
if [ -z "$pr_number" ]; then
|
if [ -z "$pr_number" ]; then
|
||||||
echo "No PR created for this commit"
|
log "No PR created for this commit"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Retrieved index: $pr_number" >&2
|
log "Retrieved index: $pr_number"
|
||||||
echo "Expected PR URL: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pulls/$pr_number" >&2
|
log "Expected PR URL: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pulls/$pr_number"
|
||||||
|
|
||||||
echo 'Generating comment body' >&2
|
log 'Generating comment body'
|
||||||
comment=$(markdown_from_report "$@")
|
comment=$(markdown_from_report "$@")
|
||||||
|
|
||||||
echo 'Posting comment:' >&2
|
group 'Comment Data:'
|
||||||
echo "$comment" >&2
|
log "$comment"
|
||||||
|
endgroup
|
||||||
|
|
||||||
echo 'Request data:' >&2
|
|
||||||
data=$(echo '{}' | jq --arg comment "$comment" '.body=$comment')
|
data=$(echo '{}' | jq --arg comment "$comment" '.body=$comment')
|
||||||
echo "$data" >&2
|
group 'Request data:'
|
||||||
|
log "$data"
|
||||||
|
endgroup
|
||||||
|
|
||||||
curl -o - -X 'POST' \
|
curl -o - -X 'POST' \
|
||||||
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number/comments" \
|
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number/comments" \
|
||||||
-H 'accept: application/json' \
|
-H 'accept: application/json' \
|
||||||
|
|
|
@ -2,41 +2,52 @@
|
||||||
|
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
echo 'Retrieving Flake information' >&2
|
util_path="${GITHUB_ACTION_PATH-.}/utils.sh"
|
||||||
|
|
||||||
|
# shellcheck source=utils.sh
|
||||||
|
. "${util_path}"
|
||||||
|
|
||||||
|
log 'Retrieving Flake information'
|
||||||
flake_info=$(nix flake show --json 2>/dev/null)
|
flake_info=$(nix flake show --json 2>/dev/null)
|
||||||
packages=$(
|
packages=$(
|
||||||
jq --raw-output '.packages."x86_64-linux" | select(. != null) | keys[]' <<-EOF
|
jq --raw-output '.packages."x86_64-linux" | select(. != null) | keys[]' <<-EOF
|
||||||
$flake_info
|
$flake_info
|
||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
echo "Packages:" >&2
|
group 'Packages:'
|
||||||
echo "$packages" >&2
|
log "$packages"
|
||||||
|
endgroup
|
||||||
configurations=$(
|
configurations=$(
|
||||||
jq --raw-output '.nixosConfigurations | select(. != null) | keys[]' <<-EOF
|
jq --raw-output '.nixosConfigurations | select(. != null) | keys[]' <<-EOF
|
||||||
$flake_info
|
$flake_info
|
||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
echo "NixOS Configurations:" >&2
|
group 'NixOS Configurations:'
|
||||||
echo "$configurations" >&2
|
log "$configurations"
|
||||||
|
endgroup
|
||||||
|
|
||||||
pkgs_json() {
|
pkgs_json() {
|
||||||
|
group 'Building packages'
|
||||||
for package in $packages; do
|
for package in $packages; do
|
||||||
echo "Building $package" >&2
|
log "Building $package"
|
||||||
path=$(nix build --print-out-paths ".#$package" 2>/dev/null)
|
path=$(nix build --print-out-paths ".#$package" 2>/dev/null)
|
||||||
echo "Calculating size of $package" >&2
|
log "Calculating size of $package"
|
||||||
nix path-info --closure-size --json "$path" 2>/dev/null |
|
nix path-info --closure-size --json "$path" 2>/dev/null |
|
||||||
jq --compact-output --arg pkg "$package" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}'
|
jq --compact-output --arg pkg "$package" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}'
|
||||||
done
|
done
|
||||||
|
endgroup
|
||||||
}
|
}
|
||||||
|
|
||||||
configs_json() {
|
configs_json() {
|
||||||
|
group 'Building NixOS configurations'
|
||||||
for config in $configurations; do
|
for config in $configurations; do
|
||||||
echo "Building $config" >&2
|
log "Building $config"
|
||||||
path=$(nix build --print-out-paths ".#nixosConfigurations.$config.config.system.build.toplevel" 2>/dev/null)
|
path=$(nix build --print-out-paths ".#nixosConfigurations.$config.config.system.build.toplevel" 2>/dev/null)
|
||||||
echo "Calculating size of $config" >&2
|
log "Calculating size of $config"
|
||||||
nix path-info --closure-size --json "$path" 2>/dev/null |
|
nix path-info --closure-size --json "$path" 2>/dev/null |
|
||||||
jq --compact-output --arg pkg "$config" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}'
|
jq --compact-output --arg pkg "$config" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}'
|
||||||
done
|
done
|
||||||
|
endgroup
|
||||||
}
|
}
|
||||||
|
|
||||||
pkgs=$(pkgs_json | jq --slurp '.')
|
pkgs=$(pkgs_json | jq --slurp '.')
|
||||||
|
|
11
utils.sh
Normal file
11
utils.sh
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
log() {
|
||||||
|
echo "$@" >&2
|
||||||
|
}
|
||||||
|
|
||||||
|
group() {
|
||||||
|
log "::group::$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
endgroup() {
|
||||||
|
log '::endgroup::'
|
||||||
|
}
|
Loading…
Reference in a new issue