From 7d5f5bba64f89cb23f1182229d0ad8aa09fe7a19 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Fri, 7 Mar 2025 12:16:34 +0100 Subject: [PATCH] listener redeploy --- deploy/redeploy_listener.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/deploy/redeploy_listener.py b/deploy/redeploy_listener.py index aba15b4..82735ed 100755 --- a/deploy/redeploy_listener.py +++ b/deploy/redeploy_listener.py @@ -1,26 +1,43 @@ -#!/bin/python3 +#!/bin/python3 -u import time import os +import subprocess +import sys + +INTERVAL_DELAY = 60 +ERROR_DELAY = 60 def main(): + dry_run = "--dry-run" in sys.argv + deployed_commit_hash = "" while True: + print("fetching newest commit...") + newest_commit_hash = os.popen("curl -s https://api.github.com/repos/Mercantec-GHC/h4-projekt-gruppe-0-sm/commits/main | jq -r .sha").read() if not newest_commit_hash: - print("error fetching commit hash") - time.sleep(60) + print("error: could not fetch commit hash", file=sys.stderr) + print(f"trying again in {ERROR_DELAY} seconds...", file=sys.stderr) + time.sleep(ERROR_DELAY) continue if newest_commit_hash != deployed_commit_hash: deployed_commit_hash = newest_commit_hash - print("should redeploy") + print("new commit found. redeploying...") + + if not dry_run: + rcode = subprocess.call(["sh", "./deploy/redeploy.sh"]) + if rcode == 0: + print("redeployed successfully") + else: + print("error: could not redeploy", file=sys.stderr) else: - print("should not redeploy") + print("no new commits") - - time.sleep(60) + print(f"sleeping for {INTERVAL_DELAY} seconds...") + time.sleep(INTERVAL_DELAY) if __name__ == "__main__": main()