-
Notifications
You must be signed in to change notification settings - Fork 2
/
cron.sh
executable file
·72 lines (58 loc) · 2.65 KB
/
cron.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env bash
# =================================================================================================
# description: zed-news execution script, to be run by cron
# author: Victor Miti <https://github.com/engineervix>
# url: <https://github.com/engineervix/zed-news>
# version: 0.9.0
# license: BSD-3-Clause
#
# Logical steps
# 1. cd to project directory
# 2. Activate virtual environment
# 3. git pull
# 4. Run script inside docker container (cleanup afterwards)
# 5. commit changes
# 6. push changes to remote
# =================================================================================================
set -e # Exit immediately if any command fails
# 1. cd to project directory
cd "${HOME}/SITES/zed-news" || { echo "Failed to change directory."; exit 1; }
# Source the .env file so we can retrieve healthchecks.io ping URL
# shellcheck source=/dev/null
source .env
# Function to send success signal to healthchecks.io
function send_healthcheck_success() {
curl -fsS --retry 3 "${HEALTHCHECKS_PING_URL}" > /dev/null
}
# Function to send failure signal to healthchecks.io
function send_healthcheck_failure() {
curl -fsS --retry 3 "${HEALTHCHECKS_PING_URL}/fail" > /dev/null
}
# 2. Activate virtual environment
# shellcheck source=/dev/null
source "${HOME}/Env/zed-news/bin/activate" || { echo "Failed to activate virtual environment."; send_healthcheck_failure; exit 1; }
# 3. git pull
git pull || { echo "Failed to pull changes from Git."; send_healthcheck_failure; exit 1; }
# 4. Run script inside docker container
inv up --build || { echo "Failed to build Docker container."; send_healthcheck_failure; exit 1; }
docker compose run --rm app invoke toolchain || {
echo "Failed to run script inside Docker container."
send_healthcheck_failure
exit 1
}
inv down || { echo "Failed to stop Docker container."; send_healthcheck_failure; exit 1; }
# 5. commit changes
today_iso=$(date --iso)
git add app/web/_pages/episodes || { echo "Failed to stage changes for commit."; send_healthcheck_failure; exit 1; }
git commit --no-verify -m "chore: ✨ new episode 🎙️ » ${today_iso}" || { echo "Failed to commit changes."; send_healthcheck_failure; exit 1; }
# 6. push changes to remote
git push origin main || { echo "Failed to push changes to remote repository."; send_healthcheck_failure; exit 1; }
# Send success signal to healthchecks.io
send_healthcheck_success
# Pause for 5 minutes
sleep 300
# Notify Admin via Apprise + ntfy.sh
today_human_readable=$(date +"%a %d %b %Y")
apprise -vv -t "🎙️ New Episode » ${today_human_readable}" \
-b "📻 Listen now at ${BASE_URL}/episode/${today_iso}/" \
"${APPRISE_NTFY_URL}"