2022-10-12 14:22:22 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
function error_handler() {
|
|
|
|
>&2 echo "Exited with BAD EXIT CODE '${2}' in ${0} script at line: ${1}."
|
|
|
|
exit "$2"
|
|
|
|
}
|
|
|
|
trap 'error_handler ${LINENO} $?' ERR
|
|
|
|
set -o errtrace -o errexit -o nounset -o pipefail
|
|
|
|
|
|
|
|
# HELP: git adds and commits the entire BPMN models directory, including all process groups
|
|
|
|
|
|
|
|
bpmn_models_absolute_dir="$1"
|
|
|
|
git_commit_message="$2"
|
2022-12-15 17:52:53 +00:00
|
|
|
git_branch="$3"
|
2022-10-12 14:22:22 +00:00
|
|
|
|
2023-02-20 21:21:31 +00:00
|
|
|
if [[ -z "${3:-}" ]]; then
|
|
|
|
>&2 echo "usage: $(basename "${0}") [bpmn_models_absolute_dir] [git_commit_message] [git_branch]"
|
2023-01-20 20:11:23 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-12-19 16:15:05 +00:00
|
|
|
function failed_to_get_lock() {
|
|
|
|
>&2 echo "ERROR: Failed to get lock."
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
function run() {
|
2023-02-20 21:21:31 +00:00
|
|
|
cd "${bpmn_models_absolute_dir}"
|
2022-12-19 16:15:05 +00:00
|
|
|
git add .
|
|
|
|
|
|
|
|
# https://unix.stackexchange.com/a/155077/456630
|
|
|
|
if [ -z "$(git status --porcelain)" ]; then
|
|
|
|
echo "No changes to commit"
|
2023-02-20 21:21:31 +00:00
|
|
|
return
|
2022-12-19 16:15:05 +00:00
|
|
|
fi
|
2023-02-20 21:21:31 +00:00
|
|
|
|
2023-02-22 20:34:47 +00:00
|
|
|
# FIXME: the environment variables may not be working with the root user which we are using in the docker container.
|
|
|
|
# we see some evidence with this issue https://stackoverflow.com/questions/68975943/git-config-environment-variables
|
|
|
|
# and it didn't seem to work for us either so set them like this for now.
|
|
|
|
# One day we should probably not use the root user in the docker container.
|
|
|
|
git config --local user.email "$GIT_COMMITTER_EMAIL"
|
|
|
|
git config --local user.name "$GIT_COMMITTER_NAME"
|
|
|
|
|
2023-02-20 21:21:31 +00:00
|
|
|
git commit -m "${git_commit_message}"
|
|
|
|
git push --set-upstream origin "${git_branch}"
|
2022-12-19 16:15:05 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 19:31:55 +00:00
|
|
|
lock_directory="/var/lock"
|
|
|
|
if [[ ! -d "${lock_directory}" ]]; then
|
|
|
|
lock_directory="/tmp"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! command -v flock >/dev/null 2>&1; then
|
|
|
|
if command -v brew >/dev/null 2>&1; then
|
|
|
|
# some hero made this available on mac: https://github.com/discoteq/flock
|
|
|
|
brew install flock
|
|
|
|
else
|
|
|
|
>&2 echo "ERROR: flock is not installed and we cannot install it."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
exec {lock_fd}>"${lock_directory}/spiffworkflow-git-lock" || failed_to_get_lock
|
2023-02-20 21:21:31 +00:00
|
|
|
flock --timeout 60 "${lock_fd}" || failed_to_get_lock
|
2022-12-19 16:15:05 +00:00
|
|
|
run
|
2023-02-20 21:21:31 +00:00
|
|
|
flock -u "${lock_fd}"
|