1- import subprocess
21import os
2+ import subprocess
3+ import sys
4+
35import yaml
46
57from lib .base_logger import logger
8+ from scripts .release .build .build_info import *
69
710CHART_DIR = "helm_chart"
811
9- OCI_REGISTRY = "oci://268558157000.dkr.ecr.us-east-1.amazonaws.com/dev/helm-charts"
1012
11- def run_command (command : list [str ], description : str ):
13+ def run_command (command : list [str ]):
1214 try :
13- subprocess .run (command , check = True , text = True , capture_output = False )
14- logger .info (f"Command { ' ' .join (command )} executed successfully." )
15+ # Using capture_output=True to grab stdout/stderr for better error logging.
16+ process = subprocess .run (command , check = True , text = True , capture_output = True )
17+ logger .info (f"Successfully executed: { ' ' .join (command )} " )
18+ if process .stdout :
19+ logger .info (process .stdout )
1520 except subprocess .CalledProcessError as e :
16- logger .error (f"Error executing command: { ' ' .join (command )} " )
17- raise RuntimeError (f"{ description } failed." ) from e
21+ raise RuntimeError (f"Command { ' ' .join (command )} failed. Stderr: { e .stderr .strip ()} " ) from e
1822 except FileNotFoundError :
19- raise FileNotFoundError ("Error: 'helm' command not found. Ensure Helm CLI is installed and in your PATH." )
23+ raise FileNotFoundError (f "Error: { command [ 0 ] } command not found. Ensure { command [ 0 ] } is installed and in your PATH." )
2024
25+
26+ # update_chart_and_get_metadata updates the helm chart's Chart.yaml and sets the version
27+ # to either evg patch id or commit which is set in OPERATOR_VERSION.
2128def update_chart_and_get_metadata (chart_dir : str ) -> tuple [str , str ]:
2229 chart_path = os .path .join (chart_dir , "Chart.yaml" )
23- version_id = os .environ .get ('version_id' )
30+ version_id = os .environ .get ("OPERATOR_VERSION" )
2431 if not version_id :
25- raise ValueError ("Error: Environment variable 'version_id' must be set to determine the chart version to publish." )
26-
27- new_version = f"0.0.0+ { version_id } "
32+ raise ValueError (
33+ "Error: Environment variable 'OPERATOR_VERSION' must be set to determine the chart version to publish."
34+ )
2835
36+ new_version = f"0.0.0+{ version_id } "
2937 logger .info (f"New helm chart version will be: { new_version } " )
3038
3139 if not os .path .exists (chart_path ):
@@ -35,44 +43,76 @@ def update_chart_and_get_metadata(chart_dir: str) -> tuple[str, str]:
3543 )
3644
3745 try :
38- with open (chart_path , 'r' ) as f :
46+ with open (chart_path , "r" ) as f :
3947 data = yaml .safe_load (f )
4048
41- chart_name = data .get (' name' )
49+ chart_name = data .get (" name" )
4250 if not chart_name :
43- raise ValueError ("Chart.yaml is missing required 'name' field." )
51+ raise ValueError ("Chart.yaml is missing required 'name' field." )
4452
45- data ['version' ] = new_version
46-
47- with open (chart_path , 'w' ) as f :
48- yaml .safe_dump (data , f , sort_keys = False )
53+ data ["version" ] = new_version
4954
50- logger . info ( f"Successfully updated version for chart ' { chart_name } ' to ' { new_version } ' before publishing it." )
51- return chart_name , new_version
55+ with open ( chart_path , "w" ) as f :
56+ yaml . safe_dump ( data , f , sort_keys = False )
5257
58+ logger .info (f"Successfully updated version for chart '{ chart_name } ' to '{ new_version } '." )
59+ return chart_name , new_version
5360 except Exception as e :
5461 raise RuntimeError (f"Failed to read or update Chart.yaml: { e } " )
5562
56- def publish_helm_chart ():
63+
64+ def get_oci_registry (chart_info : HelmChartInfo ) -> str :
65+ registry = chart_info .registry
66+ repo = chart_info .repository
67+
68+ if not registry :
69+ raise ValueError ("Error: registry doesn't seem to be set in HelmChartInfo." )
70+
71+ if not repo :
72+ raise ValueError ("Error: reposiotry doesn't seem to be set in HelmChartInfo." )
73+
74+
75+ oci_registry = f"oci://{ registry } /{ repo } "
76+ logger .info (f"Determined OCI Registry: { oci_registry } " )
77+ return oci_registry
78+
79+
80+ def publish_helm_chart (chart_info : HelmChartInfo ):
5781 try :
82+ oci_registry = get_oci_registry (chart_info )
5883 chart_name , chart_version = update_chart_and_get_metadata (CHART_DIR )
59-
6084 tgz_filename = f"{ chart_name } -{ chart_version } .tgz"
61- logger .info (f"Packaging chart: { chart_name } with Version: { chart_version } " )
6285
63- package_command = ["helm" , "package" , CHART_DIR ]
64- run_command (package_command , f"Packaging chart '{ CHART_DIR } '" )
86+ try :
87+ logger .info (f"Packaging chart: { chart_name } with Version: { chart_version } " )
88+ package_command = ["helm" , "package" , CHART_DIR ]
89+ run_command (package_command )
90+
91+ logger .info (f"Pushing chart to registry: { oci_registry } " )
92+ push_command = ["helm" , "push" , tgz_filename , oci_registry ]
93+ run_command (push_command )
6594
66- push_command = ["helm" , "push" , tgz_filename , OCI_REGISTRY ]
67- run_command (push_command , f"Pushing '{ tgz_filename } ' to '{ OCI_REGISTRY } '" )
95+ logger .info (f"Helm Chart { chart_name } :{ chart_version } was published successfully!" )
96+ finally :
97+ # Cleanup the local .tgz file regardless of push success/failure
98+ if os .path .exists (tgz_filename ):
99+ logger .info (f"Cleaning up local file: { tgz_filename } " )
100+ os .remove (tgz_filename )
68101
69- if os .path .exists (tgz_filename ):
70- logger .info (f"\n Cleaning up local file: { tgz_filename } " )
71- os .remove (tgz_filename )
72-
73- logger (f"Helm Chart { chart_name } :{ chart_version } was published successfully!" )
74102 except (FileNotFoundError , RuntimeError , ValueError ) as e :
75- logger .error (f"\Failed publishing the helm chart: { e } " )
103+ raise Exception (f"Failed publishing the helm chart { e } " )
104+
105+
106+ def main ():
107+ build_scenario = os .environ .get ("BUILD_SCENARIO" )
108+ build_info = load_build_info (build_scenario )
109+
110+ return publish_helm_chart (build_info .helm_charts ["mongodb-kubernetes" ])
111+
76112
77113if __name__ == "__main__" :
78- publish_helm_chart ()
114+ try :
115+ main ()
116+ except Exception as e :
117+ logger .error (f"Failure in the helm publishing process { e } " )
118+ sys .exit (1 )
0 commit comments