From 389288b34203e5224ba7f5e50f041ceb4e4db92c Mon Sep 17 00:00:00 2001 From: Ben Horowitz Date: Sat, 28 Dec 2024 10:14:25 -0800 Subject: [PATCH 1/2] Add hello world workflow to help diagnose GitHub token issues --- .github/workflows/hello_world_workflow.yml | 12 ++++++ .../run_hello_world_workflow.py | 37 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 .github/workflows/hello_world_workflow.yml create mode 100644 src/discord-cluster-manager/run_hello_world_workflow.py diff --git a/.github/workflows/hello_world_workflow.yml b/.github/workflows/hello_world_workflow.yml new file mode 100644 index 00000000..acb8b012 --- /dev/null +++ b/.github/workflows/hello_world_workflow.yml @@ -0,0 +1,12 @@ +name: Hello World Job + +on: + workflow_dispatch: + +jobs: + say-hello: + runs-on: ubuntu-latest + + steps: + - name: Say Hello + run: echo "Hello, world!" diff --git a/src/discord-cluster-manager/run_hello_world_workflow.py b/src/discord-cluster-manager/run_hello_world_workflow.py new file mode 100644 index 00000000..4ed2cad7 --- /dev/null +++ b/src/discord-cluster-manager/run_hello_world_workflow.py @@ -0,0 +1,37 @@ +from consts import GITHUB_REPO, GITHUB_TOKEN +from github import Github +from github.GithubException import GithubException +from utils import get_github_branch_name, setup_logging + +logger = setup_logging() + + +def trigger_workflow(): + try: + gh = Github(GITHUB_TOKEN) + repo = gh.get_repo(GITHUB_REPO) + + workflows = repo.get_workflows() + for w in workflows: + logger.info(f"Found workflow: {w.name}") + + workflow = repo.get_workflow("Hello World Job") + + if not workflow: + raise ValueError("Could not find hello world workflow") + + branch = get_github_branch_name() + workflow.create_dispatch(branch) + logger.info("Successfully triggered hello world workflow") + + except GithubException as e: + logger.error(f"GitHub API error: {e.status} - {e.data.get('message', 'Unknown error')}") + except Exception as e: + logger.error(f"Error: {str(e)}") + finally: + if "gh" in locals(): + gh.close() + + +if __name__ == "__main__": + trigger_workflow() From 79b7bea3b299f9eb525e98b5e1b770028258538c Mon Sep 17 00:00:00 2001 From: Ben Horowitz Date: Sat, 28 Dec 2024 15:29:19 -0800 Subject: [PATCH 2/2] Move workflow script to scripts directory --- scripts/run_hello_world_workflow.py | 42 +++++++++++++++++++ .../run_hello_world_workflow.py | 37 ---------------- 2 files changed, 42 insertions(+), 37 deletions(-) create mode 100644 scripts/run_hello_world_workflow.py delete mode 100644 src/discord-cluster-manager/run_hello_world_workflow.py diff --git a/scripts/run_hello_world_workflow.py b/scripts/run_hello_world_workflow.py new file mode 100644 index 00000000..e3b83a84 --- /dev/null +++ b/scripts/run_hello_world_workflow.py @@ -0,0 +1,42 @@ +import os + +from dotenv import load_dotenv +from github import Github +from github.GithubException import GithubException + +load_dotenv() + +GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") +GITHUB_REPO = os.getenv("GITHUB_REPO") + +if not GITHUB_TOKEN: + print("Environment variable GITHUB_TOKEN is not defined.") + exit(1) + +if not GITHUB_REPO: + print("Environment variable GITHUB_REPO is not defined.") + exit(1) + +try: + gh = Github(GITHUB_TOKEN) + repo = gh.get_repo(GITHUB_REPO) + + workflows = repo.get_workflows() + for w in workflows: + print(f"Found workflow: {w.name}") + + workflow = repo.get_workflow("hello_world_workflow.yml") + + if not workflow: + raise ValueError("Could not find hello world workflow.") + + workflow.create_dispatch('main') + print("Successfully triggered hello world workflow") + +except GithubException as e: + print(f"GitHub API error: {e.status} - {e.data.get('message', 'Unknown error')}") +except Exception as e: + print(f"Error: {str(e)}") +finally: + if "gh" in locals(): + gh.close() diff --git a/src/discord-cluster-manager/run_hello_world_workflow.py b/src/discord-cluster-manager/run_hello_world_workflow.py deleted file mode 100644 index 4ed2cad7..00000000 --- a/src/discord-cluster-manager/run_hello_world_workflow.py +++ /dev/null @@ -1,37 +0,0 @@ -from consts import GITHUB_REPO, GITHUB_TOKEN -from github import Github -from github.GithubException import GithubException -from utils import get_github_branch_name, setup_logging - -logger = setup_logging() - - -def trigger_workflow(): - try: - gh = Github(GITHUB_TOKEN) - repo = gh.get_repo(GITHUB_REPO) - - workflows = repo.get_workflows() - for w in workflows: - logger.info(f"Found workflow: {w.name}") - - workflow = repo.get_workflow("Hello World Job") - - if not workflow: - raise ValueError("Could not find hello world workflow") - - branch = get_github_branch_name() - workflow.create_dispatch(branch) - logger.info("Successfully triggered hello world workflow") - - except GithubException as e: - logger.error(f"GitHub API error: {e.status} - {e.data.get('message', 'Unknown error')}") - except Exception as e: - logger.error(f"Error: {str(e)}") - finally: - if "gh" in locals(): - gh.close() - - -if __name__ == "__main__": - trigger_workflow()