Skip to content

Instantly share code, notes, and snippets.

@fabidick22
Last active May 27, 2022 18:49
Show Gist options
  • Save fabidick22/96f50b072b7b2ea1e42ccf386cb3a6c6 to your computer and use it in GitHub Desktop.
Save fabidick22/96f50b072b7b2ea1e42ccf386cb3a6c6 to your computer and use it in GitHub Desktop.
Script to get a github actions runner token (no dependencies)
#!/usr/bin/env python3
import urllib.request
import urllib.parse as req_parse
import os
import json
GITHUB_API = os.getenv("GITHUB_API", "https://api.github.com/{}")
RUNNER_SCOPE = os.getenv("GITHUB_API", "repo")
GITHUB_OWNER = os.getenv("GITHUB_OWNER")
GITHUB_REPO = os.getenv("GITHUB_REPO")
GITHUB_PAT = os.getenv("GITHUB_PAT")
def get_token():
req = urllib.request.Request(GITHUB_API.format(f"repos/{GITHUB_OWNER}/{GITHUB_REPO}/actions/runners/registration-token"))
req.add_header('Accept', 'application/vnd.github.v3+json"')
req.add_header('Authorization', f'token {GITHUB_PAT}')
r = urllib.request.urlopen(req, data=req_parse.urlencode({}).encode('ascii'))
token = json.loads(r.read().decode('utf-8'))
print(json.dumps(token))
def check_config():
assert GITHUB_PAT is not None, "GitHub Personal Access Token is required!"
if RUNNER_SCOPE == "repo":
assert GITHUB_REPO is not None, "GitHub repository is required!"
get_token()
else:
raise Exception("Scope not supported!")
if __name__ == '__main__':
check_config()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment