A simple Codeberg Pages Deployment Script
There are multiple ways to deploy a static site to Codeberg Pages. I will leverage a bash script to do so.
Setup
I have the pages
branch setup for Codeberg Pages:
Create a branch
pages
in a public repository:
git switch --orphan pages
git rm --cached -r .
I did not create two repos as suggested in the docs but wrote a simple script to deploy this site.
I named the script buildAndDeploy.sh
and added it to my main
branch.
Don’t forget to make the script executable with chmod +x buildAndDeploy.sh
.
The script assumes that it is located in the base folder of your project.
It is way more comfortable if you have your git credentials stored on disk, otherwise you will have to type username and password several times.
You need to have rsync installed for this script.
You need to git-ignore the build folder, here public
. Add it to your .gitignore
file or create such a file.
/public/
.hugo_build.lock
Script
#!/bin/bash
set -Eeuo pipefail
## variables
scriptpath="$(dirname "$(realpath -s "$0")")"
current_path=$(pwd)
current_branch=$(git rev-parse --abbrev-ref HEAD)
current_head_hash_short=$(git rev-parse --short HEAD)
# set $date to current date and time
printf -v date '%(%Y-%m-%d %H:%M:%S)T\n' -1
## functions
build () {
echo ">>> build page"
if ! hugo--ignoreCache ; then
exit 1
fi
}
deploy () {
echo ">>> switch to branch 'pages'"
if ! git switch pages; then
echo ">>> Error: please stash or commit all changes first"
exit 1
fi
echo ">>> update branch 'pages'"
rsync -av --delete --exclude=.git --exclude=.domains --exclude=public public/ .
rm -rf public
if [ "$(git ls-files --other --exclude-standard --directory | wc -l)" = 0 ] && git diff --exit-code > /dev/null; then \
echo ">>> no changes, nothing to deploy"
git switch "$current_branch"
cd "$current_path"
else
echo ">>> publish"
git add .
git commit -m "release: $date $current_head_hash_short" --author="Deployment Script <script@example.com>"
git push
echo ">>> switch back to $current_branch"
git switch "$current_branch"
cd "$current_path"
fi
}
## main
echo "You build and deploy from '$current_branch' !"
# Ask for confirmation
[[ "$(read -e -p 'Continue? [y/N]> '; echo "$REPLY")" == [Yy]* ]] \
&& echo ">>> switch to base directory" \
&& cd "$scriptpath" \
&& build \
&& deploy \
|| echo Abort!
Whats happening?
- Get the path to the script and the current path
- Get the branch and short commit hash you are on
- Get the current date and time
- Confirm that you want to build form the branch
- cd into the directory of the script
- Build the page. Here it is the command
hugo
. Replace it with the build command of your SSG (static site generator) - Switch to the
pages
branch - Copy the build folder, here
public
, to the base folder withrsync
. Becausersync
runs with--delete
the script needs to ignore (keep) the folders.git
,.domains
and the build folder, herepublic
- Remove the build folder, here
public
in the branchpages
- Check if there were any changes in the branch
pages
- Add everything, make a commit with the current date, time and source commit hash and push to codeberg with ‘Deployment Script’ as author
- Switch back to the branch you are building from and change back to the
$current_path
directory
Questions, comments, ideas?
Just open an issue on the repo or get in contact via mastodon