Skip to content

Commit

Permalink
Pushing helm charts to artifactory (#736)
Browse files Browse the repository at this point in the history
* Pushing helm charts to artifactory

* error modified
  • Loading branch information
dunefro authored Oct 25, 2024
1 parent 78a846d commit c0a4835
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/artifactory-helm-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Release Charts

on:
push:
branches:
- main

env:
ARTIFACTORY_REGISTRY: ${{ secrets.TRUEFOUNDRY_ARTIFACTORY_REGISTRY_URL }}
ARTIFACTORY_USERNAME: ${{ secrets.TRUEFOUNDRY_ARTIFACTORY_PUBLIC_USERNAME }}
ARTIFACTORY_PASSWORD: ${{ secrets.TRUEFOUNDRY_ARTIFACTORY_PUBLIC_PASSWORD }}
ARTIFACTORY_REPOSITORY_URL: ${{ secrets.TRUEFOUNDRY_ARTIFACTORY_PUBLIC_HELM_REGISTRY }}

jobs:
release:

runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Helm
uses: azure/[email protected]
with:
version: v3.14.0

- name: Add helm dependencies and update
run: |
helm repo add grafana https://grafana.github.io/helm-charts
helm repo add clickhouse-operator https://docs.altinity.com/clickhouse-operator
helm repo add nvidia https://helm.ngc.nvidia.com/nvidia
helm repo add dcgm-exporter https://nvidia.github.io/dcgm-exporter/helm-charts
helm repo add truefoundry https://truefoundry.github.io/infra-charts
helm repo add nats https://nats-io.github.io/k8s/helm/charts/
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
- name: Helm registry login
run: |
helm registry login ${{ env.ARTIFACTORY_REGISTRY }} -u ${{ env.ARTIFACTORY_USERNAME }} -p ${{ env.ARTIFACTORY_PASSWORD }}
- uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install python dependencies
run: |
pip install -r scripts/artifactory-chart-releaser/requirements.txt
- name: Publish helm charts
run: |
python scripts/artifactory-chart-releaser/main.py
64 changes: 64 additions & 0 deletions scripts/artifactory-chart-releaser/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import os
import subprocess
import yaml

ARTIFACTORY_REPOSITORY_URL = os.getenv("ARTIFACTORY_REPOSITORY_URL")

def get_chart_info(chart_dir):
"""Extract chart name and version from Chart.yaml"""
with open(os.path.join(chart_dir, "Chart.yaml"), "r") as file:
chart_yaml = yaml.safe_load(file)
chart_name = chart_yaml.get("name")
chart_version = chart_yaml.get("version")
return chart_name, chart_version

def helm_chart_exists(chart_name, chart_version):
"""Check if a Helm chart exists in the repository"""
try:
subprocess.run(
["helm", "pull", f"oci://{ARTIFACTORY_REPOSITORY_URL}/{chart_name}", "--version", chart_version],
check=True,
capture_output=True,
text=True
)
return True
except subprocess.CalledProcessError:
return False

def upload_chart(chart_dir):
"""Upload Helm chart to OCI repository"""
chart_name, chart_version = get_chart_info(chart_dir)
print(f"Uploading chart {chart_name} with version {chart_version}")

if helm_chart_exists(chart_name, chart_version):
print(f"Helm chart {chart_name} with version {chart_version} already exists in {ARTIFACTORY_REPOSITORY_URL}. Skipping...")
return

# Update dependencies, package the chart, and push to the repository
subprocess.run(["helm", "dependency", "update"], check=True, cwd=chart_dir)
subprocess.run(["helm", "package", "."], check=True, cwd=chart_dir)

package_file = f"{chart_name}-{chart_version}.tgz"
try:
subprocess.run(
["helm", "push", package_file, f"oci://{ARTIFACTORY_REPOSITORY_URL}"],
check=True,
cwd=chart_dir
)
print(f"Pushed helm chart {package_file} to the OCI repository oci://{ARTIFACTORY_REPOSITORY_URL}")
except subprocess.CalledProcessError as e:
print(f"Failed to push helm chart {chart_name}: {e}")
exit(1)

def main():
"""Main script to iterate over chart directories and upload charts"""
current_dir = os.getcwd()
charts_dir = os.path.join(current_dir, "charts")
chart_dirs = [os.path.join(charts_dir, d) for d in os.listdir(charts_dir) if os.path.isdir(os.path.join(charts_dir, d))]

for chart_dir in chart_dirs:
print(f"Current directory is {chart_dir}")
upload_chart(chart_dir)

if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions scripts/artifactory-chart-releaser/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PyYAML==6.0.2

0 comments on commit c0a4835

Please sign in to comment.