Skip to main content
In this guide, you’ll learn how to parameterize Kubernetes YAML manifests and dynamically inject environment-specific values during CI/CD. By leveraging envsubst, you can maintain a single set of manifests for development, staging, and production.

Prerequisites

Ensure you have the following installed locally:

1. Manifest Files with Placeholders

Your repository structure should contain a single kubernetes/manifest/ folder with:
  • deployment.yaml
  • ingress.yaml
  • service.yaml
Each file uses Bash-style placeholders (${VAR}) to be substituted in CI/CD.

kubernetes/manifest/deployment.yaml

kubernetes/manifest/ingress.yaml

kubernetes/manifest/service.yaml

2. Placeholder Reference Table

3. Defining CI/CD Environment Variables

Configure global variables in your .gitlab-ci.yml under variables: or via the [GitLab UI][GitLab CI/CD Variables].
Add non-masked variables (NAMESPACE, REPLICAS) in CI/CD → Variables:
The image shows a GitLab CI/CD settings page with a list of environment variables, including keys like DEV_KUBE_CONFIG, DOCKER_PASSWORD, and REPLICAS, all masked for security.
Never commit secrets (e.g., database credentials) directly in your YAML files. Always use masked or protected CI/CD variables.

4. Retrieving the Ingress Controller IP

At runtime, fetch the external IP of your Ingress controller:
You can learn more about the Ingress resource in the Kubernetes Ingress documentation.

5. CI Job: dev-deploy

The dev-deploy job below installs kubectl and envsubst (from GNU gettext), exports INGRESS_IP, and replaces placeholders in your manifests.
Commit and push to trigger the pipeline. You’ll see a successful dev-deploy stage:
The image shows a GitLab pipeline interface for a NodeJS project named "Solar System," indicating a successful pipeline run with a job labeled "dev-deploy."

6. Verifying the Logs

Key sections in the job logs:
Here you can confirm all four placeholders (NAMESPACE, REPLICAS, K8S_IMAGE, INGRESS_IP) have been correctly injected.
You’re now ready to apply templated manifests across multiple environments, ensuring consistency and reusability.

References

Watch Video