|
| 1 | +import os |
| 2 | +import re |
| 3 | + |
| 4 | +import httpx |
| 5 | + |
| 6 | +DEPLOY_OUTPUT = os.environ['DEPLOY_OUTPUT'] |
| 7 | +GITHUB_TOKEN = os.environ['GITHUB_TOKEN'] |
| 8 | +REPOSITORY = os.environ['REPOSITORY'] |
| 9 | +ENVIRONMENT = os.environ['ENVIRONMENT'] |
| 10 | + |
| 11 | +m = re.search(r'https://(\S+)\.workers\.dev', DEPLOY_OUTPUT) |
| 12 | +assert m, f'Could not find worker URL in {DEPLOY_OUTPUT!r}' |
| 13 | + |
| 14 | +worker_name = m.group(1) |
| 15 | +m = re.search(r'Current Version ID: ([^-]+)', DEPLOY_OUTPUT) |
| 16 | +assert m, f'Could not find version ID in {DEPLOY_OUTPUT!r}' |
| 17 | + |
| 18 | +version_id = m.group(1) |
| 19 | +preview_url = f'https://{version_id}-{worker_name}.workers.dev' |
| 20 | + |
| 21 | +gh_headers = { |
| 22 | + 'Accept': 'application/vnd.github+json', |
| 23 | + 'Authorization': f'Bearer {GITHUB_TOKEN}', |
| 24 | + 'X-GitHub-Api-Version': '2022-11-28', |
| 25 | +} |
| 26 | + |
| 27 | +deployment_url = f'https://api.github.com/repos/{REPOSITORY}/deployments' |
| 28 | +deployment_data = { |
| 29 | + 'ref': '9616d25acd9d73e36b8e57032fc6bec7e9bb42f1', |
| 30 | + 'task': 'docs preview', |
| 31 | + 'environment': ENVIRONMENT, |
| 32 | +} |
| 33 | +r = httpx.post(deployment_url, headers=gh_headers, json=deployment_data) |
| 34 | +print(f'POST {deployment_url}: {r.status_code} {r.text}') |
| 35 | +r.raise_for_status() |
| 36 | +deployment_id = r.json()['id'] |
| 37 | + |
| 38 | +status_url = f'https://api.github.com/repos/{REPOSITORY}/deployments/{deployment_id}/statuses' |
| 39 | +status_data = { |
| 40 | + 'environment': ENVIRONMENT, |
| 41 | + 'environment_url': preview_url, |
| 42 | + 'state': 'success', |
| 43 | +} |
| 44 | +r = httpx.post(status_url, headers=gh_headers, json=status_data) |
| 45 | +print(f'POST {status_url}: {r.status_code} {r.text}') |
| 46 | +r.raise_for_status() |
0 commit comments