Skip to content

Instantly share code, notes, and snippets.

@back-2-95
Last active February 13, 2023 07:09
Show Gist options
  • Save back-2-95/ff0fe5c5d93051b727195fc889a9f34d to your computer and use it in GitHub Desktop.
Save back-2-95/ff0fe5c5d93051b727195fc889a9f34d to your computer and use it in GitHub Desktop.
Wodby Slack notifier
#!/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
pipeline:
- name: Deployment
type: command
command: ./wodby.deploy.sh
@back-2-95
Copy link
Author

back-2-95 commented May 19, 2020

Requirements

  • Create Slack App in https://api.slack.com/apps
  • Create integration to Slack and to a channel you want to send messages.
  • Create wodby.deploy.sh file in your repo root and chmod +x wodby.deploy.sh
  • Add Webhook URL for that channel as ENV variable SLACK_HOOK for php container

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment