Last active
February 13, 2023 07:09
-
-
Save back-2-95/ff0fe5c5d93051b727195fc889a9f34d to your computer and use it in GitHub Desktop.
Wodby Slack notifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
trap 'catch' EXIT | |
deploy() { | |
notify "start" | |
info "Install dependencies" | |
composer install --prefer-dist -n --no-dev | |
info "Setup public and private file folders" | |
ln -sfn /mnt/files/private /var/www/html/public/sites/default/files/private | |
sudo files_chown /mnt/files/public && sudo files_chmod /mnt/files/public | |
sudo files_chown /mnt/files/private && sudo files_chmod /mnt/files/private | |
info "Run Drush deploy" | |
drush deploy | |
notify "complete" | |
} | |
# Slack hook is added as ENV variable | |
SLACK_HOOK="${SLACK_HOOK}" | |
notify() { | |
NOTIFY=${1:-complete} | |
info "notify ${NOTIFY} called..." | |
if [ -z "${SLACK_HOOK}" ] | |
then | |
info "Env SLACK_HOOK was not provided, skipping..." | |
# Return without error: does not prevent following tasks | |
return | |
fi | |
APP="${WODBY_APP_NAME:-app_name}" | |
BRANCH="$(git rev-parse --abbrev-ref HEAD)" | |
GIT_MSG="$(git show -s --format='%an: %s - %h')" | |
SITE_URL="https://${WODBY_HOST_PRIMARY:-localhost}" | |
APP_UUID="${WODBY_APP_UUID:-app_uuid}" | |
INSTANCE_NAME="${WODBY_INSTANCE_NAME:-instance_name}" | |
WODBY_TASKS="https://cloud.wodby.com/apps/${APP_UUID}/${INSTANCE_NAME}/tasks" | |
GIT_MSG=${GIT_MSG//\"/\\\"} | |
GIT_MSG=${GIT_MSG//\'/\\\'} | |
if [ "${NOTIFY}" == "error" ]; then | |
MSG_BASE="$(printf '%s *[%s]* pipeline failed for branch `%s` on %s' ":x:" ${APP} ${BRANCH} ${SITE_URL})" | |
MSG="${MSG_BASE}\n"'```'"${GIT_MSG}"'```'"\n<${WODBY_TASKS}|See detailed logs>" | |
elif [ "${NOTIFY}" == "complete" ]; then | |
MSG_BASE="$(printf '%s *[%s]* branch `%s` on %s builded successfully' ":white_check_mark:" ${APP} ${BRANCH} ${SITE_URL})" | |
MSG="${MSG_BASE}\n"'```'"${GIT_MSG}"'```'"\n<${WODBY_TASKS}|See detailed logs>" | |
elif [ "${NOTIFY}" == "start" ]; then | |
MSG_BASE="$(printf '%s *[%s]* pipeline started for branch `%s` on %s ...' ":clock1:" ${APP} ${BRANCH} ${SITE_URL})" | |
MSG="${MSG_BASE}" | |
else | |
info "Error: NOTIFY value is not valid." | |
exit 1 | |
fi | |
#echo "$(printf 'payload={"text": "%s"}' "${MSG}" )" && exit | |
curl --silent --data-urlencode "$(printf 'payload={"text": "%s"}' "${MSG}" )" ${SLACK_HOOK} > /dev/null || true | |
} | |
catch() { | |
notify "error" | |
} | |
info() { | |
echo "[INFO] ${1}" | |
} | |
deploy | |
trap - EXIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipeline: | |
- name: Deployment | |
type: command | |
command: ./wodby.deploy.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requirements
wodby.deploy.sh
file in your repo root andchmod +x wodby.deploy.sh
SLACK_HOOK
for php container