From fb568768c5eba85dabac5ce8e686d3a1e9fc0a17 Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Thu, 23 Jan 2020 12:38:41 -0500 Subject: [PATCH] include docker tag in build info --- docker/set_build.py | 43 +++++++++++++++++++++++-------------------- lbry/build_info.py | 1 + 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/docker/set_build.py b/docker/set_build.py index 9bbe9bc72..77592c2ef 100644 --- a/docker/set_build.py +++ b/docker/set_build.py @@ -1,38 +1,41 @@ -"""Set the build version to be 'qa', 'rc', 'release'""" - import sys import os import re import logging +import lbry.build_info as build_info_mod log = logging.getLogger() log.addHandler(logging.StreamHandler()) log.setLevel(logging.DEBUG) -def get_build_type(ci_tag=None): - if not ci_tag: - return "qa" - log.debug("getting build type for tag: \"%s\"", ci_tag) - if re.match(r'v\d+\.\d+\.\d+rc\d+$', ci_tag): - return 'rc' - elif re.match(r'v\d+\.\d+\.\d+$', ci_tag): - return 'release' - return 'qa' +def _check_and_set(d: dict, key: str, value: str): + try: + d[key] + except KeyError: + raise Exception(f"{key} var does not exist in {build_info_mod.__file__}") + d[key] = value def main(): - root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) - build_type_path = os.path.join(root_dir, 'lbry', 'build_type.py') - log.debug("configuring build type file: %s", build_type_path) - commit_hash = os.getenv('CI_COMMIT_SHA', os.getenv('TRAVIS_COMMIT')) + build_info = {item: build_info_mod.__dict__[item] for item in dir(build_info_mod) if not item.startswith("__")} + + commit_hash = os.getenv('DOCKER_COMMIT', os.getenv('CI_COMMIT_SHA', os.getenv('TRAVIS_COMMIT'))) if commit_hash is None: raise ValueError("Commit hash not found in env vars") - commit_hash = commit_hash[:6] - build_type = get_build_type(os.getenv('CI_COMMIT_TAG', os.getenv('TRAVIS_TAG'))) - log.debug("setting build type=%s, build commit=%s", build_type, commit_hash) - with open(build_type_path, 'w') as f: - f.write(f"BUILD = \"{build_type}\"\nCOMMIT_HASH = \"{commit_hash}\"\n") + _check_and_set(build_info, "COMMIT_HASH", commit_hash[:6]) + + docker_tag = os.getenv('DOCKER_TAG') + if docker_tag: + _check_and_set(build_info, "DOCKER_TAG", docker_tag) + _check_and_set(build_info, "BUILD", "docker") + else: + ci_tag = os.getenv('CI_COMMIT_TAG', os.getenv('TRAVIS_TAG')) + _check_and_set(build_info, "BUILD", "release" if re.match(r'v\d+\.\d+\.\d+$', str(ci_tag)) else "qa") + + log.debug("build info: %s", ", ".join([f"{k}={v}" for k, v in build_info.items()])) + with open(build_info_mod.__file__, 'w') as f: + f.write("\n".join([f"{k} = \"{v}\"" for k, v in build_info.items()]) + "\n") if __name__ == '__main__': diff --git a/lbry/build_info.py b/lbry/build_info.py index d85b9593b..6dadae418 100644 --- a/lbry/build_info.py +++ b/lbry/build_info.py @@ -1,3 +1,4 @@ # don't touch this. CI server changes this during build/deployment BUILD = "dev" COMMIT_HASH = "none" +DOCKER_TAG = "none"