Use Templates

Updated 2 months ago by Michael Cretzman

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

In this topic:

Only members of Harness User Group accounts with the Manage Template Library permission may create, edit, and delete templates. Others User Group members can view and link to templates.

Intended Audience

  • Developers
  • DevOps

Before You Begin

Account and Application Templates

Harness includes an account-wide Template Library, called the Shared Template Library, and an Application-wide Template Library.

The Shared Template Library is available from Setup and the Application Template Library is available in each Application.

Using templates from either source works the same way, and both options are available in Harness components, but Application templates may be used within their Application only.

For example, when you click Add Command in the Service, you see the option to select a template from the Application or Shared Template Library.

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 bash and PowerShell commands to Services, you can create templates 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. When you use this Service in a Workflow, the Service command will be available as a step.

Create Service Command Units

You can add multiple Service commands into a single template as a Service Command Unit. You can share and change variables across the commands in the unit, and then use the unit in Services and Workflows just like an individual Service command.

Here is an example of a Service Command Unit named MyServiceTest that contains the Service command MeetAndCheck and two instances of the Service command ServiceCheck.

To create a Service Command Unit, do the following:

  1. In a Template Library (Application or Shared), create the individual Service commands that you want to use as a unit.
  2. Create a Service Command Unit by adding the Service commands to one Service Command.
    1. Click Add Template, and click Service Command.
    2. In Name, enter a name for the new Service command.
    3. In Variables, add any variables that you want to use in the subordinate commands. You can leave Variables empty, and when you add subordinate commands their variables will be added to the Variables section.
    4. Click SUBMIT. The Service Command is created.
    5. Click Add to add the subordinate Service commands. The Service commands in the same folder are displayed.
    6. Click a command. The command's dialog appears, and you can change any variables in the command.
    7. Click SUBMIT.

      If you click the parent Service Command Unit's name you will see that the variable from the subordinate Service command you added is now in the parent Service command's Variables section.
    8. Add any more Service commands to the unit. All of the variables used in the subordinate commands are also added to the parent Service command's Variables section.

      You can also add the same Service command more than once and Harness will add a -number suffix to their names.
      You can also use different versions of the same command in the unit:
      You can also add a variable to the parent Service command, and use it in a subordinate Service command.
    9. When you are done, link the Service Command Unit to a Service. In the Service, click Add Command, and select the Service Command Unit from a Template Library by clicking its Link button.
      You will be prompted to replace any Service command variables.
    10. Change any variables, if needed, and click SUBMIT. The Service Command Unit is added to the Service.

The Service Command Unit can be added to Workflow in two ways:

  • Template Library - In the Workflow, simply click Add Command, and select the Service Command Unit from a Template Library.
  • From the Service in the Workflow - When you create a Workflow you select a Harness Service to deploy. If the Service contains a Service Command Unit, click Add Command and the Service Command Unit is available in the Commands options.

The same Service Command Unit added from the Template Library is different from the Service Command Unit added via the Service:

  • The Service Command Unit added from a Template Library will contain the variable values set in the Template Library.
  • The Service Command Unit added from the Service will contain the variable values set in the Service.

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 Workflow

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

To copy a template to a Workflow, do the following:

  1. In your Application, open a Workflow.
  2. Click Add Command. The Add Command dialog appears.
  3. Click Select from Application Template Library or Select from Shared Template Library.
  4. Click the arrow next to the Link option, and then click Copy.
    Once the template is copied to the Workflow, it is not longer part of the versioning of the template in a Template Library. Consequently, the version number for the copied command is reset to Version: 1.
  5. Edit the copied command as needed.

Copying vs Linking to a Template

When you add a command by linking to a Template, the Template often shows you Template variables that you must provides values for.

When you copy a Template, it is similar to creating a new command, and so the variables from the Template do not appear.

For example, a new Shell Script command would not have existing variables, and so copying a Shell Script command Template will not have the variables either.

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?