How to host helm charts with gitlab package registry

Pinterest LinkedIn Tumblr

Helm helps you manage Kubernetes applications — Helm Charts help you define, install, and upgrade even the most complex Kubernetes application. Helm charts are a collection of Kubernetes YAML manifests distributed as a single package. They make it faster and easier to deploy complex applications in your cluster. Using GitLab as a ledger allows your team to store charts with your code in an organization-managed repository.

Also check:

Using the Helm GitLab repository

Make sure your GitLab instance has been updated to version 14.1. If you are using the GitLab.com hosted deployment, you will already have everything you need.

Switch over to your GitLab instance. Either create a new project or find an existing one. Using the left sidebar, navigate to Packages & Registries > Package Registry within your project. If you don’t see these menus, head to Settings > General, expand the “Visibility, project features, permissions” heading, and enable the “Packages” toggle.

Creating hem chart

To create a helm chart, use the helm create command.

➜ helm create nginx
Creating nginx

Helm create a directory with the name you supplied. The directory contains the templates and chart definition. Inspect using this command:

➜ tree nginx                             
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

3 directories, 10 files

Helm creates a starter chart directory along with the common files and directories used in a chart with NGINX as an example. We can install this into our Kubernetes cluster:

$ helm install nginx .

Package distribution

To distribute helm chart, it has to be hosted in a repo. You can build the helm chart into a compressed file for ease of distribution using this command:

➜ helm package nginx 
Successfully packaged chart and saved it to: /tmp/helm/nginx-0.1.0.tgz

The command creates a new tar.gz archive ready to upload. Before doing so, you can inspect the archive with the tar command to verify its content.

➜ tar ztf nginx-0.1.0.tgz

Publish a package in Gitlab Package registry

For this, you need a username and an access token. To get it, head over to user settings in gitlab then access tokens. Create a new access token with api access. You can generate a new token by clicking your profile icon in the top-right of GitLab’s interface. Select “Preferences” from the menu, then “Access Tokens” from the left sidebar. Add a new access token with the api scope. The write_repository scope might look correct but won’t actually work; it only facilitates access using Git over HTTP, whereas Helm requires API integration. Checkout more information here.

Once built, a chart can be uploaded to the desired channel with curl or helm cm-push:

With curl:

curl --request POST \
    --form '[email protected]' \
    --user <username>:<access_token> \
  • <username>: the GitLab username or the deploy token username.
    • <access_token>: the personal access token or the deploy token.
    • <project_id>: the project ID (like 42) or the URL-encoded path of the project (like group%2Fproject).
    • <channel>: the name of the channel (like stable).

This is the output on my server

➜ curl --request POST \
    --form '[email protected]' \
    --user api-token:glpat-xxxxxxxxxxxxx \
{"message":"201 Created"}                                 

With the helm cm-push plugin. For this you will need the plugin installed. Use this command:

helm plugin install https://github.com/chartmuseum/helm-push

 Then you need to register it with your Helm client to begin with. Use this command:

helm repo add --username <username> --password <access_token> citizix \
helm cm-push mychart-0.1.0.tgz citizix
  • <username>: the GitLab username or the deploy token username.
    • <access_token>: the personal access token or the deploy token.
    • <project_id>: the project ID (like 42).
    • <channel>: the name of the channel (like stable).

This is it in my machine

➜ helm repo add --username api-token --password glpat-xxxxx citizix https://gitlab.com/api/v4/projects/34000000/packages/helm/stable

"citizix" has been added to your repositories

Then push

➜ helm cm-push nginx-0.1.0.tgz citizix

Pushing nginx-0.1.0.tgz to citizix...


➜ helm search repo citizix
citizix/nginx	0.1.0        	1.16.0     	A Helm chart for Kubernetes

Using Charts in the Repository

With the chart in your repository, you can deploy it into your Kubernetes cluster. helm install commands should work straightaway:

# load the contents of repositories
helm repo update

# install the chart
helm install citizix/nginx

This will start a new chart deployment into your active cluster. Use the --kubeconfig and --namespace Helm flags if necessary to select an appropriate Kubernetes configuration file and specify the namespace to deploy into.

Accessing GitLab’s Repository in a CI Pipeline

As with its other package formats, GitLab’s Helm repository benefits from built-in integration with the software’s CI/CD system. Your pipelines will receive preset environment variables which simplify the chart publication process. You don’t need to set any custom CI variables to configure authentication.

Here’s an example pipeline which creates a chart and publishes it to your repository:

  - helm-publish

  stage: helm-publish
    name: alpine/helm:latest
    entrypoint: [""]
    - helm repo add --username $CI_REGISTRY_USER --password $CI_REGISTRY_PASSWORD my-repo $CI_REGISTRY
    - helm package my-chart
    - helm push my-chart.tgz my-repo

The $CI_REGISTRY variables are configured to let you push new packages into the registry.

I am a Devops Engineer, but I would describe myself as a Tech Enthusiast who is a fan of Open Source, Linux, Automations, Cloud and Virtualization. I love learning and exploring new things so I blog in my free time about Devops related stuff, Linux, Automations and Open Source software. I can also code in Python and Golang.

Write A Comment