Skip to content

Commit dfbc534

Browse files
Publish helm chart to OCI registry for PR patches
1 parent fd4450b commit dfbc534

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

scripts/dev/helm_registry_login.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
3+
ECR_REGISTRY="268558157000.dkr.ecr.us-east-1.amazonaws.com"
4+
AWS_REGION="us-east-1"
5+
6+
source scripts/dev/set_env_context.sh
7+
8+
export PATH=${PROJECT_DIR}/bin:$PATH
9+
10+
echo "Checking if helm CLI is installed..."
11+
if ! command -v helm &> /dev/null
12+
then
13+
echo "Error: helm CLI could not be found."
14+
echo "Please ensure helm is installed and in your system's PATH."
15+
exit 1
16+
fi
17+
18+
echo "Checking if aws CLI is installed..."
19+
if ! command -v aws &> /dev/null
20+
then
21+
echo "Error: aws CLI could not be found."
22+
echo "Please ensure aws CLI is installed and configured."
23+
exit 1
24+
fi
25+
26+
echo "Logging into OCI Registry: ${ECR_REGISTRY} in region ${AWS_REGION}..."
27+
28+
if aws ecr get-login-password --region "$AWS_REGION" | helm registry login \
29+
--username AWS \
30+
--password-stdin \
31+
"$ECR_REGISTRY"
32+
then
33+
echo "Helm successfully logged into the OCI registry."
34+
exit 0
35+
else
36+
echo "ERROR: Helm login to ECR registry failed."
37+
echo "Please ensure your AWS credentials have permission to access ECR in the specified region."
38+
exit 1
39+
fi

scripts/publish_helm_chart.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)