2024-08-26 16:28:32 +02:00
#!/bin/bash
2025-07-11 09:20:01 +00:00
exec >> /home/azureuser/dbt_tests.log 2>& 1
2024-08-26 16:28:32 +02:00
2025-07-11 09:20:01 +00:00
# Define the Slack webhook URL
2024-08-26 16:28:32 +02:00
script_dir = $( dirname " $0 " )
webhooks_file = "slack_webhook_urls.txt"
env_file = " $script_dir / $webhooks_file "
if [ -f " $env_file " ] ; then
export $( grep -v '^#' " $env_file " | xargs)
else
echo " Error: $webhooks_file file not found in the script directory. "
exit 1
fi
2025-07-11 09:20:01 +00:00
# Messages to be sent to Slack
2024-08-26 16:28:32 +02:00
slack_failure_message = ":rotating_light::rotating_light::rotating_light: One or more failures in dbt tests in production. :rotating_light::rotating_light::rotating_light:"
slack_success_message = ":white_check_mark::white_check_mark::white_check_mark: dbt tests executed successfully in production. :white_check_mark::white_check_mark::white_check_mark:"
2025-07-11 09:20:01 +00:00
# Initialize the failure flag
2024-08-26 16:28:32 +02:00
has_any_step_failed = 0
2025-07-11 09:20:01 +00:00
cd /home/azureuser/data-dwh-dbt-project
2024-08-26 16:28:32 +02:00
2025-07-11 09:20:01 +00:00
# Update from git
2024-08-26 16:28:32 +02:00
echo "Updating dbt project from git."
git checkout master
git pull
2025-07-11 09:20:01 +00:00
# Activate venv
2024-08-26 16:28:32 +02:00
source venv/bin/activate
2025-07-11 09:20:01 +00:00
# Run tests
2024-08-26 16:28:32 +02:00
echo "Triggering dbt test"
dbt test
if [ $? -ne 0 ] ; then
has_any_step_failed = 1
fi
2025-07-11 09:20:01 +00:00
# Check if any step failed and send a Slack message
if [ $has_any_step_failed -eq 1 ] ; then
curl -X POST -H 'Content-type: application/json' --data " {\"text\":\" $slack_failure_message \"} " $SLACK_ALERT_WEBHOOK_URL
2025-06-26 14:58:08 +02:00
fi
2025-07-11 09:20:01 +00:00
if [ $has_any_step_failed -eq 0 ] ; then
curl -X POST -H 'Content-type: application/json' --data " {\"text\":\" $slack_success_message \"} " $SLACK_RECEIPT_WEBHOOK_URL
fi