Skip to main content
Automate the injection of environment-specific values into your Kubernetes YAML files by replacing tokens at build time. In this guide, you’ll configure a GitHub Actions workflow that:
  1. Authenticates to your cluster using kubeconfig.
  2. Dynamically fetches the ingress controller’s external IP.
  3. Uses the cschleiden/replace-tokens action to swap placeholders in manifests with repository variables and environment variables.
  4. Verifies the final YAML before applying it.

1. Kubernetes Manifests with Tokens

Your manifests include placeholders for namespace, replicas, container image, and ingress IP: Deployment (kubernetes/development/deployment.yaml):
Service (kubernetes/development/service.yaml):
Ingress (kubernetes/development/ingress.yaml):
Before deploying, these tokens must be replaced with actual values.

2. Define Repository Variables

In GitHub, go to Settings → Secrets and variables → Actions → Variables and add the following:
The image shows a GitHub settings page where a new action variable is being created, with "NAMESPACE" as the name and "develop" as the value.
The image shows a GitHub repository settings page, specifically the "Secrets and variables" section under "Actions," displaying environment and repository variables.
Repository variables are exposed as ${{ vars.VARIABLE_NAME }} in workflows. Use secrets for sensitive data like credentials.

3. Select a Token Replacement Action

From the GitHub Marketplace, choose an action for token substitution. We recommend:
  • cschleiden/replace-tokens@v1
    • Customizable tokenPrefix and tokenSuffix
    • Supports multiple files
The image shows a GitHub Marketplace search results page for "replace tokens" under the Actions category, listing various actions available for automating token replacement in files.

4. Configure the GitHub Actions Workflow

Create or update .github/workflows/deploy.yaml:
Key steps:
  • Save Nginx Ingress IP
    Captures the external IP and exposes it as env.INGRESS_IP for later steps.
  • Replace tokens
    Sets tokenPrefix and tokenSuffix to {_ and _}, then injects the variables into your YAML files.
The image shows a GitHub repository settings page, specifically the "Actions" section under "Secrets and variables," displaying a list of repository variables such as DOCKERHUB_USERNAME and MONGO_USERNAME.

5. Verify and Apply

After pushing to main, check the GitHub Actions dashboard for a successful run. The Replace tokens in manifests step will list updated files, and Verify replaced manifests will print the final YAML:
The image shows a GitHub Actions workflow summary for a project named "solar-system," indicating a successful run with completed jobs like unit testing, code coverage, containerization, and deployment.
Example of the replaced deployment.yaml:

References

Watch Video