Skip to content

Commit 067b257

Browse files
Publish helm chart to OCI registry for PR patches
1 parent 749c1a3 commit 067b257

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
@@ -220,6 +220,15 @@ functions:
220220
working_dir: src/github.com/mongodb/mongodb-kubernetes
221221
binary: scripts/evergreen/setup_docker_sbom.sh
222222

223+
helm_registry_login:
224+
command: subprocess.exec
225+
type: setup
226+
params:
227+
working_dir: src/github.com/mongodb/mongodb-kubernetes
228+
add_to_path:
229+
- ${workdir}/bin
230+
binary: scripts/dev/helm_registry_login.sh
231+
223232
# Logs into all used registries
224233
configure_docker_auth: &configure_docker_auth
225234
command: subprocess.exec
@@ -489,6 +498,15 @@ functions:
489498
- rh_pyxis
490499
binary: scripts/dev/run_python.sh scripts/preflight_images.py --image ${image_name} --submit "${preflight_submit}"
491500

501+
# publish_helm_chart packages and publishes the MCK helm chart to the OCI container registry
502+
publish_helm_chart:
503+
- command: subprocess.exec
504+
params:
505+
working_dir: src/github.com/mongodb/mongodb-kubernetes
506+
include_expansions_in_env:
507+
- version_id
508+
binary: scripts/dev/run_python.sh scripts/publish_helm_chart.py
509+
492510
build_multi_cluster_binary:
493511
- command: subprocess.exec
494512
type: setup

.evergreen.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,16 @@ tasks:
435435
- func: setup_building_host
436436
- func: pipeline_version_upgrade_hook
437437

438+
- name: publish_helm_chart
439+
commands:
440+
- func: clone
441+
- func: setup_kubectl
442+
- func: setup_aws
443+
- func: prepare_aws
444+
- func: helm_registry_login
445+
- func: python_venv
446+
- func: publish_helm_chart
447+
438448
- name: prepare_aws
439449
priority: 59
440450
commands:
@@ -1682,6 +1692,7 @@ buildvariants:
16821692
- name: build_readiness_probe_image
16831693
- name: build_version_upgrade_hook_image
16841694
- name: prepare_aws
1695+
- name: publish_helm_chart
16851696

16861697
- name: init_test_run_ibm_power
16871698
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)