2020-08-10 14:26:31 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
This script gathers build metadata from Travis environment variables and Travis
|
|
|
|
APIs.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
$ python get_build_info.py
|
|
|
|
{
|
|
|
|
"json": ["containing", "build", "metadata"]
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
2021-01-13 15:17:11 -08:00
|
|
|
def gha_get_self_url():
|
|
|
|
# stringed together api call to get the current check's html url.
|
|
|
|
sha = os.environ["GITHUB_SHA"]
|
|
|
|
repo = os.environ["GITHUB_REPOSITORY"]
|
|
|
|
resp = requests.get(
|
|
|
|
"https://api.github.com/repos/{}/commits/{}/check-suites".format(
|
|
|
|
repo, sha))
|
|
|
|
data = resp.json()
|
|
|
|
for check in data["check_suites"]:
|
|
|
|
slug = check["app"]["slug"]
|
|
|
|
if slug == "github-actions":
|
|
|
|
run_url = check["check_runs_url"]
|
|
|
|
html_url = (
|
|
|
|
requests.get(run_url).json()["check_runs"][0]["html_url"])
|
|
|
|
return html_url
|
|
|
|
|
|
|
|
# Return a fallback url
|
|
|
|
return "https://github.com/ray-project/ray/actions"
|
|
|
|
|
|
|
|
|
2020-08-10 14:26:31 -07:00
|
|
|
def get_build_env():
|
2021-01-13 15:17:11 -08:00
|
|
|
if os.environ.get("GITHUB_ACTION"):
|
|
|
|
return {
|
|
|
|
"TRAVIS_COMMIT": os.environ["GITHUB_SHA"],
|
|
|
|
"TRAVIS_JOB_WEB_URL": gha_get_self_url(),
|
|
|
|
"TRAVIS_OS_NAME": "windows",
|
|
|
|
}
|
|
|
|
|
2021-04-13 14:16:42 -07:00
|
|
|
if os.environ.get("BUILDKITE"):
|
|
|
|
return {
|
|
|
|
"TRAVIS_COMMIT": os.environ["BUILDKITE_COMMIT"],
|
2021-04-14 18:58:23 -07:00
|
|
|
"TRAVIS_JOB_WEB_URL": (os.environ["BUILDKITE_BUILD_URL"] + "#" +
|
|
|
|
os.environ["BUILDKITE_BUILD_ID"]),
|
2021-06-08 09:33:03 -07:00
|
|
|
"TRAVIS_OS_NAME": "linux",
|
2021-04-13 14:16:42 -07:00
|
|
|
}
|
|
|
|
|
2020-08-10 14:26:31 -07:00
|
|
|
keys = [
|
2021-01-13 15:17:11 -08:00
|
|
|
"TRAVIS_COMMIT",
|
|
|
|
"TRAVIS_JOB_WEB_URL",
|
|
|
|
"TRAVIS_OS_NAME",
|
2020-08-10 14:26:31 -07:00
|
|
|
]
|
|
|
|
return {key: os.environ.get(key) for key in keys}
|
|
|
|
|
|
|
|
|
|
|
|
def get_build_config():
|
2021-01-13 15:17:11 -08:00
|
|
|
if os.environ.get("GITHUB_ACTION"):
|
|
|
|
return {"config": {"env": "Windows CI"}}
|
|
|
|
|
2021-04-13 14:16:42 -07:00
|
|
|
if os.environ.get("BUILDKITE"):
|
|
|
|
return {
|
|
|
|
"config": {
|
|
|
|
"env": "Buildkite " + os.environ["BUILDKITE_LABEL"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:26:31 -07:00
|
|
|
url = "https://api.travis-ci.com/job/{job_id}?include=job.config"
|
|
|
|
url = url.format(job_id=os.environ["TRAVIS_JOB_ID"])
|
|
|
|
resp = requests.get(url, headers={"Travis-API-Version": "3"})
|
|
|
|
return resp.json()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
build_env = get_build_env()
|
|
|
|
build_config = get_build_config()
|
|
|
|
|
|
|
|
print(
|
|
|
|
json.dumps(
|
|
|
|
{
|
|
|
|
"build_env": build_env,
|
|
|
|
"build_config": build_config
|
|
|
|
}, indent=2))
|