From c0a48353771bdce06bc871d87cf6852762b253f5 Mon Sep 17 00:00:00 2001 From: Vedant Pareek <36420365+dunefro@users.noreply.github.com> Date: Sat, 26 Oct 2024 01:39:35 +0530 Subject: [PATCH] Pushing helm charts to artifactory (#736) * Pushing helm charts to artifactory * error modified --- .../workflows/artifactory-helm-release.yaml | 56 ++++++++++++++++ scripts/artifactory-chart-releaser/main.py | 64 +++++++++++++++++++ .../requirements.txt | 1 + 3 files changed, 121 insertions(+) create mode 100644 .github/workflows/artifactory-helm-release.yaml create mode 100644 scripts/artifactory-chart-releaser/main.py create mode 100644 scripts/artifactory-chart-releaser/requirements.txt diff --git a/.github/workflows/artifactory-helm-release.yaml b/.github/workflows/artifactory-helm-release.yaml new file mode 100644 index 00000000..982a6ba1 --- /dev/null +++ b/.github/workflows/artifactory-helm-release.yaml @@ -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/setup-helm@v4.2.0 + 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 + + \ No newline at end of file diff --git a/scripts/artifactory-chart-releaser/main.py b/scripts/artifactory-chart-releaser/main.py new file mode 100644 index 00000000..611baffb --- /dev/null +++ b/scripts/artifactory-chart-releaser/main.py @@ -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() diff --git a/scripts/artifactory-chart-releaser/requirements.txt b/scripts/artifactory-chart-releaser/requirements.txt new file mode 100644 index 00000000..79377ea6 --- /dev/null +++ b/scripts/artifactory-chart-releaser/requirements.txt @@ -0,0 +1 @@ +PyYAML==6.0.2 \ No newline at end of file