ClickHouse/tests/ci/build_check.py

383 lines
12 KiB
Python
Raw Normal View History

2021-10-21 11:09:15 +00:00
#!/usr/bin/env python3
2021-10-21 11:09:15 +00:00
import subprocess
import logging
import json
import os
import sys
import time
2022-04-08 22:34:23 +00:00
from shutil import rmtree
from typing import List, Tuple
from env_helper import (
CACHES_PATH,
GITHUB_JOB,
IMAGES_PATH,
REPO_COPY,
S3_BUILDS_BUCKET,
TEMP_PATH,
)
from s3_helper import S3Helper
2021-11-26 14:00:09 +00:00
from pr_info import PRInfo
2022-01-12 12:17:21 +00:00
from version_helper import (
ClickHouseVersion,
Git,
2022-01-12 12:17:21 +00:00
get_version_from_repo,
update_version_local,
)
2021-11-10 09:08:43 +00:00
from ccache_utils import get_ccache_if_not_exists, upload_ccache
2022-01-12 12:17:21 +00:00
from ci_config import CI_CONFIG, BuildConfig
2021-11-12 12:36:25 +00:00
from docker_pull_helper import get_image_with_version
2021-12-03 08:33:16 +00:00
from tee_popen import TeePopen
2021-10-21 11:09:15 +00:00
2022-02-08 18:12:04 +00:00
IMAGE_NAME = "clickhouse/binary-builder"
BUILD_LOG_NAME = "build_log.log"
2022-02-08 18:12:04 +00:00
2021-10-21 11:09:15 +00:00
2022-01-12 12:17:21 +00:00
def _can_export_binaries(build_config: BuildConfig) -> bool:
if build_config["package_type"] != "deb":
2021-10-21 11:09:15 +00:00
return False
if build_config["bundled"] != "bundled":
2021-10-21 11:09:15 +00:00
return False
if build_config["splitted"] == "splitted":
2021-10-21 11:09:15 +00:00
return False
if build_config["sanitizer"] != "":
2021-10-21 11:09:15 +00:00
return True
if build_config["build_type"] != "":
2021-10-21 11:09:15 +00:00
return True
return False
def get_packager_cmd(
2022-01-12 12:17:21 +00:00
build_config: BuildConfig,
packager_path: str,
output_path: str,
build_version: str,
image_version: str,
ccache_path: str,
2022-03-30 14:29:13 +00:00
official: bool,
2022-01-12 12:17:21 +00:00
) -> str:
package_type = build_config["package_type"]
comp = build_config["compiler"]
cmd = (
f"cd {packager_path} && ./packager --output-dir={output_path} "
f"--package-type={package_type} --compiler={comp}"
)
if build_config["build_type"]:
cmd += f" --build-type={build_config['build_type']}"
if build_config["sanitizer"]:
cmd += f" --sanitizer={build_config['sanitizer']}"
if build_config["splitted"] == "splitted":
cmd += " --split-binary"
if build_config["tidy"] == "enable":
cmd += " --clang-tidy"
cmd += " --cache=ccache"
cmd += f" --ccache_dir={ccache_path}"
2022-03-14 21:06:53 +00:00
if "additional_pkgs" in build_config and build_config["additional_pkgs"]:
2022-03-14 21:07:58 +00:00
cmd += " --additional-pkgs"
cmd += f" --docker-image-version={image_version}"
cmd += f" --version={build_version}"
2021-10-21 11:09:15 +00:00
if _can_export_binaries(build_config):
cmd += " --with-binaries=tests"
2021-10-21 11:09:15 +00:00
2022-03-30 14:29:13 +00:00
if official:
cmd += " --official"
2021-10-21 11:09:15 +00:00
return cmd
2022-01-12 12:17:21 +00:00
def build_clickhouse(
packager_cmd: str, logs_path: str, build_output_path: str
) -> Tuple[str, bool]:
build_log_path = os.path.join(logs_path, BUILD_LOG_NAME)
2022-01-12 12:17:21 +00:00
success = False
2021-12-03 08:33:16 +00:00
with TeePopen(packager_cmd, build_log_path) as process:
retcode = process.wait()
2021-12-14 10:40:03 +00:00
if os.path.exists(build_output_path):
build_results = os.listdir(build_output_path)
else:
build_results = []
2021-10-21 11:09:15 +00:00
if retcode == 0:
2022-01-12 12:17:21 +00:00
if len(build_results) > 0:
success = True
2021-12-14 10:40:03 +00:00
logging.info("Built successfully")
else:
logging.info(
"Success exit code, but no build artifacts => build failed"
)
2021-10-21 11:09:15 +00:00
else:
logging.info("Build failed")
2022-01-12 12:17:21 +00:00
return build_log_path, success
2021-10-21 11:09:15 +00:00
2021-12-01 14:23:51 +00:00
def check_for_success_run(
s3_helper: S3Helper,
s3_prefix: str,
build_name: str,
build_config: BuildConfig,
):
logged_prefix = os.path.join(S3_BUILDS_BUCKET, s3_prefix)
logging.info("Checking for artifacts in %s", logged_prefix)
2021-12-01 14:23:51 +00:00
try:
# TODO: theoretically, it would miss performance artifact for pr==0,
# but luckily we rerun only really failed tasks now, so we're safe
build_results = s3_helper.list_prefix(s3_prefix)
2021-12-01 14:23:51 +00:00
except Exception as ex:
logging.info("Got exception while listing %s: %s\nRerun", logged_prefix, ex)
return
if build_results is None or len(build_results) == 0:
logging.info("Nothing found in %s, rerun", logged_prefix)
return
logging.info("Some build results found:\n%s", build_results)
build_urls = []
log_url = ""
for url in build_results:
url_escaped = url.replace("+", "%2B").replace(" ", "%20")
if BUILD_LOG_NAME in url:
log_url = f"https://s3.amazonaws.com/{S3_BUILDS_BUCKET}/{url_escaped}"
else:
build_urls.append(
f"https://s3.amazonaws.com/{S3_BUILDS_BUCKET}/{url_escaped}"
)
if not log_url:
# log is uploaded the last, so if there's no log we need to rerun the build
return
success = len(build_urls) > 0
create_json_artifact(
TEMP_PATH,
build_name,
log_url,
build_urls,
build_config,
0,
success,
)
# Fail build job if not successeded
if not success:
sys.exit(1)
else:
sys.exit(0)
2021-12-01 14:23:51 +00:00
def create_json_artifact(
2022-01-12 12:17:21 +00:00
temp_path: str,
build_name: str,
log_url: str,
build_urls: List[str],
build_config: BuildConfig,
elapsed: int,
success: bool,
):
subprocess.check_call(
f"echo 'BUILD_URLS=build_urls_{build_name}' >> $GITHUB_ENV", shell=True
)
2021-12-01 14:23:51 +00:00
result = {
"log_url": log_url,
"build_urls": build_urls,
"build_config": build_config,
"elapsed_seconds": elapsed,
"status": success,
2022-05-25 13:15:11 +00:00
"job_name": GITHUB_JOB,
2021-12-01 14:23:51 +00:00
}
json_name = "build_urls_" + build_name + ".json"
print(f"Dump json report {result} to {json_name} with env build_urls_{build_name}")
with open(os.path.join(temp_path, json_name), "w", encoding="utf-8") as build_links:
2021-12-01 14:23:51 +00:00
json.dump(result, build_links)
def get_release_or_pr(pr_info: PRInfo, version: ClickHouseVersion) -> Tuple[str, str]:
# FIXME performance
# performance builds are havily relies on a fixed path for artifacts, that's why
# we need to preserve 0 for anything but PR number
# It should be fixed in performance-comparison image eventually
performance_pr = "0"
2022-01-12 12:17:21 +00:00
if "release" in pr_info.labels or "release-lts" in pr_info.labels:
# for release pull requests we use branch names prefixes, not pr numbers
return pr_info.head_ref, performance_pr
elif pr_info.number == 0:
# for pushes to master - major version
return f"{version.major}.{version.minor}", performance_pr
2022-01-12 12:17:21 +00:00
# PR number for anything else
pr_number = str(pr_info.number)
return pr_number, pr_number
2022-01-12 12:17:21 +00:00
def upload_master_static_binaries(
pr_info: PRInfo,
build_config: BuildConfig,
s3_helper: S3Helper,
build_output_path: str,
):
"""Upload binary artifacts to a static S3 links"""
2022-01-13 14:13:58 +00:00
static_binary_name = build_config.get("static_binary_name", False)
if pr_info.number != 0:
return
2022-01-13 12:55:51 +00:00
elif not static_binary_name:
return
elif pr_info.base_ref != "master":
return
2022-01-13 12:55:51 +00:00
s3_path = "/".join((pr_info.base_ref, static_binary_name, "clickhouse"))
binary = os.path.join(build_output_path, "clickhouse")
url = s3_helper.upload_build_file_to_s3(binary, s3_path)
print(f"::notice ::Binary static URL: {url}")
2022-01-12 12:17:21 +00:00
def main():
2021-10-21 11:09:15 +00:00
logging.basicConfig(level=logging.INFO)
build_name = sys.argv[1]
2021-10-21 11:09:15 +00:00
build_config = CI_CONFIG["build_config"][build_name]
2021-10-21 11:09:15 +00:00
2022-01-12 12:17:21 +00:00
if not os.path.exists(TEMP_PATH):
os.makedirs(TEMP_PATH)
2021-10-21 11:09:15 +00:00
2021-11-26 14:00:09 +00:00
pr_info = PRInfo()
2021-10-21 11:09:15 +00:00
2022-01-12 12:17:21 +00:00
logging.info("Repo copy path %s", REPO_COPY)
2021-10-21 11:09:15 +00:00
s3_helper = S3Helper("https://s3.amazonaws.com")
2021-12-01 14:23:51 +00:00
version = get_version_from_repo(git=Git(True))
release_or_pr, performance_pr = get_release_or_pr(pr_info, version)
2021-12-01 14:23:51 +00:00
s3_path_prefix = "/".join((release_or_pr, pr_info.sha, build_name))
# FIXME performance
s3_performance_path = "/".join(
(performance_pr, pr_info.sha, build_name, "performance.tgz")
)
2021-12-01 14:23:51 +00:00
# If this is rerun, then we try to find already created artifacts and just
# put them as github actions artifact (result)
check_for_success_run(s3_helper, s3_path_prefix, build_name, build_config)
2021-10-21 11:09:15 +00:00
2022-02-08 18:12:04 +00:00
docker_image = get_image_with_version(IMAGES_PATH, IMAGE_NAME)
2021-11-12 12:36:25 +00:00
image_version = docker_image.version
2021-10-21 11:09:15 +00:00
2022-01-28 13:39:23 +00:00
logging.info("Got version from repo %s", version.string)
2022-03-30 14:29:13 +00:00
official_flag = pr_info.number == 0
2022-05-13 17:22:23 +00:00
if "official" in build_config:
official_flag = build_config["official"]
version_type = "testing"
if "release" in pr_info.labels or "release-lts" in pr_info.labels:
version_type = "stable"
2022-03-30 14:29:13 +00:00
official_flag = True
update_version_local(version, version_type)
logging.info("Updated local files with version")
2021-10-21 11:09:15 +00:00
logging.info("Build short name %s", build_name)
2021-10-21 11:09:15 +00:00
2022-01-12 12:17:21 +00:00
build_output_path = os.path.join(TEMP_PATH, build_name)
2021-10-21 11:09:15 +00:00
if not os.path.exists(build_output_path):
os.makedirs(build_output_path)
2022-01-12 12:17:21 +00:00
ccache_path = os.path.join(CACHES_PATH, build_name + "_ccache")
2021-11-10 09:08:43 +00:00
logging.info("Will try to fetch cache for our build")
2022-04-08 22:34:23 +00:00
try:
get_ccache_if_not_exists(ccache_path, s3_helper, pr_info.number, TEMP_PATH)
except Exception as e:
# In case there are issues with ccache, remove the path and do not fail a build
logging.info("Failed to get ccache, building without it. Error: %s", e)
rmtree(ccache_path, ignore_errors=True)
2021-11-10 09:08:43 +00:00
2021-10-21 11:48:56 +00:00
if not os.path.exists(ccache_path):
2021-11-10 09:08:43 +00:00
logging.info("cache was not fetched, will create empty dir")
2021-10-21 11:48:56 +00:00
os.makedirs(ccache_path)
packager_cmd = get_packager_cmd(
build_config,
2022-01-12 12:17:21 +00:00
os.path.join(REPO_COPY, "docker/packager"),
build_output_path,
2022-01-28 13:39:23 +00:00
version.string,
image_version,
ccache_path,
2022-04-06 08:15:36 +00:00
official_flag,
)
2022-03-30 14:29:13 +00:00
2021-10-21 11:09:15 +00:00
logging.info("Going to run packager with %s", packager_cmd)
logs_path = os.path.join(TEMP_PATH, "build_log")
if not os.path.exists(logs_path):
os.makedirs(logs_path)
2021-10-21 11:09:15 +00:00
2021-10-21 14:41:07 +00:00
start = time.time()
log_path, success = build_clickhouse(packager_cmd, logs_path, build_output_path)
2021-10-21 14:41:07 +00:00
elapsed = int(time.time() - start)
subprocess.check_call(
f"sudo chown -R ubuntu:ubuntu {build_output_path}", shell=True
)
2021-10-21 12:37:19 +00:00
subprocess.check_call(f"sudo chown -R ubuntu:ubuntu {ccache_path}", shell=True)
2021-10-21 11:09:15 +00:00
logging.info("Build finished with %s, log path %s", success, log_path)
# Upload the ccache first to have the least build time in case of problems
2021-11-10 09:08:43 +00:00
logging.info("Will upload cache")
2022-01-12 12:17:21 +00:00
upload_ccache(ccache_path, s3_helper, pr_info.number, TEMP_PATH)
2021-11-10 09:08:43 +00:00
# FIXME performance
2022-05-19 20:44:10 +00:00
performance_urls = []
performance_path = os.path.join(build_output_path, "performance.tgz")
if os.path.exists(performance_path):
2022-05-19 20:44:10 +00:00
performance_urls.append(
s3_helper.upload_build_file_to_s3(performance_path, s3_performance_path)
)
logging.info(
"Uploaded performance.tgz to %s, now delete to avoid duplication",
2022-05-19 20:44:10 +00:00
performance_urls[0],
)
os.remove(performance_path)
2022-05-19 20:44:10 +00:00
build_urls = (
s3_helper.upload_build_folder_to_s3(
build_output_path,
s3_path_prefix,
keep_dirs_in_s3_path=False,
upload_symlinks=False,
)
+ performance_urls
)
2021-10-21 12:37:19 +00:00
logging.info("Got build URLs %s", build_urls)
2021-10-21 12:46:25 +00:00
print("::notice ::Build URLs: {}".format("\n".join(build_urls)))
2021-10-21 12:46:25 +00:00
if os.path.exists(log_path):
log_url = s3_helper.upload_build_file_to_s3(
log_path, s3_path_prefix + "/" + os.path.basename(log_path)
)
logging.info("Log url %s", log_url)
else:
logging.info("Build log doesn't exist")
print(f"::notice ::Log URL: {log_url}")
2021-10-21 11:09:15 +00:00
create_json_artifact(
2022-01-12 12:17:21 +00:00
TEMP_PATH, build_name, log_url, build_urls, build_config, elapsed, success
)
upload_master_static_binaries(pr_info, build_config, s3_helper, build_output_path)
2021-11-25 10:01:29 +00:00
# Fail build job if not successeded
if not success:
sys.exit(1)
2022-01-12 12:17:21 +00:00
if __name__ == "__main__":
main()