Better hugohost again
This commit is contained in:
parent
587243ab44
commit
8580ffbe57
|
@ -11,3 +11,5 @@ In this case, [HugoHost](https://git.kitsunehosting.net/Kenwood/HugoHost).
|
||||||

|

|
||||||
|
|
||||||
Pretty cool right?
|
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.
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import shutil
|
|
||||||
import logging
|
import logging
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
@ -10,7 +9,6 @@ from datetime import datetime
|
||||||
GIT_REPO_PATH = "/app/site"
|
GIT_REPO_PATH = "/app/site"
|
||||||
PUB_PATH = "/app/public"
|
PUB_PATH = "/app/public"
|
||||||
FOOTER_FILE_PATH = "/app/site/themes/kitsune-theme/layouts/partials/footer.html"
|
FOOTER_FILE_PATH = "/app/site/themes/kitsune-theme/layouts/partials/footer.html"
|
||||||
HASH_FILE_PATH = "/app/current-git-hash.txt"
|
|
||||||
|
|
||||||
|
|
||||||
def get_git_hash() -> str:
|
def get_git_hash() -> str:
|
||||||
|
@ -28,47 +26,66 @@ def get_git_hash() -> str:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
r_hash = result.stdout.strip()
|
r_hash = result.stdout.strip()
|
||||||
|
|
||||||
return r_hash if len(r_hash) > 3 else None
|
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():
|
def update_repo():
|
||||||
# Make dir
|
# Make dir
|
||||||
logging.info(f"Making dirs {GIT_REPO_PATH}")
|
logging.debug(f"Making dirs {GIT_REPO_PATH}")
|
||||||
if get_git_hash() == None and os.path.exists(GIT_REPO_PATH):
|
if get_git_hash() is None and os.path.exists(GIT_REPO_PATH):
|
||||||
logging.warning("Clearing file path")
|
logging.warning("Clearing file path")
|
||||||
subprocess.run(["rm", "-rv", f"{GIT_REPO_PATH}/*"])
|
subprocess.run(["rm", "-rv", f"{GIT_REPO_PATH}/*"])
|
||||||
os.makedirs(GIT_REPO_PATH, exist_ok=True)
|
os.makedirs(GIT_REPO_PATH, exist_ok=True)
|
||||||
|
|
||||||
# Clone
|
# Clone
|
||||||
url = os.environ.get("GIT_REPO_URL", "").strip('"')
|
url = os.environ.get("GIT_REPO_URL", "").strip('"')
|
||||||
if get_git_hash() == None:
|
if get_git_hash() is None:
|
||||||
logging.info(f"Cloning '{url}'.")
|
logging.info(f"Cloning '{url}'.")
|
||||||
subprocess.run(["git", "clone", url, GIT_REPO_PATH, "--recurse-submodules"])
|
subprocess.run(["git", "clone", url, GIT_REPO_PATH, "--recurse-submodules"])
|
||||||
else:
|
else:
|
||||||
logging.info(f"Fetch '{url}'.")
|
# Check if fetch indicates a need for an update
|
||||||
subprocess.run(["git", "fetch"], cwd=GIT_REPO_PATH)
|
if git_fetch():
|
||||||
logging.info(f"Pull '{url}'.")
|
logging.info(f"Fetch and pull '{url}'.")
|
||||||
subprocess.run(["git", "fetch"], cwd=GIT_REPO_PATH)
|
subprocess.run(["git", "reset", "--hard"], cwd=GIT_REPO_PATH)
|
||||||
|
subprocess.run(["git", "pull"], cwd=GIT_REPO_PATH)
|
||||||
logging.info(f"Update submodules '{url}'.")
|
logging.info(f"Update submodules '{url}'.")
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
["git", "submodule", "update", "--init", "--recursive"], cwd=GIT_REPO_PATH
|
["git", "submodule", "update", "--init", "--recursive"],
|
||||||
|
cwd=GIT_REPO_PATH,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Return new hash
|
return True
|
||||||
return get_git_hash()
|
return False
|
||||||
|
|
||||||
|
|
||||||
def build_pages():
|
def build_pages():
|
||||||
logging.info("Cleaning files")
|
logging.info("Cleaning files")
|
||||||
subprocess.run(["rm", "-rv", f"{PUB_PATH}/."])
|
subprocess.run(["rm", "-rf", f"{PUB_PATH}/*"])
|
||||||
subprocess.run(["rm", "-rv", f"{GIT_REPO_PATH}/public/."])
|
subprocess.run(["rm", "-rf", f"{GIT_REPO_PATH}/public/*"])
|
||||||
|
|
||||||
logging.info("Building pages")
|
logging.info("Building pages")
|
||||||
subprocess.run(["hugo"], cwd=GIT_REPO_PATH)
|
subprocess.run(["hugo"], cwd=GIT_REPO_PATH)
|
||||||
|
|
||||||
logging.info("Copying files")
|
logging.info("Copying files")
|
||||||
subprocess.run(["cp", "-rv", f"{GIT_REPO_PATH}/public/.", PUB_PATH])
|
subprocess.run(["cp", "-r", f"{GIT_REPO_PATH}/public/.", PUB_PATH])
|
||||||
|
|
||||||
|
|
||||||
def update_footer(last_build_time, git_hash):
|
def update_footer(last_build_time, git_hash):
|
||||||
|
@ -88,24 +105,12 @@ def main():
|
||||||
|
|
||||||
# Get the current Git hash
|
# Get the current Git hash
|
||||||
current_git_hash = get_git_hash()
|
current_git_hash = get_git_hash()
|
||||||
last_git_hash = ""
|
|
||||||
|
|
||||||
# Check if the Git hash file exists
|
|
||||||
if not os.path.exists(HASH_FILE_PATH):
|
|
||||||
logging.warning(f"Hash at {HASH_FILE_PATH} doesn't exist!")
|
|
||||||
else:
|
|
||||||
with open(HASH_FILE_PATH, "r") as file:
|
|
||||||
last_git_hash = file.read().strip()
|
|
||||||
|
|
||||||
if current_git_hash == last_git_hash and current_git_hash != None:
|
|
||||||
logging.info(
|
|
||||||
f"No new changes detected. Skipping rebuild for {current_git_hash}."
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Update repo
|
# Update repo
|
||||||
logging.info("Updating repo")
|
logging.info("Updating repo")
|
||||||
current_git_hash = update_repo()
|
if not update_repo():
|
||||||
|
logging.debug("Nothing to do")
|
||||||
|
return
|
||||||
|
|
||||||
# Update footer with build metadata
|
# Update footer with build metadata
|
||||||
last_build_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
last_build_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
@ -115,10 +120,6 @@ def main():
|
||||||
logging.info("Building the site")
|
logging.info("Building the site")
|
||||||
build_pages()
|
build_pages()
|
||||||
|
|
||||||
# Store the new Git hash
|
|
||||||
with open(HASH_FILE_PATH, "w") as file:
|
|
||||||
file.write(current_git_hash)
|
|
||||||
|
|
||||||
logging.info(f"Done in {time.time() - startime}!")
|
logging.info(f"Done in {time.time() - startime}!")
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue