diff --git a/.gitignore b/.gitignore index a283517..3aa1f8b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -config/ +quartz/ .env diff --git a/Dockerfile b/Dockerfile index eaa8056..6b26e10 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,36 +1,15 @@ -FROM node:alpine AS build - -RUN apk --no-cache add git && \ - git clone https://code.modernleft.org/gravityfargo/quartz.git && \ - cd quartz && git switch json-config && cd .. && \ - mkdir -p /usr/share/nginx/html && \ - mv quartz/* /usr/share/nginx/html && \ - cd /usr/share/nginx/html && \ - npm ci && npx quartz build && \ - rm -rf /usr/share/nginx/html/node_modules /usr/share/nginx/html/docs - FROM nginx:stable-alpine3.20 -COPY --from=build /usr/share/nginx/html /usr/share/nginx/html COPY default.conf.template /etc/nginx/templates/default.conf.template -COPY config.json /usr/share/nginx/html/default-config.json -WORKDIR /usr/share/nginx/html +RUN apk --no-cache add nodejs npm bash dumb-init git busybox-suid && \ + rm -rf /var/cache/apk/* -RUN apk --no-cache add nodejs npm bash dumb-init git && \ - rm -rf /var/cache/apk/* && \ - echo "*/5 * * * * cd /usr/share/nginx/html/content && git pull >/dev/null 2>&1 && cd /usr/share/nginx/html && npx quartz build >/dev/null 2>&1" > /etc/crontabs/root - -RUN mkdir /config && \ - ln -s /usr/share/nginx/html/content / && \ - chown -R nginx:nginx /usr/share/nginx/html && \ - chown -R nginx:nginx /config - -# REQUIRED +WORKDIR /quartz +ENV CONTENT_REPO="" ENV NGINX_PORT=80 ENV SERVER_NAME="quartz.zhao.xyz" ENV ENABLE_CRON="false" -ENV BUILD_SCHEDULE="*/5 * * * *" -ENV QUARTZ_CONFIG_PATH="/config/config.json" +ENV BUILD_SCHEDULE="*/5 * * * *" COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh diff --git a/default.conf.template b/default.conf.template index 3293961..6c8c7d8 100644 --- a/default.conf.template +++ b/default.conf.template @@ -1,7 +1,7 @@ server { listen ${NGINX_PORT}; server_name ${SERVER_NAME}; - root /usr/share/nginx/html/public; + root /quartz/src/public; index index.html; error_page 404 /404.html; diff --git a/docker-compose.yml b/docker-compose.yml index 666ae3d..2fe6f97 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,12 +10,7 @@ services: NGINX_PORT: 8080 SERVER_NAME: "localhost" ENABLE_CRON: "true" - BUILD_SCHEDULE: "*/10 * * * *" + BUILD_SCHEDULE: "*/1 * * * *" + CONTENT_REPO: "https://code.modernleft.org/gravityfargo/modernleft-docs.git" volumes: - - /home/nathan/Repositories/modernleft-docs:/content - - ./config:/config - - quartz-deps:/usr/share/nginx/html/node_modules - -volumes: - quartz-deps: - driver: local + - ./quartz:/quartz diff --git a/entrypoint.sh b/entrypoint.sh index e4ae9f6..433dd4e 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,45 +1,79 @@ #!/bin/bash set -e -verify_config_exist() { - if [[ ! -f "/config/config.json" ]]; then - echo "Config file not found, copying default config..." - cp /usr/share/nginx/html/config.json /config/config.json +setup_user() { + if ! getent group quartzgroup >/dev/null 2>&1; then + addgroup -g "$GROUP_ID" quartzgroup + fi + + if ! id -u quartz >/dev/null 2>&1; then + adduser -D -u "$USER_ID" -G quartzgroup quartz fi } setup_cron() { - echo "${BUILD_SCHEDULE} cd /usr/share/nginx/html/content && git pull >/dev/null 2>&1 && cd /usr/share/nginx/html && npx quartz build >/dev/null 2>&1" >/etc/crontabs/root - + # NEW_JOB="${BUILD_SCHEDULE} (cd /quartz/content && git pull) >/dev/null && echo && (cd /quartz/src && npx quartz build) >/dev/null 2>&1" + NEW_JOB="${BUILD_SCHEDULE} (cd /quartz/content && git restore . && git pull) >/dev/null && (cd /quartz/src && npx quartz build) >/dev/null && echo 'Content Updated.'" if [[ "$ENABLE_CRON" == "true" ]]; then - echo "Cron is enabled" - crond -b + echo "Cron is enabled..." + echo "Setting up cron job..." + echo "$NEW_JOB" >/etc/crontabs/root + /usr/sbin/crond -f & + else echo "Cron is disabled" fi } +verify_quartz() { + if [ ! -d "/quartz/src" ]; then + echo "Cloning Quartz..." + git clone https://github.com/jackyzha0/quartz.git /quartz/src + cd /quartz/src + rm -rf /quartz/src/content + ln -s /quartz/content /quartz/src + git config --global --add safe.directory /quartz/src + + echo "Installing Quartz dependencies..." + npm install >/dev/null + + echo "Building Quartz..." + npx quartz build >/dev/null + fi +} + +download_content() { + mkdir -p /quartz/content # Ensure directory exists + + if [ -n "${CONTENT_REPO}" ] && [ -z "$(ls -A /quartz/content 2>/dev/null)" ]; then + echo "Downloading content..." + git clone "${CONTENT_REPO}" /quartz/content + fi + git config --global --add safe.directory /quartz/content +} + main() { - verify_config_exist + echo "Setting up Quartz environment..." + setup_user setup_cron + verify_quartz + download_content - chown -R nginx:nginx /usr/share/nginx/html - chown -R "${USER_ID}":"${GROUP_ID}" /content - chown -R "${USER_ID}":"${GROUP_ID}" /config + rm -rf /quartz/src/content + ln -s /quartz/content /quartz/src + + echo "Setting correct permissions..." + chown -R "${USER_ID}":"${GROUP_ID}" /quartz + chmod -R 755 /quartz - # NGINX script to sub variables in the template echo "NGINX variable substitution..." - /docker-entrypoint.d/20-envsubst-on-templates.sh - - echo "Starting Quartz..." - echo "Installing dependencies..." - npm i >/dev/null + /docker-entrypoint.d/20-envsubst-on-templates.sh >/dev/null echo "Building Quartz..." - npx quartz build >/dev/null + su quartz -c "bash -c 'cd /quartz/src && npx quartz build >/dev/null'" echo "Starting Nginx..." - exec nginx -g "daemon off;" + exec nginx -g "daemon off; error_log warn;" } main