Automating Helm Chart Builds and Deployment with GitLab CI/CD

Robin Janke
3 min readJan 10, 2024

--

In the Kubernetes ecosystem, efficiently managing and deploying applications is key. Helm charts have emerged as a standard for defining, installing, and upgrading Kubernetes applications. However, managing these charts can be cumbersome. This is where GitLab CI/CD shines, offering automation for building and deploying Helm charts. In this article, I’ll guide you through setting up a GitLab CI/CD pipeline for building Helm Docker images and deploying Helm charts.

Setting Up the Pipeline

Our GitLab CI/CD pipeline is divided into two primary stages: building the Helm Docker image and building and deploying Helm charts.

1. Defining Stages and Variables

We begin by defining the stages and some variables in our .gitlab-ci.yml file:

stages:
- build_helm_image
- build_and_deploy_charts
variables:
CHARTS_DIR: "charts"
PACKAGE_PATH: "packages"

2. Building the Helm Docker Image

The first stage builds a Docker image containing Helm, which will be used in the subsequent stages.

build_helm_image:
stage: build_helm_image
image: docker:19.03.12
services:
- docker:19.03.12-dind
script:
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.gitlab.com
- docker build -t registry.gitlab.com/your-namespace/your-repo .
- docker push registry.gitlab.com/your-namespace/your-repo
only:
changes:
- Dockerfile
tags:
- docker

Replace your-namespace/your-repo with your GitLab namespace and repository path.

3. The Dockerfile

To build our Helm Docker image, we use the following Dockerfile:

# Use an official lightweight base image
FROM alpine:3.12

# Set the desired Helm version
ENV HELM_VERSION v3.2.4

# Install necessary packages
RUN apk add --no-cache curl ca-certificates bash

# Install Helm
RUN curl -L https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz | tar zxv -C /tmp \
&& mv /tmp/linux-amd64/helm /usr/bin/helm \
&& chmod +x /usr/bin/helm \
&& rm -rf /tmp/linux-amd64
# Set the working directory
WORKDIR /apps
# Default to bash
CMD ["bash"]

4. Building and Deploying Helm Charts

The second stage packages each Helm chart in the charts directory and uploads them to the GitLab Helm repository.

build_and_deploy_charts:
stage: build_and_deploy_charts
image: registry.gitlab.com/your-namespace/your-repo:latest
script:
- mkdir -p ${PACKAGE_PATH}
- |
for dir in ${CHARTS_DIR}/*; do
if [ -d "$dir" ]; then
CHART_NAME=$(basename $dir)
helm package $dir --destination ${PACKAGE_PATH}
mv ${PACKAGE_PATH}/*.tgz ${PACKAGE_PATH}/${CHART_NAME}.tgz
fi
done
- |
for chart in ${PACKAGE_PATH}/*.tgz; do
echo "Uploading $chart..."
if [ -f "$chart" ]; then
curl --request POST --form "chart=@$chart" --user gitlab-ci-token:$CI_JOB_TOKEN "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/helm/api/stable/charts"
fi
echo "Done"
done
only:
- main
tags:
- docker

Conclusion

This setup ensures that every time you push changes to your Helm charts or the Dockerfile, GitLab CI/CD will automatically build and deploy your charts. This automation streamlines the management of Kubernetes applications, making it more efficient and less prone to errors.

Remember to replace placeholders with your specific project details. This setup is a basic example and can be further customized to suit more complex workflows and requirements.

This draft now provides a comprehensive guide, including the Dockerfile, for setting up a GitLab CI/CD pipeline for Helm chart management. Feel free to adjust the content to better fit your style and the specifics of your project.

--

--