Fix exit issue with failed commands, some cleanup

This commit is contained in:
GH Action - Upstream Sync
2021-07-18 21:31:37 -05:00
parent f8b8552a56
commit 4eec163da2
7 changed files with 28 additions and 17 deletions
-1
View File
@@ -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..."
+6 -3
View File
@@ -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"
+6 -3
View File
@@ -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'
+5 -2
View File
@@ -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'
+1 -1
View File
@@ -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
+8 -5
View File
@@ -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
}
+2 -2
View File
@@ -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