From 5e2a8e9f27a56f9e45db800fa91099e68165358b Mon Sep 17 00:00:00 2001 From: Viktor Lofgren Date: Tue, 31 Dec 2024 16:04:13 +0100 Subject: [PATCH] (deploy) Add capability of adding tags to deploy script --- tools/deployment/deployment.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tools/deployment/deployment.py b/tools/deployment/deployment.py index 044606b8..deb7e4df 100755 --- a/tools/deployment/deployment.py +++ b/tools/deployment/deployment.py @@ -222,6 +222,31 @@ def run_gradle_build(targets: str) -> None: if return_code != 0: raise BuildError(service, return_code) + +def find_free_tag() -> str: + cmd = ['git', 'tag'] + result = subprocess.run(cmd, capture_output=True, text=True) + + if result.returncode != 0: + raise RuntimeError(f"Git command failed: {result.stderr}") + + existing_tags = set(result.stdout.splitlines()) + + for i in range(1, 100000): + tag = f'deploy-{i:04d}' + if not tag in existing_tags: + return tag + raise RuntimeError(f"Failed to find a free deployment tag") + +def add_tags(tags: str) -> None: + new_tag = find_free_tag() + + cmd = ['git', 'tag', new_tag, '-am', tags] + result = subprocess.run(cmd) + + if result.returncode != 0: + raise RuntimeError(f"Git command failed: {result.stderr}") + # Example usage: if __name__ == '__main__': # Define service configuration @@ -295,7 +320,9 @@ if __name__ == '__main__': parser = argparse.ArgumentParser( prog='deployment.py', description='Continuous Deployment helper') + parser.add_argument('-v', '--verify', help='Verify the tags are valid, if present', action='store_true') + parser.add_argument('-a', '--add', help='Add the tags provided as a new deployment tag, usually combined with -t', action='store_true') parser.add_argument('-t', '--tag', help='Use the specified tag value instead of the head git tag starting with deploy-') args = parser.parse_args() @@ -316,7 +343,10 @@ if __name__ == '__main__': print("Services to build:", plan.services_to_build) print("Instances to deploy:", [container.name for container in plan.instances_to_deploy]) - if not args.verify: + if args.verify: + if args.add: + add_tags(args.tag) + else: print("\nExecution Plan:") build_and_deploy(plan, SERVICE_CONFIG)