The lack of proper integration testing in CI/CDs can lead to halted deployments which can then lead to disrupted development experiences. Therefore, integration testing is crucial as it ensures that individual components work well within the entire live system.
One of the popular tools for building CI/CD pipelines to integrate and deploy your applications is GitHub Actions. They not only integrate with GitHub repositories but also provide great flexibility to build custom workflows. By defining workflows in simple YAML files, Github Actions automates tasks such as code compilation, testing, and deployment. Read more about GitHub actions.
In this blog post, we’ll show how you can leverage GitHub Actions along with Testkube to run tests as part of your CI/CD pipeline in your Kubernetes cluster environments.
Testkube is a Kubernetes native testing framework designed to execute and orchestrate your testing workflows inside your K8s clusters. It allows you to bring your chosen testing tool, integrate it out of the box with Testkube, and schedule all of the tests into your Kubernetes cluster, while providing all the visibility into your tests and flexibility when building and executing these tests from your CI/CD.
Irrespective of the CI/CD tool you’re using, Testkube provides you with multiple integration options:
Integrating Testkube into your CI/CD pipeline simplifies and expedites the testing process, offering a suite of benefits. Without Testkube, testing becomes complex and time-consuming, hindering the effective utilization of Kubernetes resources. Below are some of the benefits of using Testkube in CI/CD pipelines:
Now that we know the benefits of using Testkube in CI/CD pipelines, let us see how we can use GitHub actions with Testkube for a seamless testing experience.
As discussed earlier, GitHub Actions have become one of the most popular ways by which developers create workflows and CI/CD pipelines. To understand how Testkube integrates with GitHub actions, we’ve designed a small workflow.
We have a GitHub repository configured with a GitHub action that triggers on every new PR that is created. Within the action, we leverage Testkube’s GitHub plugin to run and execute tests from the repository on a target Kubernetes cluster that is configured as an environment on Testkube Cloud.. Depending on the result of the test, the PR will be blocked from merging if the test fails or allowed to merge if the test passes.
Once the prerequisites are in place, you should have a target Kubernetes cluster ready with a Testkube agent configured on it. You should also have a GitHub repository with the test files as well as the GitHub workflow configured.
Note: To create the API token, refer to our API Token document. To find the Org ID and environment IDs, simply login to your Teskube Cloud dashboard, and copy them from the URL which would be something like this: https://cloud.testkube.io/organization/tkcorg_abcdefghijklmnopq/environment/tkcenv_1234567890/dashboard
Save all of these as secrets in your GitHub repository, we will reference these in the workflow file that we create. All the required files are present in this GitHub repository.
The first step is to create a test. We’re creating a simple k6 test that will check the response time for a website.
```js
import http from 'k6/http';
import { check, sleep } from 'k6';
export let options = {
vus: 1, // Virtual Users
duration: '10s', // Duration of the test
};
export default function () {
// Send an HTTP GET request to the website
let response = http.get('https://testkube.io'); // Replace with your website URL
// Check if the response time is less than 1000 milliseconds
check(response, {
'Response time is less than 1000 milliseconds': (r) => r.timings.duration < 1000, // Check if response time is less than 1000 milliseconds
});
// Add a sleep period (in this case, 1 second) between requests
sleep(1);
}
```
The above test checks for the response time and, if it is less than 1 second, it fails the test, else it passes it. This code can be found here.
After the test is created, we need to create a new Github action and configure the workflow file. In the main.yaml file in the workflow folder, add the following code:
```yaml
name: Running tests with Testkube
on:
pull_request:
jobs:
job_1:
name: Testkube K6 Workflow For EKS Cluster
runs-on: ubuntu-latest
steps:
- name: Configure AWS cluster
uses: kubeshop/setup-testkube@v1
with:
organization: ${{ secrets.TESTKUBE_ORG_ID }}
environment: ${{ secrets.TESTKUBE_ENV_ID }}
token: ${{ secrets.TESTKUBE_API_TOKEN }}
- run: |
testkube create test --name k6-kubeshop-test --type k6/script --test-content-type git-file --git-uri https://github.com/techmaharaj/testkube-GH.git --git-branch main --git-path k6/testkube.js –update
testkube run test k6-kubeshop-test -f
```
The above workflow does the following things:
Save the workflow file. With this, our basic setup is complete and we can test.
To check if everything is working correctly, raise a new PR in the repository with some changes. You’ll see that your action is triggered almost immediately.
In the background, our test will be running on the target Kubernetes cluster specified.
If the test passes, our GitHub action will update the status in the PR an a user can merge the PR.
However, if the test fails, GitHub action will update the workflow status as failed in the PR. You can see that merging is blocked because of the branch protection rule that we have created which prevents merge if the test fails.
With this example, we have seen how easy it is to configure Testkube with GitHub actions. One can perform advanced scenarios by tweaking the workflow and configuring your repositories based on your requirements.
In this blog post, we looked at the importance of integration testing in CI/CD pipelines and we also learned how Testkube integrates with GitHub actions, which is one of the popular tools for designing CI/CD workflows. We created a simple workflow and were able to integrate a k6 test using Testkube.
Refer to our documentation on other ways to leverage Testkube for your CI/CD needs and try Testkube today.
Try out the various test types in Testkube and witness firsthand how Testkube simplifies and empowers your testing process with its Kubernetes-native capabilities. Join our Slack community for guidance and support.
Testkube is a test execution and orchestration framework for Kubernetes that works with any CI/CD system and testing tool you need, empowering teams to deliver on the promise of agile, efficient, and comprehensive testing programs by leveraging all the capabilities of K8s to eliminate CI/CD bottlenecks, perfecting your testing workflow. Get started with Testkube's free trial today!
Related topics: