How to Pass GitLab CI/CD Variables to Kubernetes (AKS) Deployment.yaml

How to Pass GitLab CI/CD Variables to Kubernetes (AKS) Deployment.yaml
In the world of DevOps, the integration of GitLab’s Continuous Integration/Continuous Deployment (CI/CD) with Kubernetes (AKS) has become a game-changer. This blog post will guide you on how to pass GitLab CI/CD variables to Kubernetes (AKS) deployment.yaml. This is a crucial step in automating your deployment processes, ensuring a seamless and efficient workflow.
Prerequisites
Before we dive in, ensure you have the following:
- A GitLab account with a project set up.
- A Kubernetes cluster on Azure Kubernetes Service (AKS).
- Basic understanding of GitLab CI/CD, Kubernetes, and YAML.
Step 1: Define Your GitLab CI/CD Variables
In your GitLab project, navigate to Settings > CI/CD > Variables
. Here, you can define your variables. These could be your Docker image name, Kubernetes namespace, or any other data you want to pass to your Kubernetes deployment.
DOCKER_IMAGE: "my-app-image"
K8S_NAMESPACE: "my-app-namespace"
Step 2: Create Your .gitlab-ci.yml File
The .gitlab-ci.yml
file is where you define your CI/CD pipeline. In this file, you’ll specify the stages of your pipeline and the jobs that run at each stage. Here’s a basic example:
stages:
- build
- deploy
build:
stage: build
script:
- echo "Building the Docker image..."
- docker build -t $DOCKER_IMAGE .
deploy:
stage: deploy
script:
- echo "Deploying to Kubernetes..."
- kubectl apply -f deployment.yaml
Step 3: Modify Your deployment.yaml File
In your deployment.yaml
file, you’ll need to use Kubernetes' env
field to pass in your GitLab CI/CD variables. Here’s how you can do it:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
template:
spec:
containers:
- name: my-app
image: $DOCKER_IMAGE
env:
- name: DOCKER_IMAGE
valueFrom:
fieldRef:
fieldPath: spec.containers[0].image
- name: K8S_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
Step 4: Use envsubst to Substitute Variables
The envsubst
command-line tool can substitute the values of environment variables. In your .gitlab-ci.yml
file, use envsubst
to replace the variables in your deployment.yaml
file before applying it:
deploy:
stage: deploy
script:
- echo "Deploying to Kubernetes..."
- envsubst < deployment.yaml | kubectl apply -f -
Conclusion
By following these steps, you can successfully pass GitLab CI/CD variables to your Kubernetes (AKS) deployment.yaml. This integration not only automates your deployment processes but also enhances the efficiency of your DevOps workflow.
Remember, the key to successful DevOps is continuous learning and adaptation. So, keep exploring, keep learning, and keep innovating!
I hope this guide was helpful. If you have any questions or need further clarification, feel free to leave a comment below. Happy coding!
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.