Skip to content

Commit 35cbe8e

Browse files
author
Boris Kossev
committed
Add build push charts to Artifactory
1 parent 3985d89 commit 35cbe8e

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

build-push-charts.py

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import os
5+
import subprocess
6+
import yaml
7+
8+
9+
def check_chart_version_exists(
10+
repo_name, chart_name, chart_version, api_key, artifactory_url
11+
):
12+
response = subprocess.run(
13+
[
14+
"curl",
15+
"-s",
16+
"-H",
17+
f"X-JFrog-Art-Api:{api_key}",
18+
f"{artifactory_url}/api/helm/{repo_name}/index.yaml",
19+
],
20+
capture_output=True,
21+
)
22+
23+
response = response.stdout.decode("utf-8")
24+
yaml_data = yaml.safe_load(response)
25+
26+
if 'entries' in yaml_data and chart_name in yaml_data['entries']:
27+
versions = yaml_data['entries'][chart_name]
28+
return any(chart_version in version.get('version', '') for version in versions)
29+
else:
30+
return False
31+
32+
33+
def update_dependency_helm_chart(chart_path):
34+
os.chdir(chart_path)
35+
subprocess.run(
36+
[
37+
"helm",
38+
"dependency",
39+
"update",
40+
"--skip-refresh"
41+
]
42+
)
43+
44+
45+
def package_helm_chart(chart_path):
46+
os.chdir(chart_path)
47+
subprocess.run(
48+
[
49+
"helm",
50+
"package",
51+
"."
52+
]
53+
)
54+
55+
56+
def push_to_artifactory(
57+
chart_path, repo_name, chart_name, chart_version, api_key, artifactory_url
58+
):
59+
chart_file = f"{chart_name}-{chart_version}.tgz"
60+
subprocess.run(
61+
[
62+
"curl",
63+
"-T",
64+
chart_file,
65+
"-H",
66+
f"X-JFrog-Art-Api:{api_key}",
67+
f"{artifactory_url}/{repo_name}/{chart_name}/{chart_version}/{chart_file}",
68+
]
69+
)
70+
71+
72+
def extract_chart_details(chart_path):
73+
chart_file = os.path.join(chart_path, "Chart.yaml")
74+
with open(chart_file, "r") as stream:
75+
try:
76+
chart_data = yaml.safe_load(stream)
77+
return chart_data["name"], chart_data["version"]
78+
except yaml.YAMLError as exc:
79+
print(exc)
80+
return None, None
81+
82+
83+
def process_charts(chart_folder, repository_name, artifactory_url, api_key, override_charts):
84+
subfolders = [f.path for f in os.scandir(chart_folder) if f.is_dir()]
85+
86+
for chart_path in subfolders:
87+
chart_name, chart_version = extract_chart_details(chart_path)
88+
89+
print(f"Processing chart path: '{chart_path}'")
90+
91+
if chart_name and chart_version:
92+
if not check_chart_version_exists(
93+
repository_name, chart_name, chart_version, api_key, artifactory_url
94+
):
95+
do_push_to_registry = True
96+
else:
97+
if override_charts:
98+
do_push_to_registry = True
99+
print(f"Overriding chart version {chart_version} for the chart {chart_name} in the repository...")
100+
else:
101+
do_push_to_registry = False
102+
print(f"Chart version {chart_version} for the chart {chart_name} already exists in the repository. Skipping...")
103+
104+
if do_push_to_registry:
105+
print(f"Updating chart dependencies...")
106+
update_dependency_helm_chart(chart_path)
107+
108+
print(f"Building chart...")
109+
package_helm_chart(chart_path)
110+
111+
print(f"Pushing chart to registry...")
112+
push_to_artifactory(
113+
chart_path,
114+
repository_name,
115+
chart_name,
116+
chart_version,
117+
api_key,
118+
artifactory_url,
119+
)
120+
121+
122+
if __name__ == "__main__":
123+
parser = argparse.ArgumentParser(
124+
description="Process Helm charts and push to Artifactory"
125+
)
126+
parser.add_argument(
127+
"--chart-folder",
128+
help="Path to the root directory containing Helm charts", required=True,
129+
)
130+
parser.add_argument(
131+
"--repository-name", help="Name of the Artifactory repository", required=True
132+
)
133+
parser.add_argument(
134+
"--artifactory-url", help="URL of the Artifactory instance", required=True
135+
)
136+
parser.add_argument(
137+
"--api-key", help="API key for Artifactory access", required=True
138+
)
139+
parser.add_argument(
140+
"--override-charts", action="store_true", help="Override existing charts in Artifactory"
141+
)
142+
143+
args = parser.parse_args()
144+
145+
chart_folder = args.chart_folder
146+
repository_name = args.repository_name
147+
artifactory_url = args.artifactory_url
148+
api_key = args.api_key
149+
override_charts = args.override_charts
150+
151+
process_charts(
152+
chart_folder,
153+
repository_name,
154+
artifactory_url,
155+
api_key,
156+
override_charts
157+
)

0 commit comments

Comments
 (0)