For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to page URLs.
Skip to content

Custom deployments using Deployment Templates

Perform custom deployment in Harness using Deployment Templates to connect to target platforms, obtain target host information, and execute deployment steps.

In some cases, you might be using a platform that does not have direct support in Harness, such as OpenStack, WebLogic, or WebSphere. Harness calls these non-native deployments.

For non-native deployments, Harness provides a custom deployment option using Deployment Templates.

Deployment Templates use shell scripts to connect to target platforms, obtain target host information, and execute deployment steps.

This tutorial walks you through simple Deployment Templates using Kubernetes. Harness includes direct Kubernetes support (go to Kubernetes deployment tutorial), but this tutorial uses Kubernetes because it is a simple way to review Deployment Templates features.

Before you begin

Before you begin, make sure you have the following:

  • Install and register a Harness Delegate that can connect to your target platform. For steps, go to Install a delegate.

  • Have access to a Docker image you want to deploy.

  • Review the Deployment Template limitations on Deployment Template limitations before you start.

What you will learn from this topic

In this tutorial, you will learn:

  • How to install and register a Harness Delegate for Deployment Templates use cases.

  • How to create the Deployment Template.

    • How to add a script to fetch a JSON array containing target instance information.

  • How to create the Deployment Template pipeline.

  • How to add your Docker image to Harness.

  • How to define the pipeline service, infrastructure, and execution.

  • How to run and verify the deployment.

The tutorial should only take about 15 minutes.

Deployment Template setup summary

Here is a summary of the steps for setting up custom deployments using Deployment Templates:

  1. Create a Deployment Template.

    1. In the template, include a script that returns a JSON array containing a list of the target instances where Harness will deploy your artifact.

    2. Identify the array path to the host object in the JSON so Harness can locate these at deployment runtime.

    3. Map any important host attributes that you want to reference later, like IP, region, etc.

  2. Create a Harness Pipeline stage using the Deployment Template type and the Deployment Template.

    1. In your Harness Pipeline stage Service, create a Harness Service that uses the Deployment Template. Add the artifact you want to deploy.

    2. In your Harness Pipeline stage Environment, create a Harness Infrastructure Definition that uses the Deployment Template.

    3. In your Harness Pipeline stage Execution, review the automatically added Fetch Instances step, and move it to wherever you want to execute the script from your Deployment Template.

    4. Add a deployment step to Execution to deploy your artifact to the instances retrieved using the Fetch Instances step.

Your Pipeline fetches the target instances as you requested and deploys your artifact to them.

Deployment Template limitations

Unlike the deployments for supported platforms, like Kubernetes and AWS, Deployment Templates have the following limitations:

Type

Harness Artifact Registry

Nexus3

Artifactory

Jenkins

Amazon S3

Docker Registry

AWS ECR

GCR

ACR

Google Artifact Registry

Custom

GCS

Bamboo

AMI

Azure Artifacts

Docker

✅

✅

✅

✅

✅

✅

✅

✅

✅

✅

Other (ZIP, Jobs, etc.)

✅

✅

✅

✅

✅

✅

✅

✅

✅

  • No steady state checks on deployed services.

  • Harness does not track releases.

You can add your own scripts or tests to your Pipelines to describe deployments, check steady state, and track releases. For example, using the Shell Script or HTTP steps.

Harness Delegate setup

Complete the following steps to set up the Harness Delegate:

  1. Install a Harness Kubernetes Delegate in a cluster. For steps on installing a Delegate, go to Install a delegate.

The Delegate you use for Deployment Templates should be in an environment where it can connect and query your artifact repo and target instances. Typically, you want a Delegate in the same subnet as the target instances.

If your scripts use utilities or software that does not come with the Delegate by default, you can install them on the Delegate manually or using the Delegate INIT_SCRIPT environment variable.

For steps on using INIT_SCRIPT, go to Build custom delegate images with third-party tools.

Harness Delegate installation packages include TAR and cURL. You can use cURL and TAR in your Delegate scripts and Pipeline steps without installing these tools.

Harness Delegate installation packages also include standard tools and SDKs for the main platform integrations Harness uses, such as Kubernetes, Helm, AWS, Azure, GCP. You do not need to install their CLI tools on the Delegate.

Once the Delegate is installed and registers with your Harness account, you will see it listed on the Delegates page.

  1. Note the default tag added to the Delegate. You will use this to select this Delegate in future steps.

Create custom Deployment Template

You can create a Deployment Template at the Account, Org, or Project level. This tutorial uses the Project level.

  1. In your Harness Project, select Project Setup.

  2. Select Templates.

  3. Click New Template.

  4. Select Deployment.

  5. In Create New Deployment Template, in Name, enter DT.

  6. In Version Label, enter v1. In Logo, you can upload an image to use as the icon for this template.

  7. Click Start.

The Deployment Template is composed of two major sections:

  • Infrastructure:

    • Variables: Variables that can be used when the script is run.

    • Fetch Instances Script: Script needed to fetch a JSON array of the target instances. You can enter the script here, or use the Harness File Store to share scripts with others.

    • Instance Object Array Path: The JSON path to the label that lists the array of instances, such as items.

    • Instance Attributes: The JSON path to the instance name label for the target instances. Mapping the field name instancename to the JSON Path is mandatory. You can add more instance attributes for additional target instance values you want to use.

  • Execution:

    • Any steps that you want to use in the stage and associate with this template. If you create these in the Deployment Template, they are automatically created as separate Step Templates.

    • Your stage is not limited to using these steps. You can add any steps you want to your stage.

This tutorial uses only some of these settings. The rest of the settings are described in Additional Deployment Template settings.

Add the Fetch Instance Script

In Fetch Instance Script, you enter the script to pull the JSON collection of target instances from a server or service.

The script is expected to query the server and receive a JSON array containing the target hosts, saved in the file $INSTANCE_OUTPUT_PATH.

The Harness Delegate executes this shell script at runtime on its host. Run the script on the Delegate host first to confirm that the host can connect to your servers.

The script should return a JSON array containing the target host information Harness needs to deploy.

The script you use depends on the platform you are using.

This tutorial deploys to all the Kubernetes pods in the namespace example, so the script looks like this:

If you want, you can replace the namespace harness-delegate-ng with a namespace in your own cluster that has pods that can be fetched.

You can enter the script inline, or use the Harness File Store to store it in a file.

This script returns a JSON array of pods in the namespace, similar to this:

This example uses Kubernetes. Harness already has full, direct support for Kubernetes deployments. This script is used only as an example.

For the main Kubernetes support, go to Kubernetes deployment tutorial.

Next, you need to provide the JSON path to the JSON array object for the target hosts.

Fetch Instances script requirements

Keep the following in mind when you write the Fetch Instances script:

  • The script must return an array of target instances (virtual machine, pod, and so on). If the template is to be used for executing a trigger to an external orchestrator (for example, Ansible, Puppet, etc.), the script should query the orchestrator to return the total instances running inside. If the script returns only the application name, Harness cannot track the actual SI count.

  • If a service scales up or down after deployment, write the Fetch Instances script so that it always returns the current state of the system. This script is run periodically after deployment as part of a perpetual task.

Define the Instance Object Array Path

Complete the following steps to define the Instance Object Array Path:

  1. In Instance Object Array Path, you enter the JSON path to the JSON array object. In the example above, the array is under items.

  2. In Instance Object Array Path, enter items. Harness uses this path to traverse the JSON array.

Define Instance Attributes

After you provide a path to the instances array, you can map any useful JSON keys in Instance Attributes.

You must use instancename to identify the target host(s) in the JSON array.

For this Kubernetes example, use:

  • Field Name: instancename.

  • JSON Path: metadata.name.

You can map any additional attributes containing information you want to reference in your Execution, most likely in a Shell Script step. Go to Referencing fetched instances using expressions.

Add Execution steps

Complete the following steps to add the Execution steps:

  1. After the Deployment Template Infrastructure is complete, click Continue to view Execution.

You can create or select step templates in Execution.

You do not have to use any of these steps in your stage. Execution is a way of associating steps with Deployment Templates.

This tutorial creates a Shell Script step template to deploy the Docker image artifact to the instances fetched using the script added in Fetch Instance Script.

  1. In Deployment Steps, click Add Step, and click Create and Use Template.

  2. In Step Library, select Shell Script.

  3. In Name, enter deploy.

  4. In Script, enter the following:

  1. Click Save.

  2. In Save as new Template, in Name, enter deploy.

  3. In Version Label, enter v1.

  4. Click Save. The step template is added to the Deployment Template.

  5. Click Save to save the Deployment Template. If you have not already, name the Deployment Template DT.

Create the Pipeline

Next, create a Harness Pipeline that uses the Deployment Template.

  1. Select Pipelines.

  2. Click Create a Pipeline.

  3. In Create new Pipeline, enter the name DT Tutorial, and click Start.

  4. Click Add Stage.

  5. Select Deploy, and enter the name DT Tutorial.

  6. In Deployment Templates, select the Deployment Template you created, DT, and then click Use Template.

  7. Click Set Up Stage.

The stage is created and you are on the Service tab.

Next, define the Harness Service using the Deployment Template you created and a Docker artifact for deployment.

If you are new to Harness, learn about the basics in CD overview and key concepts and CD Pipeline modeling overview.

Create the Service

Next, add the artifact for deployment to the Harness Service.

You do not always need to add an artifact to the Harness Service. If the script in your deployment steps pulls in an artifact, or uses a hardcoded artifact in a spec or manifest, you can skip adding the artifact to the Harness Service.

  1. In Select Service, click Add Service.

  2. Name the new Service DT Tutorial.

  3. In Deployment Type, you can see your template is selected.

Next, add the Docker image for deployment.

Add the Docker image to deploy

Complete the following steps to add the Docker image to deploy:

  1. In Artifacts, click Add Primary Artifact.

  2. In Specify Artifact Repository Type, select Docker Registry, and click Continue.

  3. In Docker Registry Repository, click New Docker Registry Repository.

  4. In Overview, enter the name Docker Hub, and click Continue.

  5. In Docker Registry URL, enter the following:

    1. URL https://registry.hub.docker.com/v2/.

    2. In Authentication, select Anonymous, and click Continue.

  6. In Connect to the provider, select Connect through a Harness Delegate, and then click Continue.

  7. In Delegates Setup, select Only use Delegates with all of the following tags, and then enter the tag of the Delegate you set up earlier.

  8. Click Save and Continue.

  9. After the test, click Continue.

  10. In Artifact Details, enter the following:

    • Image Path: library/nginx.

    • Tag: Change the setting to a Fixed value, and then select perl.

  11. Click Submit. The artifact is added to the Service.

  12. Click Save. The Service is added to the stage.

  13. Click Continue to set up the Environment for the stage.

Reference the artifact in your files

Later, in the stage Execution, this tutorial adds a deployment step that downloads and applies a Kubernetes manifest. The manifest is located in a public GitHub repo.

For the manifest's Deployment object to use the artifact added to Harness, the expression <+artifact.image> is added to the image label (image: <+artifact.image>).

You must use <+artifact.image> somewhere in your stage Execution to reference the artifact you added to Harness. You can reference it in a Shell Script step, a spec or manifest, or any other relevant step or setting.

If you do not use <+artifact.image>, Harness does not attempt to download and deploy the artifact.

For non-containerized artifacts, use <+artifact.path>.

For more information, go to Use Harness expressions.

Create the Environment

You can create Environments and their Infrastructure Definitions inside a stage or in the Environments section of your Project. This tutorial creates everything inside the stage.

  1. In Specify Environment, click New Environment.

  2. Name the Environment DT Tutorial, select Pre-Production, and then click Save.

  3. Click New Infrastructure. Any variables you added to the Deployment Template are displayed. You can override their values here, and these new values are used in this stage.

  4. Name the Infrastructure Definition DT Tutorial, and then click Save. The Infrastructure Definition is added to the stage.

  5. Click Continue to see Execution.

Next, define the Execution steps for the deployment.

Define the Execution

The Fetch Instances step is added automatically.

When the pipeline runs, that step runs your Deployment Template's script and traverses the JSON instances array returned to identify the target instances for deployment.

Execution has Fetch Instances but still needs a step for deployment.

  1. Select the + sign after Fetch Instances and select Deployment Template Steps.

The deployment step is added after Fetch Instances because there are existing pods to be fetched in the default namespace.

The deploy step you added to the Deployment Template is displayed.

  1. Click the deploy step and click Use Template.

  2. Enter the name DT Tutorial for the step and click Apply Changes.

Add one more step to describe the deployment and see how it worked on each fetched instance.

  1. After the DT Tutorial step, click Add Step, and select Shell Script.

  2. Name the step kubectl describe deployments.

  3. In Script, enter the following:

This script needs to loop through all the fetched instances, using a Looping Strategy in the step's Advanced section.

  1. Select Advanced.

  2. Select Looping Strategy.

  3. Select Repeat, and enter the following script:

The <+stage.output.hosts> expression references all of the hosts, pods, or instances returned by your script. 4. Click Apply Changes.

Execution is complete. Next, select the Delegate you set up as the Delegate to use for the entire stage.

Select the Delegate for the stage

You do not have to use the same Delegate for the entire stage. If multiple Delegates are used, Harness ensures that they can perform the deployment.

This tutorial uses a single Delegate because it is deploying to its cluster.

  1. Select the Advanced tab.

  2. In Define Delegate Selector, select the tag of the Delegate you installed earlier.

Run and verify the pipeline

Complete the following steps to run and verify the pipeline:

  1. Click Save. The Pipeline is ready to run.

  2. Click Run, and then click Run Pipeline.

The Pipeline runs and you can see the Kubernetes Deployment object created and the target instances fetched.

Click the Fetch Instances step, and then select Output to see the instances output from your script:

Lastly, look at the kubectl describe deployments step to see the deployment on each pod:

Review

You successfully deployed an artifact to a Kubernetes cluster using a Harness Deployment Template.

In this tutorial, you learned how to:

  • Install and register a Harness Delegate.

  • Create the Deployment Template.

  • Create the Deployment Template Pipeline.

  • Add your Docker image to Harness.

  • Define the Pipeline Service, Infrastructure, and Execution.

  • Run and verify the deployment.

Additional Deployment Template settings

The following sections describe the other Deployment Template settings.

Deployment Template Infrastructure variables

You can create the following types of variables.

  • String: Any string you might want to reference in your stage steps.

  • Secret: Select or create a secret in Harness using Harness default Secret Manager or a Secret Manager you have configured in Harness. For more information, go to Secrets and Secret Management.

  • Number: A number you might want to reference in your stage steps.

  • Connector: A Harness Connector that you want to use for this deployment. Any Connector can be used, such as an infrastructure Connector, like an AWS Connector, or a repo Connector for your manifests and specs, like a GitHub Connector.

Using variables

These are variables that you can use in the following places:

  • In the script in Fetch Instances step. For example, you can create a variable for the URL that the script in Fetch Instances uses.

  • Any step in your stage Execution.

Referencing variables

You reference variables using a Harness expression with the syntax <+infra.variables.[variable Id]>. You can reference any setting in the variable entity, such as a Connector's URL.

Here are some examples.

First, here are the variables:

Here are the expressions referencing these variables:

Reference Secret Manager connector variables using the following expressions.

  • AWS KMS: <+infra.variables.AwsKms.spec.credential.type>

  • AWS Secrets Manager: <+infra.variables.AwsSecretsManager.spec.region>

  • Azure Key Vault: <+infra.variables.AzureKeyVault.spec.vaultName>

  • Google KMS: <+infra.variables.GcpKms.spec.keyName>

  • Google Cloud secret manager: <+infra.variables.GcpSecMan.spec.credentialsRef.identifier>

  • Custom secret manager: <+infra.variables.CustomSecMan.spec.isDefault>

  • HashiCorp Vault: <+infra.variables.HashiCorp.spec.vaultUrl>

Overwriting variables

When you define the Infrastructure Definition in your stage Environment, you can override any variable values.

Target host instance variable expressions

You can use <+instance...> expressions to reference host properties.

The <+instance...> expressions refer to the Instance Attributes in the Deployment Template:

The following expressions refer to instances collected by the mandatory instancename field:

  • <+instance.hostName>

  • <+instance.host.hostName>

  • <+instance.name>

The expression <+instance.host.properties.[property name]> can be used to reference the other properties you added to Instance Attributes.

For example, in the example above you can see the artifact field name mapped to the artifactBuildNo property.

To reference artifact you would use <+instance.host.properties.artifact>.

instance.name has the same value as instance.hostName. Both are available for backward compatibility.

To use these expressions, you need to enable the Repeat Looping Strategy and use the expression <+stage.output.hosts> on the step that follows Fetch Instances.

For example, here is a Shell Script step that outputs these expressions:

  1. In the step, in Advanced, select Looping Strategy.

  2. Select Repeat.

  3. In Repeat, use the Repeat Looping Strategy and identify all the hosts for the stage as the target:

When this step runs, it runs on every host returned by Fetch Instances.

Where to put Fetch Instances and deployment steps

The stage Execution must include the Fetch Instances step and a step to deploy the artifact to the instances fetched.

There are two questions to consider when deciding where to put the Fetch Instances and deployment steps:

  1. Will you only know the instances before deployment? Do you need to query some external source to be able to know which instances exist and then iterate over them to deploy to each instance?

  2. Will you only know the instances after deployment? For example, if an external source like an orchestrator actually creates the instances, you will not know which instances exist until after you deploy.

Typically, the deployment steps come after the Fetch Instances step because you are fetching existing instances. But in some cases, such as Kubernetes, you might want to place the Fetch Instances step after the deployment so that you are getting the pods that Kubernetes created. If you put Fetch Instances step before deployment, you will be using the pods that already exist.

Command step in Deployment Template deployments

A Command step can run scripts, download artifacts, or copy artifacts and config files.

Since a Deployment Template can be used on any host type, the Command step can only be run on the Delegate. You must use the Run on Delegate option in the step.

Consequently, there is no reason to use a Looping Strategy when using the Command step with Deployment Templates.

For more information, go to Use the Command step to download, copy, or run scripts.

Payloads without high-level objects

In some cases, you might have a JSON payload without a high-level object. In these cases, you can use $ in Instance Object Array Path.

The following is an example:

In this example, the Instance Object Array Path is $ and the instancename field would use instance.vmIp.

Reference fetched instances using expressions

You can reference each instance fetched by the Fetch Instances step using the step's Output tab.

You can expand each instances and copy the expression for the instance metadata you need.

For example, the expression for the instancename of the 4th instance fetched here is (the stage name is DT_Tutorial):

To reference the entire instance list, you can use this expression:

Deployment Template sample library

The following samples show complete Deployment Templates, step templates, services, infrastructure definitions, and pipelines for several platforms.

Salesforce deployment template support

Author: Harness.io Version: 1.0 Description: This deployment template will help users deploy salesforce application.

Pre-Requisite

You will need to ensure the salesforce binary is installed on your delegate. You can install the binary via the INIT_SCRIPT.

Deployment Template Setup

This Deployment Template lets you define the deployment type for Salesforce deployments. The template defines how to fetch the deployed Salesforce app by fetching by the componentName: <+service.name>.

Execution Salesforce Deployment Step Template

Salesforce App Service Setup - Sample

Salesforce App Infrastructure Definition Setup - Sample

Sample Salesforce Pipeline Setup - CI/CD

Google Cloud Run deployment template

Author: Harness.io Version: 1.0 Description: This deployment template will help users deploy Google Cloud Run-based services.

Pre-Requisites

Users need to install the gcloud cli on the delegate to deploy this template.

Deployment Template Setup

Execution Step Setup

Pipeline Sample Setup - CI/CD

Google Cloud Run - Sample Service

Google Cloud Run - Sample Infrastructure Definition

Google App Engine sample

Author: Harness.io Version: 1.0 Description: This deployment template will help users deploy Google App Engine services.

Deployment Template

Deployment Step - Google App Engine

Elastic Beanstalk sample

Author: Harness.io Version: 1.0 Description: This deployment template will help users deploy Elastic Beanstalk services.

Deployment Template

Execution Step - Elastic Beanstalk

Prepare Step

Create Version Step

Update Environment Step

Steady State Check Step

AWS CodeDeploy deployment template sample

Author: Harness.io Version: 1.0 Description: This deployment template will help users deploy services using AWS CodeDeploy.

Pre-Requisites

  • The user needs to install the AWS CLI on the Delegate, users can install it via the INIT_SCRIPT.

Deployment Template Configuration

The Deployment Template sets the infrastructure and the ability to fetch the instances deployed via AWS CodeDeploy.

Input Parameters

  • AWS Connector: Provide an AWS Connector that has access to perform CodeDeploy Deployments.

  • Region: This defines the region where Harness will be deploying the CodeDeploy Application

  • Application Name: A name for the CodeDeploy application

  • Deployment Group: The deployment group contains settings and configurations used during the deployment. In Harness, you will provide the name of the Deployment Group for the application.

  • Deployment Configuration: A Deployment Configuration in AWS CodeDeploy is a set of rules and settings that define how a deployment will be conducted within the service. In Harness, you will provide the setting for the Deployment Configuration. The Default Setting is "CodeDeployDefault.OneAtATime".

The Deployment Template is used in the Fetch Instances Step to set the infrastructure and instance querying to track the deployed Services.

Fetch Instances Sample Execution

AWS CodeDeploy Steady State Check

This step will check for a steady state and ensure the application is healthy.

Steady State Check Execution Logs

AWS CodeDeploy Deploy Step

This step will perform the deployment of the AWS CodeDeploy Application. The Template will use the properties you have set in the Service and Deployment Template.

Deploy Step Execution Logs

AWS CodeDeploy Service Sample

This is a sample service that can be configured to represent the AWS CodeDeploy Service Type. In this example, the artifact is fetched from S3.

Service Inputs

  • bundleType: This is the type of artifact you are deploying. By default, Harness recommends zip.

Service Execution Logs

Azure VMSS deployment template sample

Author: Harness.io Version: 1.0 Description: This deployment template will help users deploy services using Azure VMSS.

Azure VMSS Deployment Template

Azure VMSS Setup Step Template

Azure VMSS Steady State Check Step

Next steps

FAQs

For frequently asked questions about Custom deployments in Harness, go to Custom deployment FAQs.

Last updated

Was this helpful?