以Bitnami为例

git clone https://github.com/bitnami/charts.git
cd charts/bitnami
helm dep build redis
helm package redis --destination ../repo
cd ../repo/
helm repo index . --url https://charts.xxxx.com/charts
cp *.tgz /usr/share/nginx/html/charts/
cp *.yaml /usr/share/nginx/html/charts/

完整脚本

#!/bin/sh
# 设置当任意一行命令执行失败时退出脚本
set -e

# 参数校验和使用提示
if [ $# -ne 2 ]; then
    echo "Usage: $0 <module> <version>"
    echo "Example: $0 mysql 8.13.4"
    exit 1
fi

module=$1
version=$2

echo "Starting to build Helm chart for module: ${module}, version: ${version}"

# 切换到Helm charts目录,如果失败则退出脚本
cd /helm/charts || exit

# 获取最新的远程分支和标签信息
git fetch --tags origin

# 检查指定的模块和版本是否存在
if ! git show-ref --verify --quiet "refs/tags/${module}/${version}"; then
    echo "Error: Tag ${module}/${version} does not exist."
    exit 1
fi

# 切换到指定的分支或标签
git checkout "${module}/${version}"

# 构建Helm Chart依赖
helm dep build bitnami/"${module}" || {
    echo "Error: Failed to build dependencies for ${module}."
    exit 1
}

# 打包Chart
helm package bitnami/"${module}" --destination /helm/repo || {
    echo "Error: Failed to package the chart."
    exit 1
}

echo "Chart packaging completed."

# 切换到仓库目录
cd /helm/repo/ || exit

# 生成仓库索引
helm repo index . --url https://charts.xxxx.com || {
    echo "Error: Failed to generate repository index."
    exit 1
}

echo "Repository index generated successfully."

exit 0