Skip to content

Commit 575fdfd

Browse files
committed
Update handler.py
1 parent 2c7359d commit 575fdfd

1 file changed

Lines changed: 22 additions & 15 deletions

File tree

src/handler.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,20 @@
88

99
import runpod
1010

11-
GITHUB_TOKEN = os.environ.get("GITHUB_PAT")
12-
ORG = os.environ.get("GITHUB_ORG", "runpod-workers")
13-
1411
RUNNER_NAME = os.environ.get("RUNPOD_POD_ID", "serverless-runpod-runner")
1512

16-
REQUIRED_ENV_VARS = ["GITHUB_PAT", "RUNPOD_POD_ID"]
17-
18-
for var in REQUIRED_ENV_VARS:
19-
if var not in os.environ:
20-
raise Exception(f"Missing {var} environment variable")
21-
2213

23-
def get_token():
14+
def get_token(pat, org):
2415
'''
2516
- Obtains registration token from GitHub
2617
'''
2718
headers = {
2819
"Accept": "application/vnd.github+json",
29-
"Authorization": f"Bearer {GITHUB_TOKEN}",
20+
"Authorization": f"Bearer {pat}",
3021
"X-GitHub-Api-Version": "2022-11-28"
3122
}
3223

33-
url = f"https://api.github.com/orgs/{ORG}/actions/runners/registration-token"
24+
url = f"https://api.github.com/orgs/{org}/actions/runners/registration-token"
3425
response = requests.post(url, headers=headers, timeout=10)
3526

3627
if response.status_code != 201:
@@ -60,8 +51,24 @@ def handler(event):
6051
- Starts a runner
6152
'''
6253

54+
# Get PAT
55+
if event.get('github_pat', None) is not None:
56+
pat = event.get('github_pat')
57+
event.pop('github_pat', None)
58+
elif os.environ.get("GITHUB_PAT", None) is not None:
59+
pat = os.environ.get("GITHUB_PAT")
60+
else:
61+
raise Exception("Missing GitHub Personal Access Token")
62+
63+
# Get ORG
64+
if event.get('github_org', None) is not None:
65+
org = event.get('github_org')
66+
event.pop('github_org', None)
67+
elif os.environ.get("GITHUB_ORG", None) is not None:
68+
org = os.environ.get("GITHUB_ORG")
69+
6370
# Configure runner
64-
config_cmd = f'./actions-runner/config.sh --url https://github.com/{ORG} --token {get_token()} --name {RUNNER_NAME} --work _work --labels runpod'
71+
config_cmd = f'./actions-runner/config.sh --url https://github.com/{org} --token {get_token(pat, org)} --name {RUNNER_NAME} --work _work --labels runpod'
6572
run_command(config_cmd)
6673

6774
# Remove unwanted environment variables
@@ -72,14 +79,14 @@ def handler(event):
7279
for var in unwated_env_vars:
7380
runner_env.pop(var, None)
7481

75-
runner_env['RUNPOD_JOB_INPUT'] = str(event['input'])
82+
runner_env['JOB_INPUT'] = str(event['input'])
7683

7784
# Start runner
7885
start_cmd = './actions-runner/run.sh --once'
7986
run_command(start_cmd, env=runner_env)
8087

8188
# Remove runner
82-
remove_cmd = f'./actions-runner/config.sh remove --token {get_token()}'
89+
remove_cmd = f'./actions-runner/config.sh remove --token {get_token(pat, org)}'
8390
run_command(remove_cmd)
8491

8592
return "Runner Exited"

0 commit comments

Comments
 (0)