diff --git a/createHelmYaml.sh b/createHelmYaml.sh index 4ac2c14..2b67787 100644 --- a/createHelmYaml.sh +++ b/createHelmYaml.sh @@ -1,48 +1,5 @@ #!/bin/bash -# Function to create HelmChart YAML -createHelmYaml() { - # Load values from Chart.yaml - chart_version=$(yq -r '.dependencies[0].version' Chart.yaml) - chart_name=$(yq -r '.name' Chart.yaml) - chart_repo=$(yq -r '.dependencies[0].repository' Chart.yaml) - chart_namespace=$(yq -r '.annotations.namespace' Chart.yaml) - - # Check if chart_namespace is empty and default to chart_name if it is - if [[ -z "$chart_namespace" ]]; then - chart_namespace="$chart_name" - fi - - # Determine the correct chart and repo values based on chart_repo - if [[ $chart_repo == oci://* ]]; then - chart="${chart_repo}/${chart_name}" - else - repo="$chart_repo" - chart="$chart_name" - fi - - # Load the raw content of values.yaml - - values_content=$(cat values.yaml | yq -r ".$chart_name") - - # Create HelmChart content in a variable - helm_chart_yaml=$(yq eval -n -r ".apiVersion = \"helm.cattle.io/v1\" | - .kind = \"HelmChart\" | - .metadata.name = \"$chart_name\" | - .metadata.namespace = \"$chart_namespace\" | - .spec.chart = \"$chart\" | - .spec.version = \"$chart_version\" | - .spec.targetNamespace = \"$chart_namespace\" | - .spec.valuesContent = \"$values_content\"") - - # If repo is set, add it to the spec - if [[ -n "$repo" ]]; then - helm_chart_yaml=$(echo "$helm_chart_yaml" | yq eval ".spec.repo = \"$repo\"") - fi - - # Print the final YAML to stdout - echo "$helm_chart_yaml" -} function checkKubeConfig(){ echo "# Check if kubeconfig exists..." @@ -62,7 +19,31 @@ function checkKubeConfig(){ exit 1 fi } +function printHelm(){ + helm dependency build . + helm template -g . --set-json="chart_template.subchartData=$(yq '.dependencies[0]' Chart.yaml -ojson | jq -rc)" +} +function deployHelm(){ + checkKubeConfig + printHelm + echo "Deploying in 5 seconds..." + sleep 5 + helm template -g --set-json="chart_template.subchartData=$(yq '.dependencies[0]' Chart.yaml -ojson | jq -rc)" . | kubectl apply -f - + +} + +function packageHelm(){ + if [[ -z "$REPO_SECRET" ]]; then + echo "No credentials..." + false + exit 1 + fi + helm package . + helmPackage="$(yq '.name' Chart.yaml)-$(yq '.version' Chart.yaml)" + curl --user "$REPO_SECRET" -X POST --upload-file ./chart_template-0.0.2.tgz https://git.ties.one/api/packages/public/helm/api/charts + +} # if actions is not set if [[ -z "$action" ]]; then action="print" @@ -78,20 +59,13 @@ fi # Check if the action environment variable is set to 'print' if [[ "$action" == "print" ]]; then - createHelmYaml + printHelm elif [[ "$action" == "deploy" ]]; then - checkKubeConfig - createHelmYaml | kubectl apply -f - -elif [[ "$action" == "helm-print" ]]; then - helm dependency build . - helm template -g . --set-json="chart_template.subchartData=$(yq '.dependencies[0]' Chart.yaml -ojson | jq -rc)" + deployHelm elif [[ "$action" == "helm-deploy" ]]; then - checkKubeConfig - helm dependency build . - helm template -g --set-json="chart_template.subchartData=$(yq '.dependencies[0]' Chart.yaml -ojson | jq -rc)" . - echo "Deploying in 5 seconds..." - sleep 5 - helm template -g --set-json="chart_template.subchartData=$(yq '.dependencies[0]' Chart.yaml -ojson | jq -rc)" . | kubectl apply -f - + deployHelm +elif [[ "$action" == "package" ]]; then + packageHelm else echo "# invalid action" fi