Skip to content

Commit

Permalink
start using github artifacts instead
Browse files Browse the repository at this point in the history
  • Loading branch information
msaroufim committed Nov 5, 2024
1 parent a1f9fe9 commit 88148ce
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 18 deletions.
18 changes: 14 additions & 4 deletions .github/workflows/train_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
72 changes: 60 additions & 12 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from github import Github
import os
import time
from datetime import datetime, timezone
import requests

# Load environment variables
load_dotenv()
Expand All @@ -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__":
Expand All @@ -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.")
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

PyGithub
aiohttp
discord
python-dotenv
python-dotenv
requests

0 comments on commit 88148ce

Please sign in to comment.