Skip to main content
In this guide, you’ll learn how to set up the kubectl CLI within your GitHub Actions workflow to deploy to Kubernetes. By the end, you’ll have a reusable dev-deploy job that checks out your repo, installs kubectl, verifies the cluster, and is ready for your deployment steps.

1. Current Workflow Overview

Below is the existing workflow trigger and environment setup. We trigger on pushes to main and feature branches, and we expose MongoDB credentials via environment variables.

2. Adding the dev-deploy Job

We’ll append a new job named dev-deploy. It runs after the docker job on ubuntu-latest and prepares kubectl for deployment in the Development namespace.

3. Installing the kubectl CLI

Instead of manually downloading binaries, leverage the verified [azure/setup-kubectl@v3] action from the GitHub Marketplace.
The image shows a GitHub Marketplace search results page for "kubectl," displaying various actions related to kubectl with their descriptions and star ratings.
Using the official azure/setup-kubectl action ensures you get signed binaries and can pin a specific version for reproducible builds.

4. Fetching Kubernetes Cluster Details

After installing, verify connectivity by checking the client version and listing all nodes:

5. Full dev-deploy Job Snippet

Here’s the complete YAML for the dev-deploy job:

5.1 Step-by-Step Summary

6. Workflow Execution

When you push these changes, GitHub Actions will execute unit-testing, code-coverage, and docker first. Once they succeed, dev-deploy runs.
The image shows a GitHub Actions page for a project called "Solar System Workflow," displaying a list of workflow runs with their statuses and details.
The image shows a GitHub Actions workflow in progress, with steps for unit testing, code coverage, containerization, and deployment. The workflow is titled "installing kubectl" and includes a visual representation of the job sequence.

7. Troubleshooting Connection Errors

If you encounter:
that indicates kubectl has no valid context because no kubeconfig is set.
You must provide a kubeconfig file via secrets or variables and configure it in your workflow. Without it, kubectl cannot authenticate to your cluster.

Example kubeconfig

To integrate this in GitHub Actions, store the kubeconfig as a secret and load it before your kubectl steps.

Watch Video