From e0ad92c9bea922b21a8b02752273fe0c7730a7c9 Mon Sep 17 00:00:00 2001 From: Winston Smith Date: Tue, 17 Oct 2023 20:06:36 +0200 Subject: [PATCH] initial commit --- .env.example | 4 +++ .gitignore | 2 ++ Dockerfile | 16 ++++++++++ data/entrypoint.sh | 77 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 15 +++++++++ 5 files changed, 114 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100755 data/entrypoint.sh create mode 100644 docker-compose.yml diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..89b472c --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +BLOG_NAME= +LIVERELOAD_PORT= +HOST_URL= +WWW_PORT= diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1e8f8b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +jekyll/** diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..05beab6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM ruby:3.2.2 + +ARG BLOG_NAME +RUN useradd -ms /bin/bash --uid 1000 jekyll +RUN mkdir -p -m 0755 /app/jekyll +RUN mkdir -p -m 0755 /opt/jekyll +VOLUME $PWD/data:/opt/jekyll +RUN chmod +x /opt/jekyll/ && \ + chown -R 1000:1000 /opt/jekyll && \ + chown -R 1000:1000 /app + +USER 1000 + +WORKDIR /app/jekyll + +ENTRYPOINT ["/opt/jekyll/entrypoint.sh"] diff --git a/data/entrypoint.sh b/data/entrypoint.sh new file mode 100755 index 0000000..fe00fc8 --- /dev/null +++ b/data/entrypoint.sh @@ -0,0 +1,77 @@ +#!/bin/bash +source /opt/jekyll/.env +export AUTHOR_EMAIL=$AUTHOR_EMAIL +export BLOG_NAME=$BLOG_NAME +export BASE_URL=$BASE_URL +export URL=$URL +export GITHUB_USERNAME=$GITHUB_USERNAME +export LIVERELOAD_PORT=$LIVERELOAD_PORT +export HOST_URL=$HOST_URL +export SITE_TITLE=$SITE_TITLE +export THEME=$THEME +export TWITTER_USERNAME=$TWITTER_USERNAME +export WWW_PORT=$WWW_PORT +export GEM_HOME="/usr/local/bundle" +export PATH="$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH" +export SITE_PATH="/app/jekyll/$BLOG_NAME" + +update_config() { + for ENV_VAR in 'AUTHOR_EMAIL' 'BASE_URL' 'GITHUB_USERNAME' \ + 'SITE_TITLE' 'THEME' 'TWITTER_USERNAME' 'URL'; do + case $ENV_VAR in + "AUTHOR_EMAIL") param="email" ;; + "BASE_URL") param="baseurl" ;; + "GITHUB_USERNAME") param="github_username" ;; + "SITE_TITLE") param="title" ;; + "THEME") param="theme" ;; + "TWITTER_USERNAME") param="twitter_username" ;; + "URL") param="url" ;; + esac + initial_config=$(grep -E "^${param}: " ${SITE_PATH}/_config.yml) + [[ "$initial_config" != "$param: ${!ENV_VAR}" && ! -z ${!ENV_VAR} ]] && { + echo "Config change detected on $param: updating _config.yml" + echo "New $param: ${!ENV_VAR}" + [[ $ENV_VAR == "BASE_URL" || $ENV_VAR == "URL" ]] && { + sed -i "s/\(${param}: \).*/\1\"${!ENV_VAR}\"/g" ${SITE_PATH}/_config.yml + } || { + sed -i "s/\(${param}: \).*/\1${!ENV_VAR}/g" ${SITE_PATH}/_config.yml + } + } || { + echo "No change or empty variable for $param, continuing..." + } + done +} + +update_description() { + initial_description=$(sed -n '/^description/,/^baseurl/p' ${SITE_PATH}/_config.yml | sed '/^description/d' | + sed '/^baseurl: ""/d' | sed 's/^[[:space:]]*//' | sed '/^description:/,/^baseurl:/ { /^description:/n; /^baseurl:/! { /^$/d; } }') + current_description=$(sed 's/^[[:space:]]*//' /opt/jekyll/${BLOG_NAME}_description.txt) + [[ "$initial_description" != "$current_description" && ! -z $current_description ]] && { + echo "Config change detected on site description: updating _config.yml" + sed -i "/^description/,/^baseurl:/!b;//!d;/^description/r /opt/jekyll/${BLOG_NAME}_description.txt" ${SITE_PATH}/_config.yml + sed -i '/^description:/,/^baseurl:/ { /^description:/n; /^baseurl:/! s/^[^[:space:]]/ &/; }' ${SITE_PATH}/_config.yml + } || { + echo "No change or empty variable for site $BLOG_NAME description, continuing..." + sed -i '/^description:/,/^baseurl:/ { /^description:/n; /^baseurl:/! s/^[^[:space:]]/ &/; }' ${SITE_PATH}/_config.yml + sed '/^description:/,/^baseurl:/ { /^description:/n; /^baseurl:/! { /^$/d; } }' + } +} + +cd /app/jekyll && + gem install jekyll jekyll-docs jekyll-compose bundler webrick + +[[ -f ${SITE_PATH}/_config.yml ]] && + { + cd $SITE_PATH + bundle install + update_config + update_description + bundle exec jekyll serve --host $HOST_URL + } || { + jekyll new $SITE_PATH + cd $SITE_PATH + bundle install + update_config + update_description + bundle exec jekyll serve --host $HOST_URL +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ca83535 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,15 @@ +version: "3" +services: + jekyll: + container_name: jekyll + build: + context: ${PWD} + args: + BLOG_NAME: ${BLOG_NAME} + env_file: ${PWD}/data/.env + ports: + - ${LIVERELOAD_PORT}:10000 + - ${WWW_PORT}:4000 + volumes: + - ${PWD}/jekyll:/app/jekyll + - ${PWD}/data:/opt/jekyll