diff --git a/.github/workflows/train_workflow.yml b/.github/workflows/train_workflow.yml index 0b4eb8b..e4ba87d 100644 --- a/.github/workflows/train_workflow.yml +++ b/.github/workflows/train_workflow.yml @@ -7,14 +7,24 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - name: Set up Python uses: actions/setup-python@v2 with: python-version: '3.x' - + - name: Install dependencies run: | - pip install -r ci-requirements.txt - + python -m pip install --upgrade pip + pip install -r requirements.txt + - name: Run training - run: python train.py \ No newline at end of file + run: | + python train.py > training.log 2>&1 + + - name: Upload logs + uses: actions/upload-artifact@v3 + if: always() # Upload logs whether the job succeeds or fails + with: + name: training-logs + path: training.log \ No newline at end of file diff --git a/bot.py b/bot.py index 2856d41..2c77ae9 100644 --- a/bot.py +++ b/bot.py @@ -2,6 +2,8 @@ from github import Github import os import time +from datetime import datetime, timezone +import requests # Load environment variables load_dotenv() @@ -14,36 +16,80 @@ def trigger_github_action(): repo = gh.get_repo(os.getenv('GITHUB_REPO')) try: + # Record the time before triggering + trigger_time = datetime.now(timezone.utc) + # Trigger the workflow workflow = repo.get_workflow("train_workflow.yml") success = workflow.create_dispatch("main") if success: - # Get the latest run ID + # Wait a moment for the run to be created + time.sleep(2) + + # Get runs created after our trigger time runs = list(workflow.get_runs()) - if runs: - return runs[0].id # Get the most recent run + for run in runs: + if run.created_at.replace(tzinfo=timezone.utc) > trigger_time: + return run.id return None except Exception as e: print(f"Error: {str(e)}") return None +def download_artifact(run_id): + """ + Downloads the training log artifact from the workflow run + """ + gh = Github(os.getenv('GITHUB_TOKEN')) + repo = gh.get_repo(os.getenv('GITHUB_REPO')) + + # Get the specific run + run = repo.get_workflow_run(run_id) + + # Get artifacts from the run + artifacts = run.get_artifacts() + + for artifact in artifacts: + if artifact.name == 'training-logs': + # Download the artifact + url = artifact.archive_download_url + headers = {'Authorization': f'token {os.getenv("GITHUB_TOKEN")}'} + response = requests.get(url, headers=headers) + + if response.status_code == 200: + with open('training.log.zip', 'wb') as f: + f.write(response.content) + + # Read the log file from the zip + import zipfile + with zipfile.ZipFile('training.log.zip') as z: + with z.open('training.log') as f: + logs = f.read().decode('utf-8') + + # Clean up the zip file + os.remove('training.log.zip') + return logs + + return "No training logs found in artifacts" + def check_workflow_status(run_id): """ Monitors the GitHub Action workflow status - Returns the conclusion and logs when complete """ gh = Github(os.getenv('GITHUB_TOKEN')) repo = gh.get_repo(os.getenv('GITHUB_REPO')) while True: - runs = repo.get_workflow_runs() - for run in runs: - if run.id == run_id: - if run.status == "completed": - return run.conclusion, run.html_url - print("Workflow still running... checking again in 30 seconds") + run = repo.get_workflow_run(run_id) + + if run.status == "completed": + logs = download_artifact(run_id) + return run.conclusion, logs, run.html_url + + print(f"Workflow still running... Status: {run.status}") + print(f"Live view: {run.html_url}") time.sleep(30) if __name__ == "__main__": @@ -54,9 +100,11 @@ def check_workflow_status(run_id): print("Monitoring progress...") # Monitor the workflow - status, url = check_workflow_status(run_id) + status, logs, url = check_workflow_status(run_id) print(f"\nWorkflow completed with status: {status}") - print(f"View the full run at: {url}") + print("\nTraining Logs:") + print(logs) + print(f"\nView the full run at: {url}") else: print("Failed to trigger GitHub Action. Please check your configuration.") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 5b8d5f2..d8bd49e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ - PyGithub aiohttp discord -python-dotenv \ No newline at end of file +python-dotenv +requests \ No newline at end of file