Use Templates

Updated 5 days ago by Michael Cretzman

In this article:

You can use the Template Library to create templates of scripts and other commands and copy or link them into Harness Services and Workflows.

Only user accounts with the Account Management permission may create, edit, and delete templates. Others user can view and link to templates.

Intended Audience

  • Developers
  • DevOps

Before You Begin

Create Templates

Templates are created in the Template Library. Each template can have multiple versions with unique variables.

When creating a template, type ${...} in a field, then enter the variable name to create a template variable.

Create HTTP Template

HTTP templates contain HTTP URLs, methods, headers, assertions, and variables. You can use the HTTP template in a Harness Workflow.

  1. Click Setup.
  2. In Account, click Template Library.
  3. Click the template folder where you want to add your new template.
  4. Click Add Template and select the HTTP template type. The Add HTTP Template dialog appears.

Field

Description

Name

Enter a name for your template. Use a name that describes the purpose of the template.

Description

Enter a description for the template.

URL

Enter the URL for the HTTP call.

Method

Select the HTTP method.

Header

Enter the media type for the message. For example, if you are using the GET method, the header is used to specify the GET response body message type Harness will check, such as content-type: application/json.

Body

Enter the message body (if any) of the HTTP message.

Assertion

The assertion is used to validate the incoming response or request. For example, if you wanted to check the health of an HTTP connection, you could use the assertion ${httpResponseCode}==200.

Timeout

Enter a value, in seconds, for how long Harness should wait for a response from the server you specified in URL.

Variables

Enter the variable names and default values you want to use in the template. When a user adds or links this template to a Workflow, the user will provide the values for the variables. Using our assertion example above, you could create a variable named ResponseCode and give it a value of 200. Next, in the Assertion field, you could use the variable like this: ${httpResponseCode}==${ResponseCode}.

The following table displays the HTTP template in the Template Library and the applied template in a Workflow. Notice how the variable names defined in the template are greyed out in the applied template, and only the variable values are editable.

Template in Template Library

Template Applied in a Workflow

Create Shell Script Template

Shell Script templates contain Bash and PowerShell commands and parameters. You can enter variables into the template, apply the template to Workflows, and have the Workflow enter Workflow-specific values for the command.

To create a Shell Script template, do the following:

  1. Click Setup.
  2. In Account, click Template Library.
  3. Click the template folder where you want to add your new template.
  4. Click Add Template and select the Shell Script template type. The Add Shell Script Template dialog appears.
  5. In Name, name the script.
  6. In Script, fill out the Shell Script command as described in Using the Shell Script Command. That article will describe how to create variables in the template, for example, ${myVar}.
  7. Click SUBMIT to create the script. The script appears with a list of the variables you created.
  8. Next, apply the Shell Script template as a command in a Workflow. In a Workflow, simply click Add Command, and select From Template Library. The Shell Script template appears.

  9. Click Link to link to the template. You can then edit the template in your Workflow, providing values for the variables in the template.

The following table displays the Shell Script template in the Template Library and the applied template in a Workflow. Notice how the variable names defined in the template are greyed out in the applied template, and only the variable values are editable:

Template in Template Library

Template Applied in a Workflow

Once the Shell Script template is linked and added to the workflow, a link icon appears next to the command:

Create Service Command

To add shell and powershell script commands to Services, you can create templates for use in Services using a Service Command template. You can add variables to the template and have values supplied to the variables in the Service using the template.

To create a Service Command, do the following:

  1. Click Setup.
  2. In Account, click Template Library.
  3. Click the template folder where you want to add your new template.
  4. Click Add Template and select the Service Command template type. The Add Service Command Template dialog appears.

  5. Enter a name and description for the command.
  6. In Variables, click Add to add variables to the template. You can set a default value. The variable value can be supplied or replaced when the template is used in a Service.
  7. Click SUBMIT. The new Service Command template is created.

  8. Hover over Add to see the list of available subcommands to add to the template.

  9. Click a subcommand to see its script and make any changes.
  10. To use the template in a Service, click Add Command in the Service and then click From Template Library. The template is displayed.

  11. Click Link to link to the template. You can then edit the template in your Service, providing values for the variables in the template.

You can link a template to a Service or Workflow. The templates available for linking depend on whether you are adding them to a Service or Workflow. For example, if you are adding a command in a Service, then the templates available to you will be Bash and PowerShell scripts. If you are in a Workflow that deploys to a an AWS cloud provider, then HTTP templates are available.

To link a template to a service, do the following:

  1. Click Setup, click an application, and in Services, click a service. The Service page appears.
  2. Click Add Command. The Add Command dialog appears.
  3. Click From Template Library. The Template Library appears.
  4. Locate the template you want, and click the Link button. Provide values for the template variables and click SUBMIT. The template is added to the service and linked. You can see the Version:version and Template icon next to the entity you selected. For example:

To link a template to a workflow, do the following:

  1. Click Setup, click an application, and in Workflows, click a workflow. The Workflow page appears.
  2. In the workflow steps, click Add Command.
  3. Click From Template Library. The Template Library appears.
  4. Locate the template you want, and click the Link button. Provide values for the template variables and click SUBMIT. The template is added to the workflow and linked. You can see the Version:version and Link icon next to the entity you selected. For example:

Template Versioning

Each time you edit a template you create a new version of it. You can use different versions of a template in your services and workflows.

To use versions of a template, do the following:

  1. Open a service or workflow containing a linked template. A link icon is next to the templated entity.
  2. To edit the template, click the vertical ellipsis and click Manage Versions.


    The Manage Version dialog appears.
  3. In Default Version, click the version, locate the version you want to use, and then click select. Click SUBMIT. The new version is displayed next to the linked template name.

Copy Template to a Service or Workflow

You can link to a template or simply copy the template to your Service or Workflow. A copied template does not provide version control like a linked template.

To copy a template to a Service, do the following (the process is the same for copying a template to a Workflow):

  1. Click Setup, click an application, and in Services, click a service. The Service page appears.
  2. Click Add Command. The Add Command dialog appears.
  3. Click From Template Library. The Template Library appears.
  4. Click the arrow next to the Link option, and then click Copy.


    Once the template is copied to the service or workflow, it is not longer part of the versioning of the template in the Template Library. Consequently, the version number for the copied command is reset to Version: 1.
  5. Edit the copied command as needed.

Template YAML

When you look at the code for an Application containing Services or Workflows using linked templates, the YAML for the template information of the command is displayed like this:

- type: SHELL_SCRIPT
name: DocExample
properties:
sweepingOutputScope: null
connectionAttributes: null
publishAsVar: false
commandPath: null
scriptType: BASH
host: null
scriptString: echo "Hello" ${name}
timeoutMillis: 600000
sshKeyRef: null
executeOnDelegate: true
sweepingOutputName: null
tags: ''
templateUri: AccountName/DocExample:latest
templateVariables:
- name: name

Next Steps

Introducing The Harness Template Library For Continuous Delivery


How did we do?