-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_blog.sh
131 lines (111 loc) · 3.3 KB
/
update_blog.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
set -euo pipefail
# Change to the script's directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Set variables for Obsidian to Hugo copy
# Define arrays for source paths
declare -a source_paths=(
"/home/uam/Sync/ObsidianVaults/CodingNinjaJourney/posts"
"/home/uam/Sync/ObsidianVaults/CodingNinjaJourney/pages"
)
declare -a source_files=(
"/home/uam/Sync/ObsidianVaults/CodingNinjaJourney/_index.md"
)
destinationPath="/home/uam/Documents/CodingNinjaJourney/content"
# Set GitHub Repo
myrepo="[email protected]:misulik112/CodingNinjaJourney.git"
# Check for required commands
for cmd in git rsync python3 hugo; do
if ! command -v $cmd &>/dev/null; then
echo "$cmd is not installed or not in PATH."
exit 1
fi
done
# Step 1: Check if Git is initialized, and initialize if necessary
if [ ! -d ".git" ]; then
echo "Initializing Git repository..."
git init
git remote add origin $myrepo
else
echo "Git repository already initialized."
if ! git remote | grep -q 'origin'; then
echo "Adding remote origin..."
git remote add origin $myrepo
fi
fi
# Step 2: Sync posts from Obsidian to Hugo content folder using rsync
echo "Syncing posts from Obsidian..."
# Check and sync directories
for path in "${source_paths[@]}"; do
if [ ! -d "$path" ]; then
echo "Source path does not exist: $path"
exit 1
fi
rsync -av --delete "$path" "$destinationPath"
done
# Check and copy individual files
for file in "${source_files[@]}"; do
if [ ! -f "$file" ]; then
echo "Source file does not exist: $file"
exit 1
fi
cp "$file" "$destinationPath/$(basename "$file")"
done
if [ ! -d "$destinationPath" ]; then
echo "Destination path does not exist: $destinationPath"
exit 1
fi
# Step 3: Process Markdown files with Python script to handle image links
echo "Processing image links in Markdown files..."
if [ ! -f "scripts/images.py" ]; then
echo "Python script images.py not found."
exit 1
fi
if ! python3 scripts/images.py; then
echo "Failed to process image links."
exit 1
fi
# Step 4: Build the Hugo site
echo "Building the Hugo site..."
if ! hugo; then
echo "Hugo build failed."
exit 1
fi
# Step 5: Add changes to Git
echo "Staging changes for Git..."
if git diff --quiet && git diff --cached --quiet; then
echo "No changes to stage."
else
git add .
fi
# Step 6: Commit changes with a dynamic message
commit_message="New Blog Post on $(date +'%Y-%m-%d %H:%M:%S')"
if git diff --cached --quiet; then
echo "No changes to commit."
else
echo "Committing changes..."
git commit -m "$commit_message"
fi
# Step 7: Push all changes to the main branch
echo "Deploying to GitHub Main..."
if ! git push origin main; then
echo "Failed to push to main branch."
exit 1
fi
# Step 8: Push the public folder to the hostinger branch using subtree split and force push
echo "Deploying to GitHub Hostinger..."
if git branch --list | grep -q 'hostinger-deploy'; then
git branch -D hostinger-deploy
fi
if ! git subtree split --prefix public -b hostinger-deploy; then
echo "Subtree split failed."
exit 1
fi
if ! git push origin hostinger-deploy:hostinger --force; then
echo "Failed to push to hostinger branch."
git branch -D hostinger-deploy
exit 1
fi
git branch -D hostinger-deploy
echo "All done! Site synced, processed, committed, built, and deployed."