custom_act_runner/createHelmYaml.sh
ties f1d65ac4c9
All checks were successful
Build docker container / Build image (push) Successful in 13s
Update createHelmYaml.sh
2024-08-29 12:01:40 +00:00

97 lines
2.8 KiB
Bash

#!/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..."
if [[ -n "$KUBECONFIG_DATA" ]]; then
echo "Check if kubeconfig is valid..."
kubectl version
if [ $? -ne 0 ]; then
echo "# Failed to connect to Kubernetes..."
bash createServiceAccount.sh
false
exit 1
fi
else
echo "# No kubeconfig available..."
bash createServiceAccount.sh
false
exit 1
fi
}
# if actions is not set
if [[ -z "$action" ]]; then
action="print"
fi
echo "# Action: $action"
# if KUBECONFIG_DATA is set
if [[ -n "$KUBECONFIG_DATA" ]]; then
mkdir -p /root/.kube
echo "$KUBECONFIG_DATA" > /root/.kube/config
fi
# Check if the action environment variable is set to 'print'
if [[ "$action" == "print" ]]; then
createHelmYaml
elif [[ "$action" == "deploy" ]]; then
checkKubeConfig
createHelmYaml | kubectl apply -f -
elif [[ "$action" == "helm-print" ]]; then
helm dependency build .
helm template -g . --set-json="subchartData=$(yq '.dependencies[0]' Chart.yaml -ojson | jq -rc)"
elif [[ "$action" == "helm-deploy" ]]; then
checkKubeConfig
helm dependency build .
helm template -g --set-json="subchartData=$(yq '.dependencies[0]' Chart.yaml -ojson | jq -rc)" .
echo "Deploying in 5 seconds..."
sleep 5
helm template -g --set-json="subchartData=$(yq '.dependencies[0]' Chart.yaml -ojson | jq -rc)" . | kubectl apply -f -
else
echo "# invalid action"
fi