Compare commits

..

6 Commits

Author SHA1 Message Date
7707705858 make the refresh time longer 2024-08-08 14:36:51 -04:00
8580ffbe57 Better hugohost again 2024-08-08 14:15:19 -04:00
587243ab44 This works! 2024-08-08 14:11:36 -04:00
0e875c2073 Bump cat in there 2024-08-08 12:10:37 -04:00
55a22f10a5 Move things around 2024-08-08 12:09:27 -04:00
b7bc907bc3 Fix timezone 2024-08-08 11:34:22 -04:00
7 changed files with 199 additions and 11 deletions

26
Dockerfile Normal file
View File

@@ -0,0 +1,26 @@
# Use a common python image
FROM python:3.10-slim
# Install dependencies
RUN apt-get update && apt-get install -y \
wget \
git \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Download and install Hugo
RUN wget -O /tmp/hugo.deb "https://github.com/gohugoio/hugo/releases/download/v0.129.0/hugo_extended_0.129.0_linux-amd64.deb" \
&& dpkg -i /tmp/hugo.deb \
&& rm /tmp/hugo.deb
# Create a directory for the site
WORKDIR /app
# Copy the update script and Python script
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
COPY update-site.py /usr/local/bin/update-site.py
RUN chmod +x /usr/local/bin/entrypoint.sh
# Run the update script in a loop
CMD ["/bin/sh", "-c", "while true; do /usr/local/bin/entrypoint.sh; sleep 900; done"]

View File

@@ -0,0 +1,15 @@
+++
title = 'Experimenting With Project Stored Content'
date = 2024-08-08T11:31:01-04:00
draft = false
tags = ["hugo"]
+++
So if you're reading this, it works! This post is stored (primarily) on a different git project.
In this case, [HugoHost](https://git.kitsunehosting.net/Kenwood/HugoHost).
![cat](../images/canada_dry.png)
Pretty cool right?
Its a bit clunky but heres the [source](https://git.kitsunehosting.net/Kenwood/HugoHost/src/branch/main/update-site.py). Basically i'm just going to be running this build script on loop and letting it copy stuff in.

View File

Before

Width:  |  Height:  |  Size: 160 KiB

After

Width:  |  Height:  |  Size: 160 KiB

View File

@@ -1,11 +0,0 @@
+++
title = 'Experimenting With Project Stored Content'
date = 2024-08-08T11:31:01-0400
draft = false
tags = ["hugo"]
+++
So if you're reading this, it works! This post is stored (primarily) on a different git project.
In this case, [HugoHost](https://git.kitsunehosting.net/hugohost).
Pretty cool right?

23
docker-compose.yml Normal file
View File

@@ -0,0 +1,23 @@
services:
hugo:
build:
context: .
dockerfile: Dockerfile
environment:
- GIT_REPO_URL="https://git.kitsunehosting.net/Kenwood/kitsune-hosting.git"
volumes:
- site-source:/app/site
- site-public:/app/public
restart: unless-stopped
nginx:
image: nginx:alpine
volumes:
- site-public:/usr/share/nginx/html
ports:
- "80:80"
restart: unless-stopped
volumes:
site-source: # HugoHost
site-public: # HugoHost

6
entrypoint.sh Normal file
View File

@@ -0,0 +1,6 @@
#!/bin/sh
set -e
# Run the Python script
python3 /usr/local/bin/update-site.py

129
update-site.py Normal file
View File

@@ -0,0 +1,129 @@
import os
import time
import logging
import subprocess
from datetime import datetime
# Define file paths
GIT_REPO_PATH = "/app/site"
PUB_PATH = "/app/public"
FOOTER_FILE_PATH = "/app/site/themes/kitsune-theme/layouts/partials/footer.html"
def get_git_hash() -> str:
try:
result = subprocess.run(
["git", "rev-parse", "HEAD"],
cwd=GIT_REPO_PATH,
capture_output=True,
text=True,
)
except FileNotFoundError:
logging.warning(
f"No site found at {GIT_REPO_PATH}, assuming first-start. Will clone."
)
return None
r_hash = result.stdout.strip()
return r_hash if len(r_hash) > 3 else None
def git_fetch() -> bool:
# Fetch the latest changes
subprocess.run(["git", "fetch"], cwd=GIT_REPO_PATH)
# Check if the local branch is behind the remote branch
result = subprocess.run(
["git", "status", "-uno"],
cwd=GIT_REPO_PATH,
capture_output=True,
text=True,
)
# If output contains "Your branch is behind", there are updates
if "Your branch is behind" in result.stdout:
return True
return False
def update_repo():
# Make dir
logging.debug(f"Making dirs {GIT_REPO_PATH}")
if get_git_hash() is None and os.path.exists(GIT_REPO_PATH):
logging.warning("Clearing file path")
subprocess.run(["rm", "-rv", f"{GIT_REPO_PATH}/*"])
os.makedirs(GIT_REPO_PATH, exist_ok=True)
# Clone
url = os.environ.get("GIT_REPO_URL", "").strip('"')
if get_git_hash() is None:
logging.info(f"Cloning '{url}'.")
subprocess.run(["git", "clone", url, GIT_REPO_PATH, "--recurse-submodules"])
else:
# Check if fetch indicates a need for an update
if git_fetch():
logging.info(f"Fetch and pull '{url}'.")
subprocess.run(["git", "reset", "--hard"], cwd=GIT_REPO_PATH)
subprocess.run(["git", "pull"], cwd=GIT_REPO_PATH)
logging.info(f"Update submodules '{url}'.")
subprocess.run(
["git", "submodule", "update", "--init", "--recursive"],
cwd=GIT_REPO_PATH,
)
return True
return False
def build_pages():
logging.info("Cleaning files")
subprocess.run(["rm", "-rf", f"{PUB_PATH}/*"])
subprocess.run(["rm", "-rf", f"{GIT_REPO_PATH}/public/*"])
logging.info("Building pages")
subprocess.run(["hugo"], cwd=GIT_REPO_PATH)
logging.info("Copying files")
subprocess.run(["cp", "-r", f"{GIT_REPO_PATH}/public/.", PUB_PATH])
def update_footer(last_build_time, git_hash):
with open(FOOTER_FILE_PATH, "r") as file:
footer_content = file.read()
build_info = f"Last Build: {last_build_time}, Git Hash: {git_hash}"
new_footer_content = footer_content.replace("<!-- BUILD_METADATA -->", build_info)
with open(FOOTER_FILE_PATH, "w") as file:
file.write(new_footer_content)
def main():
# Time!
startime = time.time()
# Get the current Git hash
current_git_hash = get_git_hash()
# Update repo
logging.info("Updating repo")
if not update_repo():
logging.debug("Nothing to do")
return
# Update footer with build metadata
last_build_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
update_footer(last_build_time, current_git_hash)
# Rebuild site
logging.info("Building the site")
build_pages()
logging.info(f"Done in {time.time() - startime}!")
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()