0
0
KubernetesHow-ToBeginner · 4 min read

How to Use Helm with CI/CD for Kubernetes Deployments

Use helm commands within your CI/CD pipeline scripts to package, lint, and deploy Kubernetes applications automatically. Integrate Helm with tools like GitHub Actions, GitLab CI, or Jenkins by running helm install or helm upgrade commands after building and testing your code.
📐

Syntax

The basic Helm commands used in CI/CD pipelines include:

  • helm lint [chart]: Checks the chart for errors.
  • helm package [chart]: Packages the chart into a versioned archive.
  • helm install [release] [chart]: Installs a new release of the chart.
  • helm upgrade [release] [chart]: Upgrades an existing release.
  • helm repo add [name] [url]: Adds a Helm chart repository.

These commands are scripted in CI/CD pipelines to automate deployment steps.

bash
helm lint ./mychart
helm package ./mychart
helm repo add stable https://charts.helm.sh/stable
helm install myapp ./mychart-0.1.0.tgz
helm upgrade myapp ./mychart-0.1.0.tgz
💻

Example

This example shows a GitHub Actions workflow that uses Helm to deploy a Kubernetes app after code push.

yaml
name: Deploy with Helm

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Kubernetes
        uses: azure/setup-kubectl@v3
        with:
          version: 'latest'

      - name: Set up Helm
        uses: azure/setup-helm@v3
        with:
          version: 'v3.11.0'

      - name: Helm lint
        run: helm lint ./charts/myapp

      - name: Helm upgrade or install
        run: |
          helm upgrade --install myapp ./charts/myapp \
          --namespace default \
          --set image.tag=${{ github.sha }}
Output
Run helm lint ./charts/myapp ==> Linting ./charts/myapp 1 chart(s) linted, 0 chart(s) failed Run helm upgrade --install myapp ./charts/myapp --namespace default --set image.tag=abc123def456 Release "myapp" has been upgraded. Happy Helming!
⚠️

Common Pitfalls

Common mistakes when using Helm in CI/CD include:

  • Not setting the Kubernetes context correctly before running Helm commands.
  • Using helm install without checking if the release exists, causing failures on repeated runs.
  • Not managing Helm chart versions or dependencies properly.
  • Failing to pass dynamic values like image tags, leading to stale deployments.

Always use helm upgrade --install to handle new and existing releases smoothly.

bash
## Wrong way (fails if release exists):
helm install myapp ./charts/myapp

## Right way (safe for CI/CD):
helm upgrade --install myapp ./charts/myapp --namespace default --set image.tag=latest
📊

Quick Reference

CommandPurposeNotes
helm lint ./chartCheck chart for errorsRun before packaging or deploying
helm package ./chartCreate chart archiveUse for versioned releases
helm repo add name urlAdd chart repositoryNeeded to fetch charts
helm install release chartInstall new releaseFails if release exists
helm upgrade --install release chartInstall or upgrade releaseRecommended for CI/CD
helm uninstall releaseRemove releaseClean up deployments

Key Takeaways

Use helm upgrade --install to safely deploy or update releases in CI/CD pipelines.
Always set Kubernetes context and credentials before running Helm commands.
Lint and package Helm charts to catch errors early in the pipeline.
Pass dynamic values like image tags to Helm to deploy the correct app version.
Integrate Helm commands into CI/CD tools like GitHub Actions or Jenkins for automation.