Testkube Jenkins Pipelines
The Testkube Jenkins integration enables the execution of any Testkube CLI command within Jenkins pipelines. This integration is compatible with both commercial and open source deployments of Testkube, providing a robust and flexible framework for test execution and automation.
There are multiple ways to integrate Testkube with Jenkins depending on your setup:
- Jenkins on Kubernetes: Use the
kubeshop/testkube-cliDocker image as a container in your pod template. - Jenkins on VMs: Install the Testkube CLI directly on your Jenkins agents.
- Jenkins CLI Plugin: Use the Testkube CLI Jenkins Plugin to automatically install and configure the CLI.
Prerequisites
To use Jenkins CI/CD with Testkube, you need to create an API token. Then, pass the organization and environment IDs, along with the token and other parameters specific for your use case.
If a test is already created, you can run it using the command testkube run testworkflow TEST_WORKFLOW_NAME -f. However, if you need to create a test in this workflow, please add a creation command, e.g.: testkube create testworkflow -f EXAMPLE_FILE.yaml.
Jenkins on Kubernetes
If your Jenkins instance runs on Kubernetes (using the Kubernetes plugin), you can use the kubeshop/testkube-cli Docker image directly in your pipeline. This avoids the need to install the CLI binary during the pipeline run.
The pod template requires securityContext.runAsUser: 1000 so that both the JNLP agent and the Testkube container share the same UID and can read/write the shared workspace volume. Additionally, the HOME environment variable must be set to a writable path (e.g., /home/jenkins/agent) so the CLI can store its configuration.
Testkube (Cloud)
pipeline {
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
spec:
securityContext:
runAsUser: 1000
containers:
- name: testkube
image: kubeshop/testkube-cli:<version>
command: ['sleep']
args: ['infinity']
env:
- name: HOME
value: /home/jenkins/agent
'''
}
}
environment {
TK_ORG = credentials("TK_ORG")
TK_ENV = credentials("TK_ENV")
TK_API_TOKEN = credentials("TK_API_TOKEN")
}
stages {
stage('Testing') {
steps {
container('testkube') {
sh 'testkube set context --api-key $TK_API_TOKEN --org-id $TK_ORG --env-id $TK_ENV'
sh 'testkube run testworkflow TEST_WORKFLOW_NAME -f'
}
}
}
}
}
Replace <version> with the desired Testkube CLI version (e.g., 2.7.1).
Testkube (Self-Hosted)
For self-hosted Testkube instances running in the same cluster, point --api-uri-override to the in-cluster API service:
pipeline {
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
spec:
securityContext:
runAsUser: 1000
containers:
- name: testkube
image: kubeshop/testkube-cli:<version>
command: ['sleep']
args: ['infinity']
env:
- name: HOME
value: /home/jenkins/agent
'''
}
}
environment {
TK_ORG = credentials("TK_ORG")
TK_ENV = credentials("TK_ENV")
TK_API_TOKEN = credentials("TK_API_TOKEN")
TK_URL = 'https://api.mydomain:443'
}
stages {
stage('Testing') {
steps {
container('testkube') {
sh 'testkube set context --api-key $TK_API_TOKEN --org-id $TK_ORG --env-id $TK_ENV --api-uri-override $TK_URL'
sh 'testkube run testworkflow TEST_WORKFLOW_NAME -f'
}
}
}
}
}
Jenkins on VMs
If your Jenkins agents run on virtual machines (or bare metal), you can install the Testkube CLI directly during the pipeline execution. This approach downloads the CLI binary and uses it to configure the context and run tests.
Testkube (Cloud)
pipeline {
agent any
environment {
TK_ORG = credentials("TK_ORG")
TK_ENV = credentials("TK_ENV")
TK_API_TOKEN = credentials("TK_API_TOKEN")
}
stages {
stage('Setup and Run') {
steps {
sh '''
# Install the Testkube CLI
curl -sSLf https://get.testkube.io | sh
# Configure the CLI context
testkube set context --api-key $TK_API_TOKEN --org-id $TK_ORG --env-id $TK_ENV
# Run a Test Workflow
testkube run testworkflow TEST_WORKFLOW_NAME -f
'''
}
}
}
}
Testkube with EKS (Amazon Elastic Kubernetes Service)
This workflow establishes a connection to an EKS cluster and runs a Test Workflow using the Testkube CLI. Please make sure that the following points are satisfied:
- The AwsAccessKeyId, AwsSecretAccessKeyId secrets should contain your AWS IAM keys with proper permissions to connect to the EKS cluster.
- The AwsRegion secret should contain the AWS region where EKS is deployed.
- The EksClusterName secret points to the name of the EKS cluster you want to connect to.
pipeline {
agent any
environment {
TK_ORG = credentials("TK_ORG")
TK_ENV = credentials("TK_ENV")
TK_API_TOKEN = credentials("TK_API_TOKEN")
}
stages {
stage('Setup and Run') {
steps {
script {
// Setting up AWS credentials
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'AwsAccessKeyId']]) {
sh 'aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID'
sh 'aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY'
sh 'aws configure set region $AWS_REGION'
}
// Updating kubeconfig for EKS
withCredentials([string(credentialsId: 'EksClusterName', variable: 'EKS_CLUSTER_NAME')]) {
sh 'aws eks update-kubeconfig --name $EKS_CLUSTER_NAME --region $AWS_REGION'
}
// Install the Testkube CLI
sh 'curl -sSLf https://get.testkube.io | sh'
// Configure the CLI context
sh 'testkube set context --api-key $TK_API_TOKEN --org-id $TK_ORG --env-id $TK_ENV'
// Run a Test Workflow
sh 'testkube run testworkflow TEST_WORKFLOW_NAME -f'
}
}
}
}
}
Testkube with GKE (Google Kubernetes Engine)
This example connects to a Kubernetes cluster in Google Cloud and runs a Test Workflow using the Testkube CLI. Please make sure that the following points are satisfied:
- The GKE Service Account should be created prior in Google Cloud and added to Jenkins credentials along with GKE Project value.
- The GKE Cluster Name and GKE Zone can be added as credentials in Jenkins.
pipeline {
agent any
environment {
TK_ORG = credentials("TK_ORG")
TK_ENV = credentials("TK_ENV")
TK_API_TOKEN = credentials("TK_API_TOKEN")
}
stages {
stage('Setup and Run') {
steps {
script {
// Activating GKE service account
withCredentials([file(credentialsId: 'GKE_SA_KEY', variable: 'GKE_SA_KEY_FILE')]) {
sh 'gcloud auth activate-service-account --key-file=$GKE_SA_KEY_FILE'
}
// Setting GCP project
withCredentials([string(credentialsId: 'GKE_PROJECT', variable: 'GKE_PROJECT')]) {
sh 'gcloud config set project $GKE_PROJECT'
}
// Configure Docker with gcloud as a credential helper
sh 'gcloud --quiet auth configure-docker'
// Get GKE cluster credentials
withCredentials([
string(credentialsId: 'GKE_CLUSTER_NAME', variable: 'GKE_CLUSTER_NAME'),
string(credentialsId: 'GKE_ZONE', variable: 'GKE_ZONE')
]) {
sh 'gcloud container clusters get-credentials $GKE_CLUSTER_NAME --zone $GKE_ZONE'
}
// Install the Testkube CLI
sh 'curl -sSLf https://get.testkube.io | sh'
// Configure the CLI context
sh 'testkube set context --api-key $TK_API_TOKEN --org-id $TK_ORG --env-id $TK_ENV'
// Run a Test Workflow
sh 'testkube run testworkflow TEST_WORKFLOW_NAME -f'
}
}
}
}
post {
always {
// Clean up
sh 'rm -f $GKE_SA_KEY_FILE'
}
}
}
Testkube CLI Jenkins Plugin
Alternatively, you can use the Testkube CLI Jenkins Plugin, which automatically downloads, installs, and configures the Testkube CLI within your pipeline.
Install the plugin by searching for it in the "Available Plugins" section on Jenkins, or using the following URL: https://plugins.jenkins.io/testkube-cli
Testkube (Cloud)
pipeline {
agent any
environment {
TK_ORG = credentials("TK_ORG")
TK_ENV = credentials("TK_ENV")
TK_API_TOKEN = credentials("TK_API_TOKEN")
}
stages {
stage('Example') {
steps {
script {
// Setup the Testkube CLI
setupTestkube()
// Run testkube commands
sh 'testkube run testworkflow TEST_WORKFLOW_NAME -f'
}
}
}
}
}
Testkube Open Source
pipeline {
agent any
environment {
TK_NAMESPACE = 'custom-testkube-namespace'
}
stages {
stage('Example') {
steps {
script {
setupTestkube()
sh 'testkube run testworkflow TEST_WORKFLOW_NAME'
}
}
}
}
}
The steps to connect to your Kubernetes cluster differ for each provider. You should check the docs of your Cloud provider for how to connect to the Kubernetes cluster from Jenkins CI/CD.