Skip to content

Commit 9bd1970

Browse files
Publish helm chart to OCI registry for PR patches
1 parent 3ec0ca1 commit 9bd1970

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

.evergreen-functions.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ functions:
223223
working_dir: src/github.com/mongodb/mongodb-kubernetes
224224
binary: scripts/evergreen/setup_docker_sbom.sh
225225

226+
helm_registry_login:
227+
command: subprocess.exec
228+
type: setup
229+
params:
230+
working_dir: src/github.com/mongodb/mongodb-kubernetes
231+
add_to_path:
232+
- ${workdir}/bin
233+
binary: scripts/dev/helm_registry_login.sh
234+
226235
# Logs into all used registries
227236
configure_docker_auth: &configure_docker_auth
228237
command: subprocess.exec
@@ -497,6 +506,15 @@ functions:
497506
- rh_pyxis
498507
binary: scripts/dev/run_python.sh scripts/preflight_images.py --image ${image_name} --submit "${preflight_submit}"
499508

509+
# publish_helm_chart packages and publishes the MCK helm chart to the OCI container registry
510+
publish_helm_chart:
511+
- command: subprocess.exec
512+
params:
513+
working_dir: src/github.com/mongodb/mongodb-kubernetes
514+
include_expansions_in_env:
515+
- version_id
516+
binary: scripts/dev/run_python.sh scripts/publish_helm_chart.py
517+
500518
build_multi_cluster_binary:
501519
- command: subprocess.exec
502520
type: setup

.evergreen.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,16 @@ tasks:
448448
vars:
449449
image_name: upgrade-hook
450450

451+
- name: publish_helm_chart
452+
commands:
453+
- func: clone
454+
- func: setup_kubectl
455+
- func: setup_aws
456+
- func: prepare_aws
457+
- func: helm_registry_login
458+
- func: python_venv
459+
- func: publish_helm_chart
460+
451461
- name: prepare_aws
452462
priority: 59
453463
commands:
@@ -1675,6 +1685,7 @@ buildvariants:
16751685
- name: build_readiness_probe_image
16761686
- name: build_upgrade_hook_image
16771687
- name: prepare_aws
1688+
- name: publish_helm_chart
16781689

16791690
- name: init_test_run_ibm_power
16801691
display_name: init_test_run_ibm_power

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)