Skip to main content
Version: 3.14-dev

Set Test Suite Parameters Using Environment Variables in CD Pipelines

This use case demonstrates how KubeRocketCI uses environment variables to flexibly configure commands in deployment pipelines. Teams can dynamically adjust parameters by adding, modifying, or removing them without changing the test suite code. This provides precise control over quality gates, streamlines workflows, and allows pipelines to adapt to different environments or requirements while maintaining efficiency and quality standards.

Goals​

  • Flexible and convenient preparation and validation of the testing environment using environment variables.
  • The ability to manage environment variables by overriding, adding, or removing their values and keys to configure test parameters.
  • A streamlined setup process that allows teams to focus on development without platform constraints.

Preconditions​

  • KubeRocketCI instance with GitHub is configured.
  • Developer has access to the KubeRocketCI instance using the Single-Sign-On approach.
  • Developer has Write permissions for GitHub repository to merge the code.

Scenario​

To implement specific logic in deployment quality gate within the KubeRocketCI platform, follow these steps:

  • Fork the Tekton Chart: Fork, clone and deploy the prepared Tekton chart that implements the required Tekton resources using environment variables.
  • Add the Application: Add a simple application from the platform marketplace and build the application to prepare it for deployment.
  • Add the Autotests: Set up the autotests with the clone strategy to include them in the Deployment.
  • Create Deployment, Environment, and Set Variables: Create a Deployment, Set up an environment for the deployment, add Autotests as a quality gate to the deployment, and define the variable.
  • Validate: Review the pipeline logs to ensure autotests execute correctly and produce the expected output.

Fork the Tekton Chart​

The first step is to clone and install the Tekton chart:

  1. In GitHub, create a private repository.

  2. Clone your private repository to your local machine:

Clone repo

git clone git@github.com:<github_account_name>/variables-case-tekton.git
  1. Copy the chart repository content to your newly created repository. The repository structure should be as follows:

    Repository structure
    variables-case-tekton
    .
    β”œβ”€β”€ Chart.lock
    β”œβ”€β”€ Chart.yaml
    β”œβ”€β”€ README.md
    β”œβ”€β”€ chart_schema.yaml
    β”œβ”€β”€ ct.yaml
    β”œβ”€β”€ lintconf.yaml
    β”œβ”€β”€ templates
    β”‚ β”œβ”€β”€ pipelines
    β”‚ β”‚ └── cd
    β”‚ β”‚ β”œβ”€β”€ autotest-variables.yaml
    β”‚ β”‚ └── deploy-with-autotests-variables.yaml
    β”‚ β”œβ”€β”€ tasks
    β”‚ β”‚ └── cd
    β”‚ β”‚ β”œβ”€β”€ init-autotests-variables.yaml
    β”‚ β”‚ └── run-autotests-variables-java.yaml
    β”‚ └── triggers
    β”‚ └── cd
    β”‚ └── deploy-with-autotests-variables.yaml
    └── values.yaml
  2. Update the component configuration in the values.yaml file:

    # Replace `example.domain.com` with the appropriate wildcard domain for your setup.
    dnsWildCard: "example.domain.com"
  3. Install the chart:

    cd variables-case-tekton
    helm upgrade --install custom-tekton-chart . -n krci

Add Application​

As soon as the Helm chart is installed, the new deploy pipeline will appear in the Environment configuration window. The next step is to create an application from a template:

  1. Create a new Project with the Application type using the Create strategy. Select the Projects section and click + Create project:

    Projects tab

  2. Select the Select ready template option, Simple Spring Boot Application and click Continue:

    Initial setup

  3. On the Git & project info tab, define the following values and click Continue:

    • Git server: github
    • Owner: GitHub_username
    • Repository name: spring-boot-app
    • Default branch: main
    • Project name: spring-boot-app
    • Description: Simple spring boot application

Application creation

  1. On the Build config tab, define the values and click Continue:

    • Codebase versioning type: semver
    • Start version from: 0.1.0
    • Suffix: SNAPSHOT

Application creation

  1. On the Review tab, verify the project configuration and click Create project:

    Review and create

  2. On the congratulations menu, click Open project:

    Ready project

  3. Select the Branches tab and click the Build button:

Trigger build

  1. Click the PipelineRun name to view its details:

Click PipelineRun name

  1. Wait until the build is successful and its status is Completed:

PipelineRun completed

Add Autotests​

Now you need to create template autotests that will be executed when deploying the application:

  1. To add an autotest, navigate to the Projects section on the navigation bar and click + Create project.

  2. On the Create new project dialog, select Custom configuration -> Autotest -> Clone and click Continue:

Autotests project

  1. On the Git & project info tab, define the following values and click Continue:
  • RepoUrl: https://github.com/KubeRocketCI/demo-autotests-gradle
  • Git server: github
  • Owner: GitHub_username
  • Repository name: autotests
  • Default branch: master
  • Project name: autotests
  • Description: Simple autotests gradle

Autotests codebase

  1. On the Build config tab, define the values and click Continue:
  • Autotest code language: Java
  • Language version/framework: Java17
  • Build tool: Gradle
  • Autotest report framework: allure
  • Codebase versioning type: semver
  • Start version from: 0.1.0
  • Suffix: SNAPSHOT

Autotests codebase

  1. On the Review tab, verify the project configuration and click Create project:

Autotests codebase advanced settings

  1. On the congratulations menu, click View all projects.

  2. Click Close. Now the application and autotest are created:

Projects overview

Create Deployment, Environment, and Set Variables​

The next step is to configure a Deployment with appropriate deploy pipeline, application, autotests, and variables:

note

To utilize and manage various environments through the KubeRocketCI platform, the initial step is to onboard a new GitOps repository.

  1. Navigate to the Deployments section and click the Click here to add a repository button:

    Deployments tab

  2. Click to the + Add GitOps repository:

    Onboard gitops repository

  3. Select the Create strategy and github server, enter GitHub account name and click Save:

    Configure gitops repository

  4. In the KubeRocketCI portal, navigate to Deployments tab and click the + Create deployment button:

    Create deployment

  5. On the Applications tab, add the spring-boot-app application, specify the main branch for it, and click Continue:

    Applications tab

  6. On the Pipeline configuration tab, fill in the required fields and click Continue:

    • Pipeline name: demo-pipeline
    • Description: Deployment with specific variables
    • Deployment type: Container

    Pipeline configuration tab

  7. On the Review tab, verify the configuration and click Create deployment:

    Review deployment

  8. On the congratulations page, select Open deployment:

    Deployment created

  9. On the Deployment details page, click the + Create Environment button:

    Create Environment

  10. On the Basic configuration tab, define the following values and click Continue:

    • Cluster: in-cluster
    • Environment name: qa
    • Description: Testing environment

    Basic configuration tab

  11. On the Pipeline configuration tab, define the following values and click Continue:

    • Trigger type: Manual
    • Pipeline template: deploy-with-autotests-variables
    • Clean pipeline template: clean

    Pipeline configuration

  12. On the Quality gates tab, click the pencil icon to edit the first quality gate. Specify the following parameters and click Update quality gate and then Continue:

    • Quality gate type: Autotest
    • Autotest: autotests
    • Step name: Autotest
    • Autotest branch: master

    Add quality gates

  13. On the Review tab, verify the Environment configuration and click Create environment:

    Environment details

  14. Click the Open environment button:

    Open Environment

  15. Select the Variables tab and click the Add variable button:

    Add variables

  16. Enter MY_COMMAND, and set the Value to gradle -q hello. Then, click the Save button:

    Set variable

    In the Tekton task run-autotests-gradle-variables, we have implemented logic to check for the MY_COMMAND variable. If it exists, the task uses the value of this variable as the command. If it is missing or empty, the task falls back to using the logic defined in the run.json file from the autotests repository.

run-autotests-gradle-variables.yaml
apiVersion: tekton.dev/v1
kind: Task
metadata:
name: run-autotests-gradle-variables
labels:
app.kubernetes.io/based-on: "0.2"
{{- include "edp-tekton.labels" $ | nindent 4 }}
annotations:
tekton.dev/pipelines.minVersion: "0.12.1"
tekton.dev/categories: Autotest Tools
tekton.dev/tags: autotest-tool
tekton.dev/platforms: "linux/amd64,linux/s390x,linux/ppc64le"
spec:
description: >-
This task runs autotests for a specified Environment using either Maven or Gradle based on the provided configuration, extracting and executing the relevant commands from a `run.json` file.
workspaces:
- name: source
description: A workspace that contains the repository.
params:
- name: DEPLOYMENT_FLOW
type: string
- name: ENVIRONMENT
type: string
- name: base-image
type: string
steps:
- name: run-autotest
image: "$(params.base-image)"
workingDir: $(workspaces.source.path)
env:
- name: ENVIRONMENT
value: $(params.ENVIRONMENT)
- name: DEPLOYMENT_FLOW
value: $(params.DEPLOYMENT_FLOW)
envFrom:
- configMapRef:
name: $(params.DEPLOYMENT_FLOW)-$(params.ENVIRONMENT)
script: |
#!/bin/bash

set -exo pipefail

# Check if the MY_COMMAND variable is set and not empty
if [[ -n "${MY_COMMAND}" ]]; then
# If MY_COMMAND is defined, execute its content
eval "${MY_COMMAND}"
else
# If MY_COMMAND is not set, execute the existing logic
$(sed -n 's/.*"'$ENVIRONMENT'": "\(.*\)",/\1/p' run.json | awk -F '"' '{print $1}')
fi
  1. Navigate to the Applications tab. Then click Configure deploy -> Latest -> Start deploy:

    Deploy application

Validate​

Now it is time to review the pipeline logs to ensure autotests pass successfully and application produces the expected output:

  1. To find autotests logs, navigate to CI/CD Pipelines -> PipelineRuns and wait until all pipelines are done:

    Pipelines list

  2. Click on the autotests-variables-gradle-run-xxxxx PipelineRun, select the run autotests stage and the run autotests task in the logs page. You will see your autotests output:

    Autotests logs