Skip to main content

GitHub Actions

In order to automate Testkube runs, access to a K8s cluster is needed. For example, a configured environment with the set up context and kubeconfig for communication with the K8s cluster.

Testkube uses your K8s context and access settings in order to interact with the cluster and tests resources, etc.

In the next few sections, we will go through the process of Testkube and Helm (for Testkube's release deploy/upgrade) automations with the usage of GitHub Actions and GKE K8s.

Testkube GitHub Action

The testkube GitHub Action is available here and it enables running the Testkube CLI commands in a GitHub workflow.

The following example shows how to create a test using the GitHub action; a more complex example can be found here.

# Creating test
- name: Create test
id: create_test
uses: kubeshop/testkube-docker-action@v1
command: create
resource: test
namespace: testkube
parameters: "--type k6/script --name testkube-github-action"
stdin: "import http from 'k6/http';\nimport { sleep,check } from 'k6';\n\nexport default function () {\n const baseURI = `${__ENV.TESTKUBE_HOMEPAGE_URI || ''}`\n check(http.get(`${baseURI}/`), {\n 'check testkube homepage home page': (r) =>\n r.body.includes('Your Friendly Cloud-Native Testing Framework for Kubernetes'),\n });\n\n\n sleep(1);\n}\n"

Configuring Your GH Actions for Access to GKE

To obtain set up access to a GKE (Google Kubernetes Engine) from GH (GitHub) actions, please visit the official documentation from GH:

  1. Create a Service Account (SA).
  2. Save it into GH's Secrets of the repository.
  3. Run either Helm or Kubectl kubtest commands against the set up GKE cluster.

Main GH Action Section Configuration

To install on Linux or MacOS, run:

# Deploy into configured GKE cluster:
- name: Deploy
run: |-
helm upgrade --install --atomic --timeout 180s testkube helm-charts/testkube --namespace testkube --create-namespace

In addition to Helm, you can run any other K8s-native command. In our case: testkube...

Complete Example of Working GH Actions Workflow and Testkube Tests Usage

Testkube tests can be easily re-used with minimal modifications according to your needs.

To run tests on Linux or MacOS:

name: Running Testkube Tests.

- "charts/**"
- main

PROJECT_ID: ${{ secrets.GKE_PROJECT }}
GKE_CLUSTER_NAME_DEV: ${{ secrets.GKE_CLUSTER_NAME_DEV }} # Add your cluster name here.
GKE_ZONE_DEV: ${{ secrets.GKE_ZONE_DEV }} # Add your cluster zone here.
DEPLOYMENT_NAME: testkube # Add your deployment name here.

name: Deploy
runs-on: ubuntu-latest
needs: notify_slack_if_release_succeeds
- name: Checkout
uses: actions/checkout@v2
fetch-depth: 0

- name: Configure Git
run: |
git config "$GITHUB_ACTOR"
git config "[email protected]"

# Setup gcloud CLI
- uses: google-github-actions/setup-gcloud@94337306dda8180d967a56932ceb4ddcf01edae7
service_account_key: ${{ secrets.GKE_SA_KEY }}
project_id: ${{ secrets.GKE_PROJECT }}

# Configure Docker to use the gcloud command-line tool as a credential
# helper for authentication
- run: |-
gcloud --quiet auth configure-docker

# Get the GKE credentials so we can deploy to the cluster
- uses: google-github-actions/get-gke-credentials@fb08709ba27618c31c09e014e1d8364b02e5042e
cluster_name: ${{ env.GKE_CLUSTER_NAME_DEV }}
location: ${{ env.GKE_ZONE_DEV }}
credentials: ${{ secrets.GKE_SA_KEY }}

# Run Testkube test on a GKE cluster
- name: Run test
id: run_test
uses: kubeshop/testkube-docker-action@v1
command: run
resource: test
parameters: TEST_NAME

Along with the kubectl command, you can pass all the standard K8s parameters such as --namespace, etc.

If you wish to automate the CI/CD part of Testkube's Helm release, use Helm blocks as follow:

# ...
- name: Install Helm
uses: azure/setup-helm@v1
version: v3.4.0

- name: Installing repositories
run: |
helm repo add kubeshop
helm repo add bitnami

# Run Helm delpoy/upgrade of the Testkube release on a GKE cluster
- name: Deploy
run: |-
helm upgrade --install --atomic --timeout 180s testkube kubeshop/testkube --namespace testkube --create-namespace