Skip to content
You're viewing an older version of this GitHub Action. Do you want to see the latest version instead?
swdotcom

GitHub Action

Update and apply kubernetes configuration files

v1.0.0

Update and apply kubernetes configuration files

swdotcom

Update and apply kubernetes configuration files

Update kubernetes configuration yamls and apply them to the current cluster

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Update and apply kubernetes configuration files

uses: swdotcom/[email protected]

Learn more about this action in swdotcom/update-and-apply-kubernetes-configs

Choose a version

Test Branch

Update and Apply Kubernetes Configurations

Composite Github action to update k8 configurations with environment variables and then apply them with kubectl.

This requires that kubectl be installed and configured. Github's hosted runners already have kubectl installed. If you're using StrongDM to manage access to your k8 cluster, then checkout configure-kubectl-with-strongdm.

Usage

See action.yaml

Example usage

Given kubernetes configurations that are committed to the codebase.

k8/deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
  labels:
    app: my-app
  annotations:
    kubernetes.io/change-cause: ${CHANGE_CAUSE}
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: api
        image: xxxxxxxxxxxx.dkr.ecr.us-west-2.amazonaws.com/unicorn/my-app:${IMAGE_TAG}
        ports:
        - containerPort: 80

k8/config-staging.yaml

kind: ConfigMap
apiVersion: v1
metadata:
  name: my-app
  namespace: default
data:
  APP_ENV: 'staging'
  SOME_CONFIG: 'hello'

This action uses envsubst to replace values in form $VARIABLE or ${VARIABLE} that are found in the list of configuration files. If an ENV variable is not defined for a replacement, then it will be replaced with an empty string. The k8 templates do not need to define any replacements, like the k8/config-staging.yaml above. It's still useful to apply these during the deployment so that configuration can be managed through git.

The order that you list the files for k8-config-file-paths is the order that they will be applied to your cluster. So, you probably want to apply any configMap updates before applying the deployment.

The files will be applied to kubectl's current-context, so make sure to set it to the correct one before this action runs.

- name: Switch to staging cluster
  run: kubectl config use-context staging
- uses: swdotcom/update-and-apply-kubernetes-configs@v1
  with:
    k8-config-file-paths: |
      k8/config-staging.yaml
      k8/deployment.yaml
  env:
    IMAGE_TAG: ${{ github.sha }}
    CHANGE_CAUSE: ${{ github.event.release.tag_name }}

Config files can also be defined on one line with a space delimiter.

- name: Switch to staging cluster
  run: kubectl config use-context staging
- uses: swdotcom/update-and-apply-kubernetes-configs@v1
  with:
    k8-config-file-paths: k8/config-staging.yaml k8/deployment.yaml
  env:
    IMAGE_TAG: ${{ github.sha }}
    CHANGE_CAUSE: ${{ github.event.release.tag_name }}