Skip to content

Commit

Permalink
Correctly write GITHUB_STEP_SUMMARY on failure
Browse files Browse the repository at this point in the history
ici_on_teardown(): allow commands to be executed on teardown
  • Loading branch information
rhaschke committed Feb 15, 2025
1 parent f9fa3bf commit e5ecc5e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
19 changes: 11 additions & 8 deletions src/scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
# shellcheck source=src/build.sh
source "${SRC_PATH}/build.sh"

function on_exit() {
ici_log
ici_timed "ccache statistics" ccache -sv

if [ "${#BUILT_PACKAGES[@]}" -gt 0 ]; then
echo "### Successfully built packages: " > "$GITHUB_STEP_SUMMARY"
printf -- '- %s\n' "${BUILT_PACKAGES[@]}" >> "$GITHUB_STEP_SUMMARY"
fi
}

gha_report_result "LATEST_PACKAGE" ""

FAIL_EVENTUALLY=0
BUILT_PACKAGES=()
ici_on_teardown on_exit
build_all_sources

ici_log
ici_timed "ccache statistics" ccache -sv

if [ "${#BUILT_PACKAGES[@]}" -gt 0 ]; then
echo "### Successfully built packages: " > "$GITHUB_STEP_SUMMARY"
printf -- '- %s\n' "${BUILT_PACKAGES[@]}" >> "$GITHUB_STEP_SUMMARY"
fi

gha_report_result "LATEST_PACKAGE" "DONE"

if [ "$FAIL_EVENTUALLY" != 0 ]; then
Expand Down
16 changes: 13 additions & 3 deletions src/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export ANSI_RESET=0
export TRACE=${TRACE:-false}
export ICI_FOLD_NAME=${ICI_FOLD_NAME:-}
export ICI_START_TIME=${ICI_START_TIME:-}
_CLEANUP=""
_CLEANUP_FILES=""
declare -a _CLEANUP_CMDS

__ici_log_fd=1
__ici_err_fd=2
Expand Down Expand Up @@ -243,6 +244,11 @@ function ici_timed {
ici_time_end
}

# Register command to be executed on teardown
function ici_on_teardown {
_CLEANUP_CMDS+=("$@")
}

function ici_teardown {
local exit_code=${1:-$?}; shift || true

Expand All @@ -253,7 +259,7 @@ function ici_teardown {

local cleanup=()
# shellcheck disable=SC2016
IFS=: command eval 'cleanup=(${_CLEANUP})'
IFS=: command eval 'cleanup=(${_CLEANUP_FILES})'
for c in "${cleanup[@]}"; do
rm -rf "${c/#\~/$HOME}"
done
Expand All @@ -278,6 +284,10 @@ function ici_teardown {
fi
fi

for c in "${_CLEANUP_CMDS[@]}"; do
$c
done

if [ "$__ici_setup_called" = true ]; then
# These will fail if ici_setup was not called
exec {__ici_log_fd}>&-
Expand Down Expand Up @@ -598,7 +608,7 @@ function ici_join_array {

function ici_cleanup_later {
ici_trace "$@"
_CLEANUP=$(ici_join_array : "$_CLEANUP" "$@")
_CLEANUP_FILES=$(ici_join_array : "$_CLEANUP_FILES" "$@")
}

function ici_make_temp_dir {
Expand Down

0 comments on commit e5ecc5e

Please sign in to comment.