TriggerTemplate
A TriggerTemplate is a powerful component in Tekton Triggers that defines the blueprint for resources that should be created when an event is detected. It acts as the action part of the event-driven pipeline, specifying exactly what Kubernetes resources (typically TaskRuns or PipelineRuns) should be instantiated in response to events.
TOC
Terminology ExplanationWhy We Need TriggerTemplateThe Challenge of Event-Driven Resource CreationHow TriggerTemplate Addresses These ProblemsAdvantagesApplicable ScenariosConstraints and LimitationsPrinciplesTriggerTemplate StructureParameter HandlingResource Template CreationRuntime Configuration TemplatesConfiguration ExamplesBasic CI Pipeline TemplateMulti-Environment Deployment TemplateTemplate with Multiple ResourcesRuntime Configuration TemplateImportant Parameter Explanations Related to TriggerTemplateParameter SubstitutionApplicable ScenariosConstraints and LimitationsPrinciples/Parameter ExplanationJSON HandlingApplicable ScenariosConstraints and LimitationsPrinciples/Parameter ExplanationReference MaterialsTerminology Explanation
Why We Need TriggerTemplate
The Challenge of Event-Driven Resource Creation
In CI/CD systems that respond to external events, several challenges arise when creating resources:
- Dynamic Resource Creation: Resources like pipelines need to be created with information specific to each event.
- Parameterization: Different events require different parameters to be passed to pipelines.
- Reusability: The same resource creation pattern often needs to be reused across multiple events.
- Templating: Manual resource creation for each event type is error-prone and not scalable.
Without TriggerTemplates, addressing these challenges would require:
- Custom scripts to generate Kubernetes resources for each event
- Complex logic to handle parameter substitution
- Duplicated resource definitions across different event handlers
- Manual synchronization between event data and resource creation
How TriggerTemplate Addresses These Problems
TriggerTemplate provides a declarative, Kubernetes-native way to:
- Define Resource Blueprints: Specify exactly what resources should be created when events occur.
- Parameterize Resources: Use parameters extracted from events to customize resources.
- Ensure Consistency: Create resources consistently with the same structure for similar events.
- Separate Concerns: Decouple event detection (EventListener) and data extraction (TriggerBinding) from resource creation.
- Enable Reusability: Define templates once and reuse them across multiple triggers.
This approach creates a clean, declarative way to define what should happen when events are detected, making your CI/CD system more maintainable and scalable.
Advantages
- Declarative Definition: Define resources to be created using Kubernetes-native YAML
- Parameterization: Dynamically customize resources with event data
- Reusability: Create templates once and reuse them across multiple triggers
- Separation of Concerns: Clear separation between event detection, data extraction, and resource creation
- Default Values: Provide fallback values for parameters that might not be present in events
- Dynamic Naming: Generate uniquely named resources to avoid conflicts
- Multiple Resources: Create multiple resources from a single event
- Resource Validation: Resources are validated at runtime before creation
Applicable Scenarios
TriggerTemplates are essential in the following scenarios:
-
Automated CI/CD Pipelines: Create pipeline runs automatically when code is pushed or pull requests are opened.
-
Multi-Environment Deployments: Use the same template with different parameters to deploy to various environments.
-
Event-Driven Workflows: Create resources in response to external system events, like issue updates or monitoring alerts.
-
Dynamic Resource Creation: Generate resources with parameters specific to each event, like commit IDs or branch names.
-
Parameterized Testing: Create test pipelines with different parameters based on the type of event.
-
As Runtime Configuration Templates: Provide standardized runtime configurations (such as parameters, workspaces, security contexts, etc.) for repeatedly executed Pipelines. Users can select matching templates through the frontend UI to quickly generate pipelinerun instances that meet scenario requirements.
Constraints and Limitations
- Parameters in TriggerTemplates can only be string values
- JSON objects must be handled carefully when used as parameters
- Resource validation happens at runtime, not at creation time
- Default service account permissions may need to be extended for non-Tekton resources
- Embedded resources must be included within the PipelineRun or TaskRun that uses them
- Parameter names with numerical prefixes require special handling
Principles
TriggerTemplate Structure
A TriggerTemplate consists of two main sections:
- Parameters Definition: Declares the parameters that can be used in the resource templates
- Resource Templates: Defines the Kubernetes resources to be created
Parameter Handling
Parameters in TriggerTemplates follow these principles:
-
Declaration: Parameters must be declared in the
paramssection with a name and optionally a description and default value. -
Reference: Parameters are referenced in resource templates using the syntax
$(tt.params.parameter-name). -
Default Values: If a parameter is not provided by a TriggerBinding, the default value is used if specified.
-
String Values: All parameter values are treated as strings, even if they represent numbers or JSON objects.
-
Special Characters: Parameters with numerical prefixes or special characters may need to be quoted in resource templates.
Resource Template Creation
When an event is detected and processed:
- The EventListener receives the event
- Interceptors process and filter the event (if configured)
- TriggerBindings extract data from the event
- The TriggerTemplate receives the extracted parameters
- Parameter substitution occurs in the resource templates
- Kubernetes resources are created with the substituted values
Each created resource receives labels to track its origin:
tekton.dev/eventlistener: <EventListenerName>tekton.dev/triggers-eventid: <EventID>
Runtime Configuration Templates
- Label Conventions
-
triggertemplate.triggers.tekton.dev/usage: runtime-configMust be set to
runtime-configto identify the purpose of the template. -
tekton.dev/pipeline: <pipeline-name>Associates the template with a specific pipeline.
-
triggertemplate.triggers.tekton.dev/runtime-config-default: "true"Optional, marks the template as the default configuration for the pipeline. If multiple default templates exist, the one with the latest creation timestamp will be selected.
- Resource Template Conventions
- Only one
PipelineRunresource can be defined inspec.resourcetemplates. - Must include
pipelineRefto reference the target pipeline. - Supports all runtime configuration fields of
PipelineRun, such asparams,workspaces,podTemplate, etc.
Configuration Examples
Basic CI Pipeline Template
Multi-Environment Deployment Template
Template with Multiple Resources
Runtime Configuration Template
Important Parameter Explanations Related to TriggerTemplate
Parameter Substitution
Parameter substitution is the core mechanism that makes TriggerTemplates dynamic and reusable.
Applicable Scenarios
- Inserting commit SHAs into PipelineRuns
- Using repository URLs and branch names in pipeline parameters
- Passing event metadata to pipelines
- Customizing resource names with event-specific information
Constraints and Limitations
- Only string values are supported
- JSON objects need special handling
- Parameters with numerical prefixes may need quoting
Principles/Parameter Explanation
The syntax for parameter substitution is $(tt.params.parameter-name), where parameter-name is the name of a parameter defined in the params section of the TriggerTemplate.
For example:
JSON Handling
When working with JSON data in TriggerTemplates, special considerations are needed.
Applicable Scenarios
- Passing complex data structures to pipelines
- Working with webhook payloads that contain nested JSON
- Creating resources that require JSON configuration
Constraints and Limitations
- JSON objects must be handled as strings
- Escaping quotes can be challenging
- Legacy templates may require special annotations
Principles/Parameter Explanation
For handling JSON objects:
- Do not enclose JSON parameters in quotes when using them in resource templates
- For legacy templates that require escaped quotes, add the annotation: