2024-09-09 12:37:59 +00:00
|
|
|
#!/bin/bash -eE
|
2024-02-14 14:48:34 +00:00
|
|
|
|
|
|
|
exec 100> $HOME/homepage.lock
|
|
|
|
|
|
|
|
flock 100
|
|
|
|
|
2024-09-09 12:37:59 +00:00
|
|
|
log=$(mktemp)
|
|
|
|
|
|
|
|
catch_err () {
|
|
|
|
(
|
|
|
|
echo "An error occured during building"
|
|
|
|
echo ""
|
|
|
|
cat "$log"
|
|
|
|
) >&12
|
|
|
|
}
|
|
|
|
|
|
|
|
catch_exit() {
|
|
|
|
rm -f "$log"
|
|
|
|
}
|
|
|
|
|
|
|
|
trap catch_err ERR
|
|
|
|
trap catch_exit EXIT
|
|
|
|
|
2024-02-14 14:48:34 +00:00
|
|
|
src=/srv/data/tsc-cloud/homepage/hugo-page
|
|
|
|
dst=/srv/http/tsc/hugo
|
2024-02-20 07:59:42 +00:00
|
|
|
prod=/srv/data/tsc-cloud/homepage/production
|
2024-02-14 14:48:34 +00:00
|
|
|
|
2024-09-09 12:50:43 +00:00
|
|
|
if [ -n "$SSH_ORIGINAL_COMMAND" ]
|
|
|
|
then
|
|
|
|
cmd="$SSH_ORIGINAL_COMMAND"
|
|
|
|
else
|
|
|
|
cmd="$@"
|
|
|
|
fi
|
2024-02-14 14:48:34 +00:00
|
|
|
# echo "$cmd"
|
|
|
|
|
|
|
|
publishToStage=
|
|
|
|
publishToProduction=y
|
2024-09-09 12:50:43 +00:00
|
|
|
verboseLogs=n
|
2024-02-14 14:48:34 +00:00
|
|
|
|
2024-02-14 14:59:17 +00:00
|
|
|
branch=develop
|
|
|
|
|
2024-02-14 14:48:34 +00:00
|
|
|
parseCMD() {
|
|
|
|
while [ $# -gt 0 ]
|
|
|
|
do
|
|
|
|
part="$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
case "$part" in
|
|
|
|
stage)
|
|
|
|
publishToStage=y
|
|
|
|
publishToProduction=
|
|
|
|
;;
|
2024-02-14 14:59:17 +00:00
|
|
|
--branch)
|
|
|
|
branch="$1"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--debug)
|
|
|
|
set -x
|
|
|
|
;;
|
2024-09-09 12:50:43 +00:00
|
|
|
--verbose|-v)
|
|
|
|
verboseLogs=y
|
|
|
|
;;
|
2024-02-14 14:48:34 +00:00
|
|
|
*)
|
|
|
|
echo "Unknown command $part"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
parseCMD $cmd
|
|
|
|
|
2024-09-09 12:50:43 +00:00
|
|
|
if [ "$verboseLogs" = "n" ]
|
|
|
|
then
|
|
|
|
exec 11>&1 12>&2 > "$log" 2>&1
|
|
|
|
fi
|
|
|
|
|
2024-02-14 14:48:34 +00:00
|
|
|
doPublishToStage() {
|
|
|
|
rsync -ah --delete --delete-delay public/ "$dst/"
|
|
|
|
}
|
|
|
|
|
|
|
|
doPublishToProduction() {
|
2024-02-20 07:59:42 +00:00
|
|
|
# This complicated two step publication is required to speed up the transfer.
|
|
|
|
# The sftp-based production server does not allow to use checksums.
|
|
|
|
# Hugo recreates all files with the time stamp at building.
|
|
|
|
# Thus, all files would be transmitted as the time has changed.
|
|
|
|
# To overcome this, the intermediate cache should have the same times as the upstream server.
|
|
|
|
echo "Syncing to intermediate stage"
|
|
|
|
rsync -rlpcv --delete --delete-delay public/ $prod/
|
|
|
|
|
|
|
|
echo "Syncing from intermediate stage to production web server"
|
|
|
|
rclone sync --stats 3s $prod/ ionos:/
|
|
|
|
|
|
|
|
echo "Cleaning up empty directories"
|
2024-02-14 14:48:34 +00:00
|
|
|
rclone rmdirs ionos:/
|
|
|
|
}
|
|
|
|
|
|
|
|
# exit 1
|
|
|
|
|
|
|
|
cd "$src"
|
|
|
|
|
|
|
|
echo "Fetching the latest git commits"
|
|
|
|
git fetch
|
|
|
|
|
2024-02-14 14:59:17 +00:00
|
|
|
echo "Switching to the latest commit on branch $branch"
|
|
|
|
git reset --hard "origin/$branch"
|
2024-02-14 14:48:34 +00:00
|
|
|
|
|
|
|
echo "Updating NPM packages"
|
|
|
|
npm ci
|
|
|
|
|
|
|
|
echo "Dropping old public folder"
|
|
|
|
rm -rf public
|
|
|
|
|
|
|
|
echo "Building the page"
|
|
|
|
npm run build
|
|
|
|
|
|
|
|
echo "Synchronizing files to web server"
|
|
|
|
|
|
|
|
if [ -n "$publishToStage" ]
|
|
|
|
then
|
|
|
|
echo "Pushing to stage"
|
2024-09-09 12:37:59 +00:00
|
|
|
( time doPublishToStage )
|
2024-02-14 14:48:34 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$publishToProduction" ]
|
|
|
|
then
|
|
|
|
echo "Publishing to production server"
|
2024-09-09 12:37:59 +00:00
|
|
|
( time doPublishToProduction )
|
2024-02-14 14:48:34 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Deployment done"
|
|
|
|
|