Set Test Suite Parameters Using Environment Variables in CD Pipelines
This use case demonstrates how the KubeRocketCI platform uses environment variables to flexibly configure commands in deployment pipelines. Teams can dynamically adjust parameters 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 instances using the Single-Sign-On approach.
- Developer has
Write
permissions for GitHub repository to merge the code.
Scenarioβ
To implement specific logic in deployment flow 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 with the environment variables usage.
- 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 Flow.
- Create Deployment Flow, Environment, and Set Variables: Create a Deployment Flow, Set up an environment for the deployment, add Autotests as a quality gate to the flow, 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:
-
Create a private repository and copy the chart repository content. Below is the repository structure:
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 -
Clone your private repository to your local machine:
git clone git@github.com:<github_account_name>/variables-case-tekton.git
-
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" -
Install the chart:
helm upgrade --install custom-tekton-chart variables-case-tekton -n krci
Add an 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:
-
Navigate to the
Marketplace
section on the navigation bar to see theMarketplace
overview page. -
Select the component
Simple Spring Boot Application
, open its details window, and clickCreate
from template:
-
Fill in the required fields and click
Create
:- Component name:
spring-boot-app
- Description:
Simple spring boot application
- Git server:
github
- Repository name:
<github_account_name>/spring-boot-app
- Codebase versioning type:
edp
- Start version from:
0.1.0
- Suffix:
SNAPSHOT
- Component name:
-
Click the Go to application button.
-
Click on Branches and click the Trigger build PipelineRun button:
- Wait until the build is successful:
Add the Autotestsβ
Now you need to create template autotests that will be executed when deploying application:
-
To add an autotest, navigate to the Components section on the navigation bar and click + Create component.
-
Once clicked, the Create new component dialog will appear, then select Autotest and click Next:
- Choose Clone project strategy and click the CREATE button:
- Fill in the following fields in Add components info and click the NEXT button to proceed:
- RepoUrl:
https://github.com/KubeRocketCI/demo-autotests-gradle
- Git server:
github
- Repository name:
<github_account_name>/autotests
- Component name:
autotest
- Description:
simple autotests gradle
Specify the autotest language properties:
- Autotest code language:
Java
- Language version/framework:
Java17
- Build Tool:
Maven
- Autotest report framework:
allure
- Fill in the following fields in Specify advanced settings* and click CREATE:
- Default branch:
master
- Codebase versioning type:
edp
- Start version from:
0.1.0
- Suffix:
SNAPSHOT
- Click Close. Now the application and autotest are created:
Create Deployment Flow, Environment, and Set Variablesβ
The next step is to configure deployment flow with appropriate deploy pipeline, application, autotests, and variables:
To utilize and manage various environments through the KubeRocketCI platform, the initial step is to onboard a new GitOps repository.
- Navigate to the
Deployment flows
section and click the Click here to add a repository button:
- Click the + Add GitOps Repository button and fill
<github_account_name>
in the Git repo relative path and click SAVE button:
-
Navigate to the
Deployment flows
section on the navigation bar and click + Create deployment flow. Once clicked, the Create deployment flow dialog will appear. -
Enter
demo-pipeline
and click the NEXT button:
- On the Applications tab, add the
spring-boot-app
application, specify the main branch for it, and click CREATE button and GO TO DEPLOYMENT FLOW:
- Click the CREATE ENVIRONMENT button, define the following values, and click NEXT:
- Cluster:
In cluster
- Stage name:
qa
- Description:
qa
- Trigger type:
Manual
- Pipeline template:
deploy-with-autotests-variables
- Clean pipeline template:
clean
- In the Add quality gates menu, click the + button. Specify the following parameters and click Create:
- Quality gate type:
Autotest
- Step name:
Autotest
- Autotest:
autotest
- Autotest branch:
master
- Click the GO TO ENVIRONMENT button:
- Select the Variables tab. Click the Click here to add a new variable. button:
- Enter
MY_COMMAND
, and set the Value togradle -q hello
. Then, click the SAVE button:
In the Tekton task run-autotests-java-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-java-variables.yaml
apiVersion: tekton.dev/v1
kind: Task
metadata:
name: run-autotests-java-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 stage 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: cd-pipeline-name
type: string
- name: stage-name
type: string
- name: base-image
type: string
steps:
- name: run-autotest
image: "$(params.base-image)"
workingDir: $(workspaces.source.path)
env:
- name: STAGE_NAME
value: $(params.stage-name)
- name: CD_PIPELINE_NAME
value: $(params.cd-pipeline-name)
envFrom:
- configMapRef:
name: $(params.cd-pipeline-name)-$(params.stage-name)
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/.*"'$STAGE_NAME'": "\(.*\)",/\1/p' run.json | awk -F '"' '{print $1}')
fi
- Navigate to the APPLICATIONS tab. Then click CONFIGURE DEPLOY then LATEST button and click START DEPLOY button:
Validateβ
Now it is time to review the pipeline logs to ensure autotests pass successfully and application produces the expected output:
- To find autotests logs, navigate to the PIPELINES section and wait until all pipelines are done:
- Click on autotests-variables-gradle-run-xxxxx, then stage run autotests, and task run autotests in the logs tab. You will see your autotests output: