Harness is a powerful Continuous Delivery (CD) platform that enables teams to simplify their deployment workflows. One of its key features is the ability to use variables, which make your pipelines dynamic, flexible, and maintainable. In this blog, we’ll explore how to define and use variables in Harness pipelines, with a real-time example for better understanding.
Why Use Variables in Harness?
Variables allow you to:
Avoid Hardcoding: Manage dynamic values like environment names, versions, and configurations.
Reuse Pipelines: Adapt pipelines to different environments and applications without duplicating logic.
Enhance Maintainability: Centralize control over values, making pipelines easier to update.
Harness supports several types of variables:
Pipeline Variables: Defined at the pipeline level and referenced anywhere in the pipeline.
Stage Variables: Specific to a particular stage.
Step Variables: Scoped to individual steps within a stage.
Built-in Variables: Predefined by Harness, like
${
stage.name
}
or${
env.name
}
.
Real-Time Example: Deploying a Microservice
Let’s walk through a real-world scenario. Suppose you’re deploying a microservice called my-service
to environments such as Dev, Staging, and Prod, with different configurations for each environment.
Step 1: Define Pipeline Variables
Navigate to your pipeline in Harness and define the following variables:
ENVIRONMENT
: Specifies the target environment.SERVICE_VERSION
: The version of the service to deploy.REPLICAS
: The number of pods for the service.
Your pipeline YAML will look like this:
variables:
- name: ENVIRONMENT
type: String
default: "dev"
- name: SERVICE_VERSION
type: String
default: "1.0.0"
- name: REPLICAS
type: Number
default: 2
Step 2: Configure Deployment Steps
In the Deploy Stage, configure the deployment steps to use these variables.
Example: Helm Deployment Step
steps:
- step:
name: Deploy My-Service
type: K8sRollingDeploy
spec:
manifests:
- manifest:
type: Values
spec:
store:
type: Inline
spec:
content: |
environment: ${env.ENVIRONMENT}
serviceVersion: ${pipeline.variables.SERVICE_VERSION}
replicas: ${pipeline.variables.REPLICAS}
In this example:
The
${pipeline.variables.ENVIRONMENT}
dynamically injects the environment (e.g., dev, staging, or prod).The
${pipeline.variables.SERVICE_VERSION}
provides the application version to deploy.The
${pipeline.variables.REPLICAS}
sets the number of pods.
Step 3: Override Variables at Runtime
Harness allows you to override variable values during runtime. When triggering the pipeline, specify different values for the variables.
Example inputs:
ENVIRONMENT: staging
SERVICE_VERSION: 1.1.0
REPLICAS: 3
This results in a deployment to the Staging environment with version 1.1.0
and three pods.
Key Benefits
Dynamic Configurations: Use a single pipeline for multiple environments and deployment scenarios.
Simplified Maintenance: Update variables in one place rather than modifying the pipeline logic.
Error Reduction: Reduce the risk of errors caused by manual hardcoding.
Enhanced Flexibility: Easily add or modify environments, versions, and configurations.
Tips for Effective Use of Variables
Plan Ahead: Define all necessary variables at the start to avoid rework.
Document Variables: Include descriptions and examples to make pipelines understandable for the team.
Use Naming Conventions: Follow consistent naming patterns (e.g.,
ENV_
,SERVICE_
) for clarity.Secure Sensitive Variables: Use Harness secrets manager for variables containing sensitive information.
Conclusion
By leveraging variables in Harness pipelines, you can create robust, flexible, and reusable deployment workflows. Whether you’re deploying to multiple environments or managing complex configurations, variables provide a clean and efficient way to streamline your processes.
I hope this guide helps you master variables in Harness. Feel free to share your experiences or ask questions in the comments! 😊