modified build process to clone the quartz repo to /quartz; this is copied into the build container, any files Ive customized are overlayed and then the site is built
This commit is contained in:
+74
-112
@@ -1,156 +1,118 @@
|
||||
#!/bin/bash
|
||||
|
||||
# --- Configuration ---
|
||||
# IMPORTANT: Set this to the ABSOLUTE path of your Quartz project directory on the host
|
||||
QUARTZ_PROJECT_DIR="/home/hoid/projects/portfolio/quartz"
|
||||
|
||||
# IMPORTANT: Set this to the ABSOLUTE path of the ACTUAL FOLDER in your Obsidian vault
|
||||
# that contains the Markdown files and assets for your Quartz site.
|
||||
# This is the SOURCE of your content.
|
||||
QUARTZ_PROJECT_DIR="/home/hoid/projects/digital_garden" # Path to your my-quartz-deployment repo
|
||||
OBSIDIAN_CONTENT_SOURCE="/home/hoid/docker/obsidian/vaults/weeslahw_coppermind/Digital_Garden"
|
||||
|
||||
# Directory name within your QUARTZ_PROJECT_DIR that Quartz uses for content
|
||||
# This is where the script will temporarily place the real content for the Docker build.
|
||||
# The Dockerfile's `COPY . .` will then pick this up.
|
||||
LOCAL_CONTENT_TARGET_DIR_NAME="content"
|
||||
|
||||
# Timestamp file to track the last successful build's content state
|
||||
# This file will be created in the QUARTZ_PROJECT_DIR
|
||||
LOCAL_CONTENT_TARGET_DIR_NAME="content" # Temp dir for Obsidian content within QUARTZ_PROJECT_DIR
|
||||
LAST_BUILD_TIMESTAMP_FILE=".last_build_content_timestamp"
|
||||
|
||||
# Log file for this script's output (recommended when run by cron)
|
||||
LOG_FILE="${QUARTZ_PROJECT_DIR}/update_quartz_docker.log"
|
||||
|
||||
# Quartz Framework Source Configuration
|
||||
QUARTZ_FRAMEWORK_DIR_NAME="quartz" # Dir name within QUARTZ_PROJECT_DIR
|
||||
QUARTZ_FRAMEWORK_REPO_URL="https://github.com/jackyzha0/quartz.git"
|
||||
QUARTZ_FRAMEWORK_BRANCH="v4" # Or "main", or a specific tag like "v4.2.5"
|
||||
|
||||
DOCKER_COMPOSE_CMD="/usr/local/bin/docker-compose"
|
||||
|
||||
# --- Script Logic ---
|
||||
|
||||
# Function for logging
|
||||
log_message() {
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] - $1" | tee -a "$LOG_FILE"
|
||||
}
|
||||
|
||||
cd "$QUARTZ_PROJECT_DIR" || {
|
||||
log_message "FATAL: Failed to cd to project directory: ${QUARTZ_PROJECT_DIR}"
|
||||
exit 1
|
||||
}
|
||||
log_message() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] - $1" | tee -a "$LOG_FILE"; }
|
||||
cd "$QUARTZ_PROJECT_DIR" || { log_message "FATAL: Failed to cd to project directory: ${QUARTZ_PROJECT_DIR}"; exit 1; }
|
||||
|
||||
log_message "--- Quartz Docker Update Script Started ---"
|
||||
|
||||
# --- Change Detection ---
|
||||
NEEDS_REBUILD=false
|
||||
if [ ! -d "$OBSIDIAN_CONTENT_SOURCE" ]; then
|
||||
log_message "ERROR: Obsidian content source directory does not exist: ${OBSIDIAN_CONTENT_SOURCE}"
|
||||
NEEDS_REBUILD=false # Or set to true if you want to try building with an empty content dir
|
||||
elif [ ! -f "$LAST_BUILD_TIMESTAMP_FILE" ]; then
|
||||
log_message "No last build timestamp found at ${LAST_BUILD_TIMESTAMP_FILE}. Triggering initial rebuild."
|
||||
NEEDS_REBUILD=true
|
||||
else
|
||||
# Efficiently check if any file within OBSIDIAN_CONTENT_SOURCE is newer than the timestamp file
|
||||
if find "$OBSIDIAN_CONTENT_SOURCE" -type f -newer "$LAST_BUILD_TIMESTAMP_FILE" -print -quit | grep -q .; then
|
||||
log_message "Changes detected in Obsidian content directory (${OBSIDIAN_CONTENT_SOURCE}) since last build. Triggering rebuild."
|
||||
NEEDS_REBUILD=true
|
||||
# --- Step 1: Ensure Quartz Framework Source is Present and Updated ---
|
||||
FRAMEWORK_FULL_PATH="${QUARTZ_PROJECT_DIR}/${QUARTZ_FRAMEWORK_DIR_NAME}"
|
||||
if [ -d "${FRAMEWORK_FULL_PATH}/.git" ]; then
|
||||
log_message "Attempting to update Quartz framework source in ./${QUARTZ_FRAMEWORK_DIR_NAME}..."
|
||||
(cd "$FRAMEWORK_FULL_PATH" && git checkout "$QUARTZ_FRAMEWORK_BRANCH" && git fetch origin && git reset --hard "origin/${QUARTZ_FRAMEWORK_BRANCH}")
|
||||
if [ $? -ne 0 ]; then
|
||||
log_message "WARNING: Failed to update Quartz framework source. Using existing version. Consider deleting ./${QUARTZ_FRAMEWORK_DIR_NAME} to force fresh clone."
|
||||
else
|
||||
log_message "No changes detected in Obsidian content directory since last build."
|
||||
log_message "Quartz framework source updated successfully to branch/tag ${QUARTZ_FRAMEWORK_BRANCH}."
|
||||
fi
|
||||
else
|
||||
log_message "Cloning Quartz framework (branch/tag: ${QUARTZ_FRAMEWORK_BRANCH}) into ./${QUARTZ_FRAMEWORK_DIR_NAME}..."
|
||||
rm -rf "$FRAMEWORK_FULL_PATH" # Remove if it's a broken/incomplete dir
|
||||
git clone --depth 1 --branch "$QUARTZ_FRAMEWORK_BRANCH" "$QUARTZ_FRAMEWORK_REPO_URL" "$FRAMEWORK_FULL_PATH"
|
||||
if [ $? -ne 0 ]; then
|
||||
log_message "ERROR: Failed to clone Quartz framework source from ${QUARTZ_FRAMEWORK_REPO_URL} (branch: ${QUARTZ_FRAMEWORK_BRANCH})."
|
||||
log_message "--- Script Finished (Error) ---"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# --- Step 2: Change Detection for Obsidian Content ---
|
||||
NEEDS_REBUILD=false
|
||||
# ... (Your existing change detection logic for OBSIDIAN_CONTENT_SOURCE vs LAST_BUILD_TIMESTAMP_FILE) ...
|
||||
# (Make sure this logic is sound and sets NEEDS_REBUILD=true correctly)
|
||||
if [ ! -d "$OBSIDIAN_CONTENT_SOURCE" ]; then
|
||||
log_message "ERROR: Obsidian content source directory does not exist: ${OBSIDIAN_CONTENT_SOURCE}"
|
||||
NEEDS_REBUILD=false
|
||||
elif [ ! -f "$LAST_BUILD_TIMESTAMP_FILE" ]; then
|
||||
log_message "No last build timestamp found. Triggering initial rebuild."
|
||||
NEEDS_REBUILD=true
|
||||
else
|
||||
if find "$OBSIDIAN_CONTENT_SOURCE" -type f -newer "$LAST_BUILD_TIMESTAMP_FILE" -print -quit | grep -q .; then
|
||||
log_message "Changes detected in Obsidian content. Triggering rebuild."
|
||||
NEEDS_REBUILD=true
|
||||
else
|
||||
log_message "No changes detected in Obsidian content since last build."
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
if [ "$NEEDS_REBUILD" = false ]; then
|
||||
log_message "Skipping Docker rebuild and deploy."
|
||||
log_message "Skipping Docker rebuild and deploy as no content changes were detected."
|
||||
log_message "--- Script Finished (No Changes) ---"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# --- Prepare Content for Docker Build ---
|
||||
# --- Step 3: Prepare Content Directory for Docker Build ---
|
||||
LOCAL_CONTENT_FULL_PATH="${QUARTZ_PROJECT_DIR}/${LOCAL_CONTENT_TARGET_DIR_NAME}"
|
||||
log_message "Preparing content directory for Docker build: ${LOCAL_CONTENT_FULL_PATH}"
|
||||
rm -rf "${LOCAL_CONTENT_FULL_PATH}" # Remove old temporary content
|
||||
mkdir "${LOCAL_CONTENT_FULL_PATH}" || { log_message "ERROR: Failed to create local content directory."; exit 1; }
|
||||
rsync -a --delete "${OBSIDIAN_CONTENT_SOURCE}/" "${LOCAL_CONTENT_FULL_PATH}/" || { log_message "ERROR: Failed to rsync Obsidian content."; exit 1; }
|
||||
log_message "Obsidian content prepared successfully."
|
||||
|
||||
log_message "Preparing content directory: ${LOCAL_CONTENT_FULL_PATH}"
|
||||
|
||||
# Remove existing local content directory/symlink in project dir (if any)
|
||||
if [ -L "${LOCAL_CONTENT_FULL_PATH}" ] || [ -d "${LOCAL_CONTENT_FULL_PATH}" ]; then
|
||||
log_message "Removing existing ./${LOCAL_CONTENT_TARGET_DIR_NAME}"
|
||||
rm -rf "${LOCAL_CONTENT_FULL_PATH}"
|
||||
fi
|
||||
|
||||
log_message "Creating new ./${LOCAL_CONTENT_TARGET_DIR_NAME} directory"
|
||||
mkdir "${LOCAL_CONTENT_FULL_PATH}"
|
||||
if [ $? -ne 0 ]; then
|
||||
log_message "ERROR: Failed to create local content directory: ${LOCAL_CONTENT_FULL_PATH}"
|
||||
log_message "--- Script Finished (Error) ---"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log_message "Copying content from ${OBSIDIAN_CONTENT_SOURCE} to ${LOCAL_CONTENT_FULL_PATH}"
|
||||
|
||||
# Using rsync for efficient copying; ensure it's installed (sudo apt install rsync)
|
||||
# The trailing slash on OBSIDIAN_CONTENT_SOURCE/ is important for rsync
|
||||
rsync -a --delete "${OBSIDIAN_CONTENT_SOURCE}/" "${LOCAL_CONTENT_FULL_PATH}/"
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
log_message "ERROR: Failed to copy content from Obsidian source to local content directory."
|
||||
log_message "--- Script Finished (Error) ---"
|
||||
exit 1
|
||||
fi
|
||||
log_message "Content prepared successfully for Docker build."
|
||||
|
||||
# --- Docker Build & Deploy using Docker Compose ---
|
||||
# Use absolute path for docker-compose for robustness in cron
|
||||
DOCKER_COMPOSE_CMD="/usr/local/bin/docker-compose"
|
||||
|
||||
log_message "Tearing down existing Docker Compose services (if any) for a clean deployment..."
|
||||
# Create a temporary file to capture output from 'docker-compose down'
|
||||
# --- Step 4: Docker Build & Deploy using Docker Compose ---
|
||||
# ... (Your existing docker-compose down and docker-compose up sections using $DOCKER_COMPOSE_CMD) ...
|
||||
# (The Dockerfile will now use the ./quartz_framework_source/ and ./content/ directories)
|
||||
log_message "Tearing down existing Docker Compose services (if any)..."
|
||||
COMPOSE_DOWN_OUTPUT_FILE="${QUARTZ_PROJECT_DIR}/compose_down_output.tmp"
|
||||
|
||||
# Execute 'docker-compose down' and capture all its output
|
||||
$DOCKER_COMPOSE_CMD down --remove-orphans --volumes > "$COMPOSE_DOWN_OUTPUT_FILE" 2>&1
|
||||
DOWN_EXIT_CODE=$?
|
||||
|
||||
if [ $DOWN_EXIT_CODE -ne 0 ]; then
|
||||
log_message "WARNING: '$DOCKER_COMPOSE_CMD down' failed with exit code $DOWN_EXIT_CODE. Output from command:"
|
||||
# Append the captured output from docker-compose down to the main log
|
||||
# Ensure there's a newline before catting, in case log_message doesn't add one after its prefix
|
||||
echo "" >> "$LOG_FILE"
|
||||
cat "$COMPOSE_DOWN_OUTPUT_FILE" >> "$LOG_FILE"
|
||||
echo "" >> "$LOG_FILE" # Add a newline after the output
|
||||
log_message "(End of '$DOCKER_COMPOSE_CMD down' output for this attempt)"
|
||||
log_message "Attempting to proceed with 'up' command despite 'down' issues..." # This was your script's original logic
|
||||
log_message "WARNING: '$DOCKER_COMPOSE_CMD down' failed with exit code $DOWN_EXIT_CODE. Output:"
|
||||
echo "" >> "$LOG_FILE"; cat "$COMPOSE_DOWN_OUTPUT_FILE" >> "$LOG_FILE"; echo "" >> "$LOG_FILE"
|
||||
log_message "Attempting to proceed..."
|
||||
else
|
||||
log_message "'$DOCKER_COMPOSE_CMD down' executed successfully."
|
||||
fi
|
||||
rm -f "$COMPOSE_DOWN_OUTPUT_FILE" # Clean up the temporary file
|
||||
rm -f "$COMPOSE_DOWN_OUTPUT_FILE"
|
||||
|
||||
log_message "Building and deploying with Docker Compose..."
|
||||
# Create a temporary file to capture output from 'docker-compose up'
|
||||
COMPOSE_UP_OUTPUT_FILE="${QUARTZ_PROJECT_DIR}/compose_up_output.tmp"
|
||||
|
||||
$DOCKER_COMPOSE_CMD up -d --build --remove-orphans > "$COMPOSE_UP_OUTPUT_FILE" 2>&1
|
||||
UP_EXIT_CODE=$?
|
||||
|
||||
if [ $UP_EXIT_CODE -ne 0 ]; then
|
||||
log_message "ERROR: '$DOCKER_COMPOSE_CMD up -d --build --remove-orphans' failed with exit code $UP_EXIT_CODE! Output from command:"
|
||||
echo "" >> "$LOG_FILE"
|
||||
cat "$COMPOSE_UP_OUTPUT_FILE" >> "$LOG_FILE"
|
||||
echo "" >> "$LOG_FILE"
|
||||
log_message "(End of '$DOCKER_COMPOSE_CMD up' output for this attempt)"
|
||||
log_message "ERROR: '$DOCKER_COMPOSE_CMD up -d --build --remove-orphans' failed with exit code $UP_EXIT_CODE! Output:"
|
||||
echo "" >> "$LOG_FILE"; cat "$COMPOSE_UP_OUTPUT_FILE" >> "$LOG_FILE"; echo "" >> "$LOG_FILE"
|
||||
log_message "--- Script Finished (Error) ---"
|
||||
rm -f "$COMPOSE_UP_OUTPUT_FILE" # Clean up
|
||||
exit 1
|
||||
rm -f "$COMPOSE_UP_OUTPUT_FILE"; exit 1
|
||||
else
|
||||
log_message "'$DOCKER_COMPOSE_CMD up -d --build --remove-orphans' executed successfully."
|
||||
fi
|
||||
rm -f "$COMPOSE_UP_OUTPUT_FILE" # Clean up
|
||||
rm -f "$COMPOSE_UP_OUTPUT_FILE"
|
||||
|
||||
# Update the timestamp file on successful deployment
|
||||
|
||||
# --- Step 5: Update Timestamp & Cleanup ---
|
||||
touch "$LAST_BUILD_TIMESTAMP_FILE"
|
||||
log_message "Updated last build timestamp: ${LAST_BUILD_TIMESTAMP_FILE}"
|
||||
log_message "Updated last build timestamp."
|
||||
|
||||
# Optional: Clean up by removing the copied content and restoring symlink if needed for local dev
|
||||
# This is useful if your local Quartz dev workflow relies on the symlink.
|
||||
# If the cron job is the ONLY way this is built, cleanup might not be strictly necessary,
|
||||
# but it keeps the project dir clean of copied data between runs.
|
||||
log_message "Cleaning up temporary local content directory..."
|
||||
log_message "Cleaning up temporary local content directory ./${LOCAL_CONTENT_TARGET_DIR_NAME}..."
|
||||
rm -rf "${LOCAL_CONTENT_FULL_PATH}"
|
||||
# Note: We are NOT cleaning up ./${QUARTZ_FRAMEWORK_DIR_NAME} here as we want to keep it for the next run to `git pull`.
|
||||
# It's in .gitignore of your `my-quartz-deployment` repo.
|
||||
|
||||
# If you use a symlink for local dev, uncomment and adjust to restore it:
|
||||
#ln -s "${OBSIDIAN_CONTENT_SOURCE}" "${LOCAL_CONTENT_FULL_PATH}"
|
||||
log_message "Local content directory cleaned up."
|
||||
|
||||
log_message "--- Script Finished Successfully ---"
|
||||
log_message "--- Script Finished Successfully ---"
|
||||
Reference in New Issue
Block a user