33import subprocess
44
55import yaml
6+ from release .build .build_scenario import SUPPORTED_SCENARIOS
67
78from lib .base_logger import logger
89from scripts .release .build .build_info import *
@@ -25,15 +26,11 @@ def run_command(command: list[str]):
2526 )
2627
2728
28- # update_chart_and_get_metadata updates the helm chart's Chart.yaml and sets the version
29- # to either evg patch id or commit which is set in OPERATOR_VERSION.
30- def update_chart_and_get_metadata (chart_dir : str , version_prefix : str = None ) -> tuple [str , str ]:
29+ # update_chart_and_get_metadata updates the helm chart's Chart.yaml and sets the proper version
30+ # When we publish the helm chart to dev and staging we append `0.0.0+` in the chart version, details are
31+ # here https://docs.google.com/document/d/1eJ8iKsI0libbpcJakGjxcPfbrTn8lmcZDbQH1UqMR_g/edit?tab=t.gg5ble8qlesq
32+ def update_chart_and_get_metadata (chart_dir : str , version : str ) -> tuple [str , str ]:
3133 chart_path = os .path .join (chart_dir , "Chart.yaml" )
32- version = os .environ .get ("OPERATOR_VERSION" )
33- if not version :
34- raise ValueError (
35- "Error: Environment variable 'OPERATOR_VERSION' must be set to determine the chart version to publish."
36- )
3734
3835 if not os .path .exists (chart_path ):
3936 raise FileNotFoundError (
@@ -51,23 +48,18 @@ def update_chart_and_get_metadata(chart_dir: str, version_prefix: str = None) ->
5148 except Exception as e :
5249 raise Exception (f"Unable to load Chart.yaml from dir { chart_path } : { e } " )
5350
54- # If version_prefix is not specified, the chart.yaml would already have correct chart version
55- if version_prefix is None :
51+ if data [ "version" ] == version :
52+ logger . info ( f"Chart ' { chart_name } ' already has version ' { version } '. No update needed." )
5653 return chart_name , version
5754
58- # When we publish the helm chart to dev and staging we append `0.0.0+` in the chart version, details are
59- # here https://docs.google.com/document/d/1eJ8iKsI0libbpcJakGjxcPfbrTn8lmcZDbQH1UqMR_g/edit?tab=t.gg5ble8qlesq
60- new_version = f"{ version_prefix } { version } "
61- logger .info (f"New helm chart version will be: { new_version } " )
62-
6355 try :
64- data ["version" ] = new_version
56+ data ["version" ] = version
6557
6658 with open (chart_path , "w" ) as f :
6759 yaml .safe_dump (data , f , sort_keys = False )
6860
69- logger .info (f"Successfully updated version for chart '{ chart_name } ' to '{ new_version } '." )
70- return chart_name , new_version
61+ logger .info (f"Successfully updated version for chart '{ chart_name } ' to '{ version } '." )
62+ return chart_name , version
7163 except Exception as e :
7264 raise RuntimeError (f"Failed to read or update Chart.yaml: { e } " )
7365
@@ -87,16 +79,22 @@ def get_oci_registry(chart_info: HelmChartInfo) -> str:
8779 return oci_registry
8880
8981
90- def publish_helm_chart (chart_info : HelmChartInfo , build_scenario ):
82+ def publish_helm_chart (chart_info : HelmChartInfo , operator_version : str ):
9183 try :
92- oci_registry = get_oci_registry (chart_info )
93- chart_name , chart_version = update_chart_and_get_metadata (CHART_DIR , chart_info .version_prefix )
84+ # If version_prefix is not specified, the chart.yaml would already have correct chart version
85+ if chart_info .version_prefix is not None :
86+ helm_version = f"{ chart_info .version_prefix } { operator_version } "
87+ else :
88+ helm_version = operator_version
89+
90+ chart_name , chart_version = update_chart_and_get_metadata (CHART_DIR , helm_version )
9491 tgz_filename = f"{ chart_name } -{ chart_version } .tgz"
9592
9693 logger .info (f"Packaging chart: { chart_name } with Version: { chart_version } " )
9794 package_command = ["helm" , "package" , CHART_DIR ]
9895 run_command (package_command )
9996
97+ oci_registry = get_oci_registry (chart_info )
10098 logger .info (f"Pushing chart to registry: { oci_registry } " )
10199 push_command = ["helm" , "push" , tgz_filename , oci_registry ]
102100 run_command (push_command )
@@ -108,15 +106,35 @@ def publish_helm_chart(chart_info: HelmChartInfo, build_scenario):
108106
109107def main ():
110108 parser = argparse .ArgumentParser (
111- description = "Script to publish helm chart to the OCI container registry, based on the build scenario."
109+ description = "Script to publish helm chart to the OCI container registry, based on the build scenario." ,
110+ formatter_class = argparse .RawDescriptionHelpFormatter ,
111+ )
112+ parser .add_argument (
113+ "-b" ,
114+ "--build-scenario" ,
115+ metavar = "" ,
116+ action = "store" ,
117+ required = True ,
118+ type = str ,
119+ choices = SUPPORTED_SCENARIOS ,
120+ help = f"""Build scenario when reading configuration from 'build_info.json'.
121+ Options: { ", " .join (SUPPORTED_SCENARIOS )} . For '{ BuildScenario .DEVELOPMENT } ' the '{ BuildScenario .PATCH } ' scenario is used to read values from 'build_info.json'""" ,
122+ )
123+ parser .add_argument (
124+ "-v" ,
125+ "--version" ,
126+ metavar = "" ,
127+ action = "store" ,
128+ required = True ,
129+ type = str ,
130+ help = "Operator version to use when publishing helm chart" ,
112131 )
113- parser .add_argument ("--build_scenario" , type = str , help = "Build scenario (e.g., patch, staging etc)." )
114132 args = parser .parse_args ()
115133
116134 build_scenario = args .build_scenario
117135 build_info = load_build_info (build_scenario )
118136
119- return publish_helm_chart (build_info .helm_charts ["mongodb-kubernetes" ], build_scenario )
137+ return publish_helm_chart (build_info .helm_charts ["mongodb-kubernetes" ], args . version )
120138
121139
122140if __name__ == "__main__" :
0 commit comments