Home Replace Helm Chart Variables in your CI/CD Pipeline with Tokenizer
Post
Cancel

Replace Helm Chart Variables in your CI/CD Pipeline with Tokenizer

Helm is a great tool to deploy your application into Kubernetes. In my post, Helm - Getting Started, I also mentioned the values.yaml file which can be used to replace variables in the Helm chart. The problem with this approach is that the values.yaml file is hard-coded.

In this post, I want to introduce Tokenizer which is a simple Azure DevOps extension with which you can replace variables in the values.yaml file.

This post is part of “Microservice Series - From Zero to Hero”.

Why would I replace variables in my Helm Charts?

Currently the values.yaml file looks as follows:

As you can see, the replica count or the tag is hard-coded as 1 and latest respectively. One replica might be fine for my test environment but definitely not for my production environment. In my post Automatically Version Docker Containers in Azure DevOps CI, I talked about the disadvantages of using the latest tag and that it would be better to use a specific version number. Though, this version number is changing with every build and therefore needs to be inserted automatically.

Another use case of dynamic variables would be the connection string to your database. The connection string will be different for each of your environments.

Install Tokenizer in Azure DevOps

You can download the Tokenizer extension for free from the Marketplace. To download the extension, open the page of the extension in the marketplace and click on Get it free.

Get the Tokenizer extension

Get the Tokenizer extension

This opens a new page where you can either select your Azure DevOps Services organization to install it or download the extension if you want to install it on an Azure DevOps server.

Install the Tokenizer extension

Install the Tokenizer extension

This extension looks for variables starting and ending with a double underscore, for example, __MyVariable__ and replaces it with the value of the variable MyVariable.

Add the Tokenizer Task to the Azure DevOps Pipeline

You can find the code of the demo on GitHub.

Add the following task before the HelmInstall task to your pipeline:

Note that I am using templates in my pipeline and added the task to the HelmInstall.yaml file. You can find more information about templates in Improve Azure DevOps YAML Pipelines with Templates. These templates also use parameters. The task looks as follows:

It is the same code as above except that the source file pattern is passed as a parameter.

All files matching the sourceFilesPattern will be searched for tokens to be replaced. In my post Helm - Getting Started, I talked about overriding Helm chart values using the values.yaml file. For now, all I want to update is the repository and tag variable of the values.yaml file:

Here I want to replace the repository with the ImageName variable and the tag with the BuildNumber variable.

Testing the Tokenizer

I have the following variables in my pipeline:

The GitVersion variable sets the version number. You can read more in my post Automatically Version Docker Containers in Azure DevOps CI. Run the pipeline and then check what image got deployed. You can use kubectl or a dashboard to check if the right image got loaded. For more information about using a dashboard, see my post Azure Kubernetes Service - Getting Started.

The correct image got loaded

The correct image got loaded

Conclusion

Automatically replacing configuration values for different environments is crucial. This post showed how to use the Tokenizer extension and how to easily replace values in your CI/CD pipeline.

You can find the code of the demo on GitHub.

This post is part of “Microservice Series - From Zero to Hero”.

This post is licensed under CC BY 4.0 by the author.

Auto-scale in Kubernetes using the Horizontal Pod Autoscaler

Use a Database with a Microservice running in Kubernetes

Comments powered by Disqus.