2022-04-08 12:09:59 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# Used in CI workflow: install Zola binary from GitHub
|
|
|
|
# Copyright © 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
2022-04-09 11:19:16 +00:00
|
|
|
readonly PROJECT_ROOT=$(pwd)
|
2022-04-08 12:09:59 +00:00
|
|
|
readonly TARBALL=zola.tar.gz
|
|
|
|
readonly SOURCE="https://github.com/getzola/zola/releases/download/v0.15.3/zola-v0.15.3-x86_64-unknown-linux-gnu.tar.gz"
|
|
|
|
|
2022-04-09 11:19:16 +00:00
|
|
|
readonly BIN_PATH=tmp/bin
|
2022-04-08 12:09:59 +00:00
|
|
|
readonly BIN=$BIN_PATH/zola
|
|
|
|
|
|
|
|
readonly DIST=public
|
|
|
|
|
|
|
|
source $(pwd)/scripts/lib.sh
|
|
|
|
|
|
|
|
help() {
|
|
|
|
cat << EOF
|
|
|
|
zola.sh: Zola build script
|
|
|
|
USAGE:
|
|
|
|
zola.sh <options>
|
|
|
|
OPTIONS:
|
|
|
|
b build build website
|
|
|
|
c clean clean dependencies and build artifacts
|
2022-04-09 12:11:39 +00:00
|
|
|
d deploy deploy build to branch
|
2022-04-08 12:09:59 +00:00
|
|
|
h help print this help menu
|
|
|
|
i install install build dependencies
|
|
|
|
u url make urls relative
|
|
|
|
z zola invoke zola
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
download() {
|
2022-04-09 11:19:16 +00:00
|
|
|
if [ ! -e $BIN_PATH ];
|
|
|
|
then
|
|
|
|
mkdir -p $BIN_PATH
|
|
|
|
cd $BIN_PATH
|
|
|
|
echo "[*] Downloading Zola"
|
|
|
|
wget --quiet --output-document=$TARBALL $SOURCE
|
|
|
|
tar -xvzf $TARBALL > /dev/null
|
|
|
|
rm $TARBALL
|
|
|
|
echo "[*] Downloaded zola into $BIN"
|
|
|
|
cd $PROJECT_ROOT
|
|
|
|
fi
|
2022-04-08 12:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
if [ ! -d $BIN_PATH ]
|
|
|
|
then
|
|
|
|
mkdir $BIN_PATH
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -f $BIN ]
|
|
|
|
then
|
|
|
|
cd $BIN_PATH
|
|
|
|
download
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
run() {
|
|
|
|
$BIN "${@:1}"
|
|
|
|
}
|
|
|
|
|
|
|
|
build() {
|
|
|
|
run build
|
|
|
|
}
|
|
|
|
|
|
|
|
no_absolute_url() {
|
|
|
|
sed -i 's/https:\/\/hostea.org//g' $(find public -type f | grep html)
|
|
|
|
}
|
|
|
|
|
|
|
|
clean() {
|
|
|
|
rm -rf $BIN_PATH || true
|
|
|
|
rm -rf $DIST || true
|
2022-04-09 11:19:16 +00:00
|
|
|
echo "[*] Workspace cleaned"
|
2022-04-08 12:09:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-09 12:11:39 +00:00
|
|
|
# $1: branch name
|
|
|
|
# $2: directory containing build assets
|
2022-04-16 02:52:23 +00:00
|
|
|
# $3: Author in <author-name author@example.com> format
|
2022-04-09 12:11:39 +00:00
|
|
|
deploy() {
|
|
|
|
cd $PROJECT_ROOT
|
2022-04-16 06:04:38 +00:00
|
|
|
original_branch=$(git branch --show-current)
|
2022-04-09 12:11:39 +00:00
|
|
|
tmp_dir=$(mktemp -d)
|
|
|
|
cp -r $2/* $tmp_dir
|
2022-04-16 07:22:59 +00:00
|
|
|
|
2022-04-16 07:46:39 +00:00
|
|
|
if [[ -z $(git ls-remote --heads origin ${1}) ]]
|
2022-04-16 07:22:59 +00:00
|
|
|
then
|
2022-04-16 07:46:39 +00:00
|
|
|
echo "[*] Creating deployment branch $1"
|
2022-04-16 07:22:59 +00:00
|
|
|
git checkout --orphan $1
|
|
|
|
else
|
2022-04-16 07:46:39 +00:00
|
|
|
echo "[*] Deployment branch $1 exists, pulling changes from remote"
|
2022-04-16 07:49:33 +00:00
|
|
|
git fetch origin $1
|
|
|
|
git switch $1
|
2022-04-16 07:22:59 +00:00
|
|
|
fi
|
|
|
|
|
2022-04-09 12:11:39 +00:00
|
|
|
git rm -rf .
|
|
|
|
/bin/rm -rf *
|
|
|
|
cp -r $tmp_dir/* .
|
2022-04-16 02:52:23 +00:00
|
|
|
git add --all
|
2022-04-16 07:58:27 +00:00
|
|
|
if [ $(git status --porcelain | xargs | sed '/^$/d' | wc -l) -gt 0 ];
|
2022-04-16 07:31:30 +00:00
|
|
|
then
|
2022-04-16 07:49:33 +00:00
|
|
|
echo "[*] Repository has changed, committing changes"
|
2022-04-16 07:31:30 +00:00
|
|
|
git commit \
|
|
|
|
--author="$3" \
|
|
|
|
--message="new deploy: $(date --iso-8601=seconds)"
|
|
|
|
fi
|
2022-04-16 06:04:38 +00:00
|
|
|
git checkout $original_branch
|
2022-04-09 12:11:39 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 12:09:59 +00:00
|
|
|
check_arg $1
|
2022-04-09 11:19:16 +00:00
|
|
|
download
|
2022-04-08 12:09:59 +00:00
|
|
|
|
|
|
|
if match_arg $1 'i' 'install'
|
|
|
|
then
|
|
|
|
init
|
|
|
|
elif match_arg $1 'c' 'clean'
|
|
|
|
then
|
|
|
|
clean
|
2022-04-09 12:11:39 +00:00
|
|
|
elif match_arg $1 'd' 'deploy'
|
|
|
|
then
|
|
|
|
check_arg $2
|
|
|
|
check_arg $3
|
2022-04-16 03:01:34 +00:00
|
|
|
check_arg $4
|
|
|
|
deploy $2 $3 $4
|
2022-04-08 12:09:59 +00:00
|
|
|
elif match_arg $1 'b' 'build'
|
|
|
|
then
|
|
|
|
build
|
|
|
|
elif match_arg $1 'h' 'help'
|
|
|
|
then
|
|
|
|
help
|
|
|
|
elif match_arg $1 'u' 'url'
|
|
|
|
then
|
|
|
|
no_absolute_url
|
|
|
|
elif match_arg $1 'z' 'zola'
|
|
|
|
then
|
|
|
|
$BIN "${@:3}"
|
|
|
|
else
|
|
|
|
echo "Error: $1 is not an option"
|
|
|
|
help
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit 0
|