Fix workflow py script

This commit is contained in:
dvirlabs 2025-06-29 07:20:53 +03:00
parent 51c6482065
commit c167c14e53

View File

@ -10,6 +10,8 @@ REPOS = {
GITHUB_REPO = f"https://{os.getenv('GITHUB_TOKEN')}@github.com/dvirh/lab-monitor.git" GITHUB_REPO = f"https://{os.getenv('GITHUB_TOKEN')}@github.com/dvirh/lab-monitor.git"
BASE_DIR = "./repos" BASE_DIR = "./repos"
WORKFLOW_FILE = "./repos/lab-monitor/.github/workflows/monitor.yml" WORKFLOW_FILE = "./repos/lab-monitor/.github/workflows/monitor.yml"
REPO_PATH = os.path.join(BASE_DIR, "lab-monitor")
def clone_repos(): def clone_repos():
os.makedirs(BASE_DIR, exist_ok=True) os.makedirs(BASE_DIR, exist_ok=True)
@ -20,6 +22,7 @@ def clone_repos():
else: else:
subprocess.run(["git", "clone", url, repo_path]) subprocess.run(["git", "clone", url, repo_path])
def extract_urls(): def extract_urls():
urls = [] urls = []
for repo in REPOS: for repo in REPOS:
@ -57,6 +60,7 @@ def severity_to_priority(sev):
"info": 0 "info": 0
}.get(sev.lower(), 0) }.get(sev.lower(), 0)
def generate_workflow(urls): def generate_workflow(urls):
os.makedirs(os.path.dirname(WORKFLOW_FILE), exist_ok=True) os.makedirs(os.path.dirname(WORKFLOW_FILE), exist_ok=True)
with open(WORKFLOW_FILE, "w") as f: with open(WORKFLOW_FILE, "w") as f:
@ -82,7 +86,7 @@ jobs:
STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$URL") STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$URL")
TIME=$(date "+%Y-%m-%d %H:%M:%S") TIME=$(date "+%Y-%m-%d %H:%M:%S")
if [[ "$STATUS" == "$CODE" ]]; then if [[ "$STATUS" != "$CODE" ]]; then
curl -s \\ curl -s \\
--form-string "token=${{ secrets.PUSHOVER_TOKEN }}" \\ --form-string "token=${{ secrets.PUSHOVER_TOKEN }}" \\
--form-string "user=${{ secrets.PUSHOVER_USER }}" \\ --form-string "user=${{ secrets.PUSHOVER_USER }}" \\
@ -100,22 +104,31 @@ jobs:
for item in urls: for item in urls:
f.write(f' check_url "{item["url"]}" "{item["name"]}" "{item["code"]}" "{item["message"]}" "{severity_to_priority(item["severity"])}"\n') f.write(f' check_url "{item["url"]}" "{item["name"]}" "{item["code"]}" "{item["message"]}" "{severity_to_priority(item["severity"])}"\n')
def push_workflow(): def push_workflow():
repo_path = os.path.join(BASE_DIR, "lab-monitor") # clone if needed
if not os.path.exists(repo_path): if not os.path.exists(REPO_PATH):
subprocess.run(["git", "clone", GITHUB_REPO, repo_path]) print("📥 Cloning lab-monitor...")
subprocess.run(["git", "-C", repo_path, "pull", "origin", "main"]) result = subprocess.run(["git", "clone", GITHUB_REPO, REPO_PATH])
if result.returncode != 0:
raise Exception("❌ Failed to clone lab-monitor repo")
# checkout main if needed
subprocess.run(["git", "-C", REPO_PATH, "fetch"], check=True)
subprocess.run(["git", "-C", REPO_PATH, "checkout", "-B", "main", "origin/main"], check=False)
# Set Git identity # Set Git identity
subprocess.run(["git", "-C", repo_path, "config", "user.name", "lab-monitor-bot"]) subprocess.run(["git", "-C", REPO_PATH, "config", "user.name", "lab-monitor-bot"])
subprocess.run(["git", "-C", repo_path, "config", "user.email", "bot@dvirlabs.com"]) subprocess.run(["git", "-C", REPO_PATH, "config", "user.email", "bot@dvirlabs.com"])
# Add + Commit # Add + Commit
subprocess.run(["git", "-C", repo_path, "add", ".github/workflows/monitor.yml"]) subprocess.run(["git", "-C", REPO_PATH, "add", ".github/workflows/monitor.yml"])
subprocess.run(["git", "-C", repo_path, "commit", "-m", "update monitor.yml from monitoring.yaml"], check=False) subprocess.run(["git", "-C", REPO_PATH, "commit", "-m", "update monitor.yml from monitoring.yaml"], check=False)
# Push with upstream set to main # Push with upstream set to main
subprocess.run(["git", "-C", repo_path, "push", "--set-upstream", "origin", "main"]) result = subprocess.run(["git", "-C", REPO_PATH, "push", "--set-upstream", "origin", "main"])
if result.returncode != 0:
raise Exception("❌ Failed to push monitor.yml to origin/main")
if __name__ == "__main__": if __name__ == "__main__":
@ -123,4 +136,3 @@ if __name__ == "__main__":
urls = extract_urls() urls = extract_urls()
generate_workflow(urls) generate_workflow(urls)
push_workflow() push_workflow()