Re-deploying a Helm Release Using GitLab CI/CD
Prerequisites
A configured GitLab repository with CI/CD enabled.
A Kubernetes cluster with Helm installed.
An existing Helm release deployed.
GitLab Runner configured to interact with the Kubernetes cluster.
Kubernetes and Helm credentials stored in GitLab CI/CD variables.
Instructions
Step 1: Store Kubernetes and Helm Credentials in GitLab
To enable GitLab CI/CD to interact with your Kubernetes cluster and Helm, store the following variables under GitLab > Settings > CI/CD > Variables:
KUBECONFIG
(Base64 encoded Kubernetes config file, if required).HELM_KUBE_CONTEXT
(Kubernetes context name, if using multiple clusters).HELM_RELEASE_NAME
(The name of the deployed Helm release).HELM_NAMESPACE
(The namespace where the release is deployed).
Step 2: Define the .gitlab-ci.yml
Pipeline
Create or update your .gitlab-ci.yml
file to automate the re-deployment process.
stages:
- deploy
variables:
KUBE_CONTEXT: "your-k8s-context"
HELM_CHART_PATH: "./helm-chart"
HELM_RELEASE_NAME: "your-release"
HELM_NAMESPACE: "your-namespace"
before_script:
- echo "Setting up Helm and Kubernetes context"
- kubectl config use-context $KUBE_CONTEXT
- helm version
- kubectl version
deploy:
stage: deploy
image: alpine/k8s:latest
script:
- echo "Deploying Helm release: $HELM_RELEASE_NAME"
- helm upgrade --install $HELM_RELEASE_NAME $HELM_CHART_PATH -n $HELM_NAMESPACE
only:
- main
Step 3: Trigger the Deployment
The deployment will be triggered automatically when changes are pushed to the main
branch. To manually trigger the pipeline:
Go to your GitLab project.
Navigate to CI/CD > Pipelines.
Click Run Pipeline and select the appropriate branch.
Step 4: Verify the Deployment
After the deployment, verify that the Helm release was successfully updated:
helm list -n $HELM_NAMESPACE
Check the running pods and logs:
kubectl get pods -n $HELM_NAMESPACE
kubectl logs -f <pod-name> -n $HELM_NAMESPACE
Managing Changes in Secrets (Environment Variables)
When making updates to secrets (environment variables), follow these steps:
Update AWS Secrets Manager
Go to AWS Secrets Manager.
Add the new secret variable with its corresponding value.
Update Helm Secret Configuration
Modify the Helm files in the following order:
Deployment-specific configuration files.
Environment-specific configuration files.
General configuration files (only if the secret is not found in the previous files).
Push the Changes in GitLab CI/CD
Commit and push the updated Helm configuration files to the repository.
GitLab CI/CD will detect the changes and re-deploy the application automatically.
Troubleshooting
Helm upgrade fails: Ensure the Helm chart path is correct and dependencies are up to date (
helm dependency update
).Kubernetes connection issues: Verify that the Kubernetes context is set properly.
Permission errors: Ensure the GitLab Runner has the necessary permissions to deploy to Kubernetes.
Updates not showing in the new deployment: Ensure the new variables are added to the Helm secret configuration list before pushing the deployment.
Secrets showing out of sync: This can happen if a variable is added to the secrets configuration in Helm but not in AWS Secrets Manager, make sure to do that.