File
Blob: tools/cross/internal_build.py
| 1 | import argparse |
| 2 | import sys |
| 3 | import time |
| 4 | |
| 5 | import requests |
| 6 | |
| 7 | |
| 8 | def parse_args(): |
| 9 | parser = argparse.ArgumentParser() |
| 10 | |
| 11 | parser.add_argument("pr_id", help="Pull Request ID") |
| 12 | parser.add_argument("merge_sha", help="Merge Commit SHA") |
| 13 | parser.add_argument("head_sha", help="HEAD Commit SHA") |
| 14 | parser.add_argument("run_attempt", help="# of Run Attempt") |
| 15 | parser.add_argument("branch_name", help="PR's Branch Name") |
| 16 | parser.add_argument("URL", help="URL to submit build task") |
| 17 | parser.add_argument("client_id", help="CF Access client id") |
| 18 | parser.add_argument("secret", help="CF Access client secret") |
| 19 | |
| 20 | return parser.parse_args() |
| 21 | |
| 22 | |
| 23 | if __name__ == "__main__": |
| 24 | args = parse_args() |
| 25 | |
| 26 | # Submit build job |
| 27 | headers = { |
| 28 | "CF-Access-Client-Id": args.client_id, |
| 29 | "CF-Access-Client-Secret": args.secret, |
| 30 | } |
| 31 | |
| 32 | payload = { |
| 33 | "pr_id": args.pr_id, |
| 34 | "merge_commit_sha": args.merge_sha, |
| 35 | "head_commit_sha": args.head_sha, |
| 36 | "run_attempt": args.run_attempt, |
| 37 | "branch_name": args.branch_name, |
| 38 | } |
| 39 | |
| 40 | workflow_id = "" |
| 41 | try: |
| 42 | resp = requests.post(args.URL, headers=headers, json=payload) |
| 43 | resp.raise_for_status() |
| 44 | |
| 45 | workflow_id = resp.json()["workflow_id"] |
| 46 | except Exception as err: |
| 47 | print(f"Unexpected error {err=}, {type(err)=}, exiting.") |
| 48 | sys.exit(1) |
| 49 | |
| 50 | print("Internal build submitted.") |
| 51 | |
| 52 | time.sleep(30) |
| 53 | |
| 54 | # Poll build status |
| 55 | failed_requests = 0 |
| 56 | FAILED_REQUEST_LIMIT = 10 |
| 57 | |
| 58 | while failed_requests < FAILED_REQUEST_LIMIT: |
| 59 | try: |
| 60 | resp = requests.get(args.URL, headers=headers, params={"id": workflow_id}) |
| 61 | resp.raise_for_status() |
| 62 | |
| 63 | status = resp.json()["status"] |
| 64 | if status == "errored": |
| 65 | print("Internal build failed.") |
| 66 | print(resp.json()["error"]["message"]) |
| 67 | print( |
| 68 | "If you are a Cloudflare employee, please check your internal" |
| 69 | " branch and refer this doc for further details:" |
| 70 | " https://cflare.co/workerd-internal-build" |
| 71 | ) |
| 72 | print( |
| 73 | "If you are an external contributor, please ask the auto-assigned" |
| 74 | " reviewers to check the internal build failure." |
| 75 | ) |
| 76 | sys.exit(1) |
| 77 | elif status == "complete": |
| 78 | break |
| 79 | |
| 80 | print("Waiting for build to finish..") |
| 81 | |
| 82 | except Exception as err: |
| 83 | print(f"Unexpected error {err=}, {type(err)=}") |
| 84 | failed_requests = failed_requests + 1 |
| 85 | time.sleep(30) |
| 86 | |
| 87 | if failed_requests == FAILED_REQUEST_LIMIT: |
| 88 | print(f"{failed_requests=} == {FAILED_REQUEST_LIMIT=}, exiting.") |
| 89 | sys.exit(1) |
| 90 | |
| 91 | print("Internal build succeeded.") |