Create an artifact source template
Harness templates help you standardize your pipelines for your DevOps teams. Team members can select stage, step, and other templates instead of having to create them.
An artifact source template helps you define the repositories, paths, and tags for an artifact that your team can use in Harness CD services. You can define the artifact source template and your team members can select them when they use services in their pipelines.
This topic describes how to create an artifact source template and use it in a stage template and a Harness service.
Visual summary
Important notes
- Artifact source templates are not supported in pipeline triggers at this time. Support for using artifact source templates in pipeline triggers will be added soon.
- Artifact source templates cannot be stored in a remote Git repo at this time. They are stored in your Harness account.
Artifact source template scope
You can create artifact source templates at the Harness account, org, and project levels.
Create the artifact source template at the level you want to share it. For example, if you want to share it across all services in all projects in an org, create it at the org level.
Create an artifact source template
To create an artifact source template, do the following:
- Visual
- YAML
-
In a Harness project, org, or account settings, select Templates.
- Project:
- Select a module, such as Deployments.
- In Project Setup, click Templates.
- Org:
- Select an org.
- In Organization Resources, select Templates.
- Account:
- Select Account Resources.
- Select Templates.
- Project:
-
Select New Template, and then select Artifact Source.
The artifact source template appears.
-
Enter a name for the artifact source template. Use a name that describes the artifact source so that team members understand what artifact is represents.
-
In Version Label, enter a version for the template. You can update the version each time you change and save a template.
-
In Logo, upload an icon image for the template.
-
If you are creating this template at the Harness project level, in Save To, select where you want to save the template. For more information, go to saving templates to project, org, or account level.
-
Select Start.
Artifact registry type
Harness support multiple artifact registry providers, including Google Container Registry (GCR), AWS Elastic Container Registry (ECR), and Azure Container Registry (ACR).
Harness includes a platform-agnostic artifact registry type named Docker Registry. Use Docker Registry for images on any registry, including Docker Hub.
- Select the artifact repo provider where your images are stored.
Repository
- In the repository setting, select or create the Harness registry connector. For example, if you selected Docker Registry in Artifact Repository Type, the repository setting is named Docker Registry Repository.
For information on registry connectors, go to connect to an artifact repo.
You can also select Runtime input or Expression for this setting.
Artifact details
Different repository types have different Artifact Details settings.
For information on how to configure each repository's settings, see the examples in Harness Kubernetes services.
Tags
The Value setting in Tags is a runtime input. You can select the tag to use when the pipeline using this artifact source template executes.
You can also use Regex.
For example, if the build is todolist-v2.0
, the regex todolist-v\d.\d
will match.
If the regex expression does not result in a match, Harness ignores the value.
Harness supports standard Java regex. For example, if you enable Regex with the intent is to match a filename, the wildcard must be .*
instead of only *
. Similarly, if you want to match all files ending in -DEV.tgz
, your wildcard regex phrase would be: .*-DEV\.tgz
- In your template, select YAML
- Paste the following YAML example and select Save:
template:
name: Todo List
identifier: Todo_List
versionLabel: "1"
type: ArtifactSource
projectIdentifier: CD_Docs
orgIdentifier: default
tags: {}
spec:
type: DockerRegistry
spec:
imagePath: harness/todolist-sample
tag: <+input>
connectorRef: Docker_Hub_with_Pwd
Note that connectorRef
refers to the Id of a Harness connector.
Saving templates to project, org, or account level
If you are creating a template at the Harness project level, in Save To, you can select where you want to save the template:
- Project: The template is saved to this project. You will see it in the Templates list in all of the modules in this project that support templates (Deployments, Builds, Security Tests, etc).
- Organization: The template is saved to the org where this project is located. The template will only appear in the Templates list at the org level, but the template can be selected in any project in that org.
- Account: The template is saved at the account level. The template will only appear in the Templates list at the account level, but the template can be selected in any project in the account.
If you create the template at the project level but select Organization or Account in Save To, the template will not appear in the Templates list of the project. Navigate to the Templates list in the org or account where you saved it to see the template.
This can be a little confusing the first time you create a template because you saved the template in a project but it doesn't appear in that project's Templates list.
You will still be able to select the template in services in this project.
Runtime inputs and expressions in template settings
Artifact Repository Type is the only setting in the template that must be a fixed value. The remaining settings can be runtime inputs and expressions also.
For information on runtime inputs and expressions, go to fixed values runtime inputs and expressions.
Runtime inputs can be useful in artifact source templates because they let your team members select the repository, path, and tags to use when they run pipelines using artifact source templates.
Expressions can be useful in artifact source templates because you can use default Harness expressions, like <+stage.name>
, and service variables, like <+serviceVariables.myapp>
, in place of the connector name and/or image path. When the pipeline using the artifact source template runs, those expressions are resolved and their related connector and/or image path is selected.
Use artifact templates in stage templates
After creating an artifact source template, you can add it as the artifact source in stage templates.
Using stage templates with artifact source templates is useful for getting your team onboarded quickly.
Artifact sources are used in CD stages. Ensure you are using a CD stage template (Deploy).
To add an artifact source template to a stage template, do the following:
-
Open the stage template in Template Studio.
-
In Service, in Select Service, select Add Service.
-
In the new service, in Artifacts, select Use template.
-
Select the artifact source template you want to use, and then select Use Template.
-
In Configure Artifact Source, enter a name for the artifact.
-
In Tag, you can change the setting to Fixed Value, and then select the tag to use. Or you can use a runtime input or expression.
-
Select Apply Changes.
The artifact source template is added to the stage template.
Use artifact templates in CD services
After creating an artifact source template, you can add it as the artifact source of a Harness service.
To add an artifact source template to a service, do the following:
- Visual
- YAML
-
Select the Deployments module.
-
Select Services.
-
Select Manage Services.
-
Select a service.
-
Select Configuration.
-
In Service Definition, in Artifacts, select Use template.
-
Select the artifact source template you want to use, and then select Use Template.
-
Enter a name for template.
-
In Tag, you can change the setting to Fixed Value, and then select the tag to use. Or you can use a runtime input or expression.
-
Select Apply Changes. The template is added to the service.
- Select the Deployments module.
- Select Services.
- Select Manage Services.
- Select a service.
- Select Configuration.
- Select YAML.
- Paste the following YAML example and select Save:
service:
name: Example K8s
identifier: Example_K8s
serviceDefinition:
type: Kubernetes
spec:
artifacts:
primary:
primaryArtifactRef: <+input>
sources:
- name: todolist
identifier: todolist
template:
templateRef: Todo_List
versionLabel: "1"
templateInputs:
type: DockerRegistry
spec:
tag: <+input>
manifests:
- manifest:
identifier: manifest
type: K8sManifest
spec:
store:
type: Harness
spec:
files:
- /Templates/deployment.yaml
skipResourceVersioning: false
gitOpsEnabled: false
You can see the artifact source template in the artifacts
section:
...
- name: todolist
identifier: todolist
template:
templateRef: Todo_List
versionLabel: "1"
templateInputs:
type: DockerRegistry
spec:
tag: <+input>
...
Reconcile artifact source template changes in a service
When changes are made to an artifact source template the service using the template is not automatically updated in the Harness Manager.
You can reconcile these changes by using the Reconcile option in the service (next to Save).
Once the reconcile action is initiated, the changes are available in the service.