Provide a means of dynamically setting .Chart.AppVersion during install or upgrade without editing Chart.yaml

2 min read 05-10-2024
Provide a means of dynamically setting .Chart.AppVersion during install or upgrade without editing Chart.yaml


Dynamically Setting Helm Chart AppVersion During Install or Upgrade

The Problem:

Many Helm charts use the appVersion field in Chart.yaml to specify the version of the application being deployed. However, this approach can lead to inconsistencies when the application's version needs to be updated dynamically during installation or upgrade. This is especially challenging when the version information is stored in external systems or generated at runtime.

Rephrased:

Imagine you have a web application deployed using Helm. You want its version to match the version of your codebase, which might be managed in a separate system. Manually updating Chart.yaml every time your codebase changes is tedious and error-prone. Wouldn't it be great if you could have Helm automatically determine the app version dynamically?

Scenario and Original Code:

Let's assume you have a simple Helm chart for a web application with the following Chart.yaml:

apiVersion: v2
name: my-web-app
version: 1.0.0
appVersion: "1.0.0"

During installation, the appVersion will be set to "1.0.0" based on the Chart.yaml.

Dynamic AppVersion Solution:

To dynamically set the appVersion during installation or upgrade, we can leverage Helm's --set option. Here's how:

  1. Extract the App Version: Implement a mechanism to extract the desired appVersion from your codebase, external system, or any other relevant source. This might involve using a build pipeline, environment variables, or a dedicated API.

  2. Pass the App Version to Helm: During installation or upgrade, use the --set flag to pass the dynamically retrieved appVersion to the chart:

    helm install my-web-app my-chart --set appVersion="1.2.3" 
    

    Replace "1.2.3" with the dynamically extracted version.

  3. Update the Chart Template: In the chart's templates (e.g., values.yaml), use the {{ .Values.appVersion }} placeholder to access the dynamically set appVersion. This placeholder will substitute the provided value during rendering.

Example:

Let's say you're using Git tags to represent application versions. You can use a values.yaml snippet like this:

appVersion: "{{ .Values.gitTag | default "latest" }}"

Then, during installation, you can set the gitTag value:

helm install my-web-app my-chart --set gitTag=v1.2.3

This will dynamically set the appVersion to "1.2.3" in your chart.

Additional Considerations:

  • Versioning Strategy: It's essential to have a clear versioning strategy in place. You need to determine how application versions are managed and how they relate to your Helm chart versions.

  • Security: Be mindful of security implications when using dynamic values. Ensure that the source of the appVersion is trustworthy and properly validated.

Benefits:

  • Consistency: Dynamically setting appVersion ensures it always reflects the actual application version, eliminating discrepancies.

  • Flexibility: It allows you to integrate with different versioning systems and mechanisms.

  • Maintainability: Reduces the need to manually update Chart.yaml, simplifying your release process.

Conclusion:

Dynamically setting the appVersion in Helm charts provides a more robust and flexible approach to version management. This method allows you to maintain accurate version information, integrate seamlessly with your existing workflow, and streamline your deployment processes.