From 4eec163da28a19abda198db7fbb2d40cebbbb5e2 Mon Sep 17 00:00:00 2001 From: GH Action - Upstream Sync Date: Sun, 18 Jul 2021 21:31:37 -0500 Subject: [PATCH] Fix exit issue with failed commands, some cleanup --- entry/config_and_run.sh | 1 - run/checkout_branch.sh | 9 ++++++--- run/get_updates.sh | 9 ++++++--- run/push_updates.sh | 7 +++++-- run/set_upstream_repo.sh | 2 +- test/verify_upstream_access.sh | 13 ++++++++----- util/output.sh | 4 ++-- 7 files changed, 28 insertions(+), 17 deletions(-) diff --git a/entry/config_and_run.sh b/entry/config_and_run.sh index a788143..748ca9d 100755 --- a/entry/config_and_run.sh +++ b/entry/config_and_run.sh @@ -7,7 +7,6 @@ ACTION_PARENT_DIR=$(dirname "$(dirname "$0")") # source script to handle message output . "${ACTION_PARENT_DIR}"/util/output.sh -# TODO: SPIKE on 'live' tests through the action runner if [ -z "${GITHUB_ACTIONS}" ] || [ "${GITHUB_ACTIONS}" = false ]; then write_out "b" "\nRunning in LOCAL MODE..." diff --git a/run/checkout_branch.sh b/run/checkout_branch.sh index 79b2f17..8cad526 100644 --- a/run/checkout_branch.sh +++ b/run/checkout_branch.sh @@ -3,11 +3,14 @@ # checkout target branch for updates checkout() { write_out -1 "Checking out target branch '${INPUT_SOURCE_SYNC_BRANCH}' for sync." - + # shellcheck disable=SC2086 - if ! git checkout ${INPUT_SOURCE_BRANCH_CHECKOUT_ARGS} "${INPUT_SOURCE_SYNC_BRANCH}"; then + git checkout ${INPUT_SOURCE_BRANCH_CHECKOUT_ARGS} "${INPUT_SOURCE_SYNC_BRANCH}" + COMMAND_STATUS=$? + + if [ "${COMMAND_STATUS}" != 0 ]; then # exit on branch checkout fail - write_out "$?" "Target branch could not be checked out." + write_out "${COMMAND_STATUS}" "Target branch could not be checked out." fi write_out -1 "Target branch checked out" diff --git a/run/get_updates.sh b/run/get_updates.sh index 4a59632..b5d7c51 100644 --- a/run/get_updates.sh +++ b/run/get_updates.sh @@ -15,7 +15,7 @@ check_for_updates() { HAS_NEW_COMMITS="error" elif [ "${LOCAL_COMMIT_HASH}" = "${UPSTREAM_COMMIT_HASH}" ]; then HAS_NEW_COMMITS=false - else # assumes that remote will never be behind local when using this action... + else # TODO: make this more robust, currently assumes that the syncing branch is never commited to :/ HAS_NEW_COMMITS=true fi @@ -47,9 +47,12 @@ sync_new_commits() { # pull_args examples: "--ff-only", "--tags", "--ff-only --tags" # shellcheck disable=SC2086 - if ! git pull --no-edit ${INPUT_UPSTREAM_PULL_ARGS} upstream "${INPUT_UPSTREAM_SYNC_BRANCH}"; then + git pull --no-edit ${INPUT_UPSTREAM_PULL_ARGS} upstream "${INPUT_UPSTREAM_SYNC_BRANCH}" + COMMAND_STATUS=$? + + if [ "${COMMAND_STATUS}" != 0 ]; then # exit on commit pull fail - write_out "$?" "New commits could not be pulled." + write_out "${COMMAND_STATUS}" "New commits could not be pulled." fi write_out "g" 'SUCCESS\n' diff --git a/run/push_updates.sh b/run/push_updates.sh index 79a5ec4..5f60952 100644 --- a/run/push_updates.sh +++ b/run/push_updates.sh @@ -5,9 +5,12 @@ push_new_commits() { write_out -1 'Pushing synced data to target branch.' # shellcheck disable=SC2086 - if ! git push ${INPUT_SOURCE_PUSH_ARGS} origin "${INPUT_SOURCE_SYNC_BRANCH}"; then + git push ${INPUT_SOURCE_PUSH_ARGS} origin "${INPUT_SOURCE_SYNC_BRANCH}" + COMMAND_STATUS=$? + + if [ "${COMMAND_STATUS}" != 0 ]; then # exit on push to source repo fail - write_out "$?" "Could not push changes to source repo." + write_out "${COMMAND_STATUS}" "Could not push changes to source repo." fi write_out "g" 'SUCCESS\n' diff --git a/run/set_upstream_repo.sh b/run/set_upstream_repo.sh index e9baa33..5817b6c 100644 --- a/run/set_upstream_repo.sh +++ b/run/set_upstream_repo.sh @@ -1,7 +1,7 @@ #!/bin/sh set_upstream() { - write_out -1 "Setting upstream repo." + write_out -1 "Setting upstream repo to '${INPUT_UPSTREAM_SYNC_REPO}'." git remote add upstream "${UPSTREAM_REPO_URL}" # # exit if upstream can't be accessed diff --git a/test/verify_upstream_access.sh b/test/verify_upstream_access.sh index bebe3a0..f5b9a22 100755 --- a/test/verify_upstream_access.sh +++ b/test/verify_upstream_access.sh @@ -30,11 +30,14 @@ cleanup_test_dir() { if [ "${SKIP_CLEANUP}" = true ]; then true # no-op skip else - # warn if cloned test directory can't be removed - if ! rm -rf "${TEST_CLONE_DIR}"; then - write_out "r" "(Clone cleanup failed - please find and remove directory '${TEST_CLONE_DIR}')\n" - fi + rm -rf "${TEST_CLONE_DIR}" + COMMAND_STATUS=$? - write_out -1 "(Clone directory cleanup successful.)\n" + # warn if cloned test directory can't be removed + if [ "${COMMAND_STATUS}" != 0 ] ; then + write_out "r" "(Clone cleanup failed - please find and remove directory '${TEST_CLONE_DIR}')\n" + else + write_out -1 "(Clone directory cleanup successful.)\n" + fi fi } diff --git a/util/output.sh b/util/output.sh index 53b5384..639062f 100644 --- a/util/output.sh +++ b/util/output.sh @@ -40,8 +40,8 @@ write_out() { # safe exit, green output 0) + printf '\n%s\n' "$2" 1>&1 echo "${BOLD}${GREEN}SAFE EXIT${NORMAL}" 1>&1 - printf '%s\n' "$2" 1>&1 early_exit_cleanup exit 0 @@ -50,7 +50,7 @@ write_out() { # exit on error, red output *) echo "${BOLD}${RED}ERROR: ${NORMAL} exit $1" 1>&2 - printf '%s\n' "$2" 1>&2 + printf '\n%s\n' "$2" 1>&2 echo "Try running in test mode to verify your action input. If that does not help, please open an issue." 1>&2 early_exit_cleanup