|
| 1 | +import subprocess |
| 2 | +import os |
| 3 | +import yaml |
| 4 | + |
| 5 | +from lib.base_logger import logger |
| 6 | + |
| 7 | +CHART_DIR = "helm_chart" |
| 8 | + |
| 9 | +OCI_REGISTRY = "oci://268558157000.dkr.ecr.us-east-1.amazonaws.com/dev/helm-charts" |
| 10 | + |
| 11 | +def run_command(command: list[str], description: str): |
| 12 | + try: |
| 13 | + subprocess.run(command, check=True, text=True, capture_output=False) |
| 14 | + logger.info(f"Command {' '.join(command)} executed successfully.") |
| 15 | + except subprocess.CalledProcessError as e: |
| 16 | + logger.error(f"Error executing command: {' '.join(command)}") |
| 17 | + raise RuntimeError(f"{description} failed.") from e |
| 18 | + except FileNotFoundError: |
| 19 | + raise FileNotFoundError("Error: 'helm' command not found. Ensure Helm CLI is installed and in your PATH.") |
| 20 | + |
| 21 | +def update_chart_and_get_metadata(chart_dir: str) -> tuple[str, str]: |
| 22 | + chart_path = os.path.join(chart_dir, "Chart.yaml") |
| 23 | + version_id = os.environ.get('version_id') |
| 24 | + 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}" |
| 28 | + |
| 29 | + logger.info(f"New helm chart version will be: {new_version}") |
| 30 | + |
| 31 | + if not os.path.exists(chart_path): |
| 32 | + raise FileNotFoundError( |
| 33 | + f"Error: Chart.yaml not found in directory '{chart_dir}'. " |
| 34 | + "Please ensure the directory exists and contains a valid Chart.yaml." |
| 35 | + ) |
| 36 | + |
| 37 | + try: |
| 38 | + with open(chart_path, 'r') as f: |
| 39 | + data = yaml.safe_load(f) |
| 40 | + |
| 41 | + chart_name = data.get('name') |
| 42 | + if not chart_name: |
| 43 | + raise ValueError("Chart.yaml is missing required 'name' field.") |
| 44 | + |
| 45 | + data['version'] = new_version |
| 46 | + |
| 47 | + with open(chart_path, 'w') as f: |
| 48 | + yaml.safe_dump(data, f, sort_keys=False) |
| 49 | + |
| 50 | + logger.info(f"Successfully updated version for chart '{chart_name}' to '{new_version}' before publishing it.") |
| 51 | + return chart_name, new_version |
| 52 | + |
| 53 | + except Exception as e: |
| 54 | + raise RuntimeError(f"Failed to read or update Chart.yaml: {e}") |
| 55 | + |
| 56 | +def publish_helm_chart(): |
| 57 | + try: |
| 58 | + chart_name, chart_version = update_chart_and_get_metadata(CHART_DIR) |
| 59 | + |
| 60 | + tgz_filename = f"{chart_name}-{chart_version}.tgz" |
| 61 | + logger.info(f"Packaging chart: {chart_name} with Version: {chart_version}") |
| 62 | + |
| 63 | + package_command = ["helm", "package", CHART_DIR] |
| 64 | + run_command(package_command, f"Packaging chart '{CHART_DIR}'") |
| 65 | + |
| 66 | + push_command = ["helm", "push", tgz_filename, OCI_REGISTRY] |
| 67 | + run_command(push_command, f"Pushing '{tgz_filename}' to '{OCI_REGISTRY}'") |
| 68 | + |
| 69 | + if os.path.exists(tgz_filename): |
| 70 | + logger.info(f"\nCleaning up local file: {tgz_filename}") |
| 71 | + os.remove(tgz_filename) |
| 72 | + |
| 73 | + logger(f"Helm Chart {chart_name}:{chart_version} was published successfully!") |
| 74 | + except (FileNotFoundError, RuntimeError, ValueError) as e: |
| 75 | + logger.error(f"\Failed publishing the helm chart: {e}") |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + publish_helm_chart() |
0 commit comments