Reworked the building of the website in a container with buildah

This commit is contained in:
Julien Dessaux 2022-09-06 00:40:46 +02:00
parent ae86aba801
commit a300c14972
Signed by: adyxax
GPG key ID: F92E51B86E07177E
2 changed files with 39 additions and 26 deletions

View file

@ -10,6 +10,10 @@ build: ## make build # builds an optimized version of the website in $(DESTDIR)
cp public/search/index.html search/ cp public/search/index.html search/
(cd search && CGO_ENABLED=0 go build -ldflags '-s -w -extldflags "-static"' ./search.go) (cd search && CGO_ENABLED=0 go build -ldflags '-s -w -extldflags "-static"' ./search.go)
.PHONY: buildah
buildah: ## make buildah # builds the container images
deploy/build-image.sh
.PHONY: clean .PHONY: clean
clean: ## make clean # removed all $(DESTDIR) contents clean: ## make clean # removed all $(DESTDIR) contents
@echo "----- Cleaning old build -----" @echo "----- Cleaning old build -----"

View file

@ -1,55 +1,64 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -eu set -euo pipefail
(cd .. && make clean build) ret=0; buildah images adyxax/alpine &>/dev/null || ret=$?
if [[ "${ret}" != 0 ]]; then
ret=0; output=$(buildah images adyxax/alpine &>/dev/null) || ret=$? buildah rmi --all
if [ $ret != 0 ]; then
ALPINE_LATEST=$(curl --silent https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/ | ALPINE_LATEST=$(curl --silent https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/ |
perl -lane '$latest = $1 if $_ =~ /^<a href="(alpine-minirootfs-\d+\.\d+\.\d+-x86_64\.tar\.gz)">/; END {print $latest}' perl -lane '$latest = $1 if $_ =~ /^<a href="(alpine-minirootfs-\d+\.\d+\.\d+-x86_64\.tar\.gz)">/; END {print $latest}'
) )
if [ ! -e "./${ALPINE_LATEST}" ]; then if [ ! -e "./${ALPINE_LATEST}" ]; then
echo "Fetching ${ALPINE_LATEST}..." echo "Fetching ${ALPINE_LATEST}..."
curl --silent https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/${ALPINE_LATEST} \ curl --silent "https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/${ALPINE_LATEST}" \
--output ./${ALPINE_LATEST} --output "./${ALPINE_LATEST}"
fi fi
ctr=$(buildah from scratch) ctr=$(buildah from scratch)
buildah add $ctr ${ALPINE_LATEST} / buildah add "${ctr}" "${ALPINE_LATEST}" /
buildah run $ctr /bin/sh -c 'apk add --no-cache pcre sqlite-libs' buildah run "${ctr}" /bin/sh -c 'apk upgrade --no-cache'
buildah commit $ctr adyxax/alpine buildah run "${ctr}" /bin/sh -c 'apk add --no-cache pcre sqlite-libs'
else buildah commit "${ctr}" adyxax/alpine
ctr=$(buildah from adyxax/alpine) buildah rm "${ctr}"
#buildah run $ctr /bin/sh -c 'apk upgrade --no-cache'
fi fi
ret=0; buildah images adyxax/hugo &>/dev/null || ret=$?
if [[ "${ret}" != 0 ]]; then
hugo=$(buildah from adyxax/alpine)
buildah run "${hugo}" /bin/sh -c 'apk add --no-cache go git hugo make'
buildah commit "${hugo}" adyxax/hugo
else
hugo=$(buildah from adyxax/hugo)
fi
buildah run -v "${PWD}":/www "${hugo}" -- sh -c 'cd /www; make build'
buildah rm "${hugo}"
ret=0; buildah images adyxax/nginx &>/dev/null || ret=$? ret=0; buildah images adyxax/nginx &>/dev/null || ret=$?
if [ $ret != 0 ]; then if [[ "${ret}" != 0 ]]; then
nginx=$(buildah from adyxax/alpine) nginx=$(buildah from adyxax/alpine)
buildah run $nginx /bin/sh -c 'apk add --no-cache nginx' buildah run "${nginx}" /bin/sh -c 'apk add --no-cache nginx'
buildah commit $nginx adyxax/nginx buildah commit "${nginx}" adyxax/nginx
else else
nginx=$(buildah from adyxax/nginx) nginx=$(buildah from adyxax/nginx)
#buildah run $nginx /bin/sh -c 'apk upgrade --no-cache'
fi fi
buildah copy $nginx nginx.conf headers_secure.conf headers_static.conf /etc/nginx/ (cd deploy && buildah copy "${nginx}" nginx.conf headers_secure.conf headers_static.conf /etc/nginx/)
buildah config \ buildah config \
--author 'Julien Dessaux' \ --author 'Julien Dessaux' \
--cmd nginx \ --cmd nginx \
--port 80 \ --port 80 \
$nginx "${nginx}"
buildah copy $nginx ../public /var/www/www.adyxax.org buildah copy "${nginx}" public /var/www/www.adyxax.org
buildah commit $nginx adyxax/www buildah commit "${nginx}" adyxax/www
buildah rm $nginx buildah rm "${nginx}"
ctr=$(buildah from scratch) ctr=$(buildah from scratch)
buildah copy $ctr ../search/search / buildah copy "${ctr}" search/search /
buildah config \ buildah config \
--author 'Julien Dessaux' \ --author 'Julien Dessaux' \
--cmd /search \ --cmd /search \
--port 8080 \ --port 8080 \
$ctr "${ctr}"
buildah commit $ctr adyxax/www-search buildah commit "${ctr}" adyxax/www-search
buildah rm $ctr buildah rm "${ctr}"