Sobes.tech
Middle

How to properly store Helm templates in GitLab?

sobes.tech AI

Answer from AI

It is recommended to store Helm templates in a separate Git repository in GitLab. This allows versioning, applying CI/CD pipelines for linting and packaging, and access management.

The repository structure can look like this:

my-helm-charts/
├── Chart.yaml          // Chart metadata
├── values.yaml         // Default values
├── templates/          // Kubernetes manifest templates
│   ├── deployment.yaml
│   ├── ingress.yaml
│   └── service.yaml
└── charts/             // Dependent charts (optional)
    └── redis/
        └── ...

For automation, you can use GitLab CI/CD. Example .gitlab-ci.yml:

stages:
  - lint
  - package
  - deploy # Optional

variables:
  HELM_VERSION: "3.x.x" # Specify the current Helm version
  HELM_REGISTRY: "my-gitlab-registry" # Your registry name in GitLab

before_script:
  - curl https://get.helm.sh/helm-v${HELM_VERSION}-linux-amd64.tar.gz | tar xz
  - mv linux-amd64/helm /usr/local/bin/helm
  - helm version

lint_chart:
  stage: lint
  image: docker.io/alpine/helm:latest # Use an image with Helm
  script:
    - helm lint ./my-chart-directory # Path to your chart directory

package_chart:
  stage: package
  image: docker.io/alpine/helm:latest
  script:
    - helm package ./my-chart-directory # Create a .tgz archive
  artifacts:
    paths:
      - ./*.tgz # Save the packaged chart as an artifact

publish_chart:
  stage: package
  image: docker.io/alpine/helm:latest
  script:
    # Configure Helm registry in GitLab
    - HE=helm-registry.$CI_SERVER_HOST helm registry login $CI_REGISTRY -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD
    - helm push my-chart-*.tgz oci://$CI_REGISTRY/$CI_PROJECT_NAMESPACE/$HELM_REGISTRY # Push to OCI registry
  variables:
    # Ensure CI_REGISTRY_USER and CI_REGISTRY_PASSWORD variables are set in GitLab CI/CD
    GIT_STRATEGY: none # Do not clone the repository again

# Optional deployment stage
# deploy_chart:
#   stage: deploy
#   image: alpine/helm:latest
#   script:
#     # Setup dependencies (e.g., kubectl, cloud providers)
#     # helm upgrade --install my-release oci://$CI_REGISTRY/$CI_PROJECT_NAMESPACE/$HELM_REGISTRY/my-chart --version latest -n my-namespace -f values.yaml
#   environment: production # Or other environment
#   when: manual # Or on_branch, etc.

For storing packaged charts (.tgz), you can use:

  • GitLab Package Registry (Helm OCI charts): The recommended way. Allows storing charts in OCI format directly in GitLab.
  • Generic Packages: If Helm OCI is not supported or for other reasons.
  • Private static file storage: For example, S3-compatible storage or GitLab Pages.

When using GitLab Package Registry (Helm OCI), to add it as a Helm repository:

helm registry login registry.gitlab.com
helm repo add my-gitlab-charts oci://registry.gitlab.com/<your-namespace>/<your-helm-chart-project>

Replace <your-namespace> and <your-helm-chart-project> with the appropriate values.