get_build_info.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python
  2. """
  3. This script gathers build metadata from Travis environment variables and Travis
  4. APIs.
  5. Usage:
  6. $ python get_build_info.py
  7. {
  8. "json": ["containing", "build", "metadata"]
  9. }
  10. """
  11. import os
  12. import sys
  13. import json
  14. def gha_get_self_url():
  15. import requests
  16. # stringed together api call to get the current check's html url.
  17. sha = os.environ["GITHUB_SHA"]
  18. repo = os.environ["GITHUB_REPOSITORY"]
  19. resp = requests.get(
  20. "https://api.github.com/repos/{}/commits/{}/check-suites".format(repo, sha)
  21. )
  22. data = resp.json()
  23. for check in data["check_suites"]:
  24. slug = check["app"]["slug"]
  25. if slug == "github-actions":
  26. run_url = check["check_runs_url"]
  27. html_url = requests.get(run_url).json()["check_runs"][0]["html_url"]
  28. return html_url
  29. # Return a fallback url
  30. return "https://github.com/ray-project/ray/actions"
  31. def get_build_env():
  32. if os.environ.get("GITHUB_ACTION"):
  33. return {
  34. "TRAVIS_COMMIT": os.environ["GITHUB_SHA"],
  35. "TRAVIS_JOB_WEB_URL": gha_get_self_url(),
  36. "TRAVIS_OS_NAME": "windows",
  37. }
  38. if os.environ.get("BUILDKITE"):
  39. return {
  40. "TRAVIS_COMMIT": os.environ["BUILDKITE_COMMIT"],
  41. "TRAVIS_JOB_WEB_URL": (
  42. os.environ["BUILDKITE_BUILD_URL"] + "#" + os.environ["BUILDKITE_JOB_ID"]
  43. ),
  44. "TRAVIS_OS_NAME": { # The map is used to stay consistent with Travis
  45. "linux": "linux",
  46. "darwin": "osx",
  47. "win32": "windows",
  48. }[sys.platform],
  49. }
  50. keys = [
  51. "TRAVIS_COMMIT",
  52. "TRAVIS_JOB_WEB_URL",
  53. "TRAVIS_OS_NAME",
  54. ]
  55. return {key: os.environ.get(key) for key in keys}
  56. def get_build_config():
  57. if os.environ.get("GITHUB_ACTION"):
  58. return {"config": {"env": "Windows CI"}}
  59. if os.environ.get("BUILDKITE"):
  60. return {"config": {"env": "Buildkite " + os.environ["BUILDKITE_LABEL"]}}
  61. import requests
  62. url = "https://api.travis-ci.com/job/{job_id}?include=job.config"
  63. url = url.format(job_id=os.environ["TRAVIS_JOB_ID"])
  64. resp = requests.get(url, headers={"Travis-API-Version": "3"})
  65. return resp.json()
  66. if __name__ == "__main__":
  67. build_env = get_build_env()
  68. build_config = get_build_config()
  69. print(json.dumps({"build_env": build_env, "build_config": build_config}, indent=2))