github.com/argoproj/argo-events@v1.9.1/docs/sensors/triggers/build-your-own-trigger.md (about) 1 # Build Your Own Trigger 2 3 Argo Events supports a variety of triggers out of box like Argo Workflow, K8s Objects, AWS Lambda, HTTP Requests etc., but you may want to write your own logic to trigger a pipeline or create an object in K8s cluster. An example would be to trigger 4 TektonCD or AirFlow pipelines on GitHub events. 5 6 ## Custom Trigger 7 8 In order to plug your own implementation for a trigger with Argo Events Sensor, you need to 9 run a gRPC server that implements the interface that the sensor expects. 10 11 ### Interface 12 13 The interface exposed via proto file, 14 15 // Trigger offers services to build a custom trigger 16 service Trigger { 17 // FetchResource fetches the resource to be triggered. 18 rpc FetchResource(FetchResourceRequest) returns (FetchResourceResponse); 19 // Execute executes the requested trigger resource. 20 rpc Execute(ExecuteRequest) returns (ExecuteResponse); 21 // ApplyPolicy applies policies on the trigger execution result. 22 rpc ApplyPolicy(ApplyPolicyRequest) returns (ApplyPolicyResponse); 23 } 24 25 The complete proto file is available [here](https://github.com/argoproj/argo-events/blob/master/sensors/triggers/trigger.proto). 26 27 Let's walk through the contract, 28 29 1. `FetchResource`: If the trigger server needs to fetch a resource from external sources like S3, Git or a URL, this is the 30 place to do so. e.g. if the trigger server aims to invoke a TektonCD pipeline and the `PipelineRun` resource lives on Git, then 31 trigger server can first fetch it from Git and return it back to sensor. 32 33 2. `Execute`: In this method, the trigger server executes/invokes the trigger. e.g. TektonCD pipeline resource being 34 created in K8s cluster. 35 36 3. `ApplyPolicy`: This is where your trigger implementation can check whether the triggered resource transitioned into the success state. 37 Depending upon the response from the trigger server, the sensor will either stop processing subsequent triggers, or it will continue to 38 process them. 39 40 ### How to define the Custom Trigger in a sensor? 41 42 Let's look at the following sensor, 43 44 apiVersion: argoproj.io/v1alpha1 45 kind: Sensor 46 metadata: 47 name: webhook-sensor 48 spec: 49 dependencies: 50 - name: test-dep 51 eventSourceName: webhook 52 eventName: example 53 triggers: 54 - template: 55 name: webhook-workflow-trigger 56 custom: 57 # the url of the trigger server. 58 serverURL: tekton-trigger.argo-events.svc:9000 59 # spec is map of string->string and it is sent over to trigger server. 60 # the spec can be anything you want as per your use-case, just make sure the trigger server understands the spec map. 61 spec: 62 url: "https://raw.githubusercontent.com/VaibhavPage/tekton-cd-trigger/master/example.yaml" 63 # These parameters are applied on resource fetched and returned by the trigger server. 64 # e.g. consider a trigger server which invokes TektonCD pipeline runs, then 65 # the trigger server can return a TektonCD PipelineRun resource. 66 # The parameters are then applied on that PipelineRun resource. 67 parameters: 68 - src: 69 dependencyName: test-dep 70 dataKey: body.namespace 71 dest: metadata.namespace 72 # These parameters are applied on entire template body. 73 # So that you can parameterize anything under `custom` key such as `serverURL`, `spec` etc. 74 parameters: 75 - src: 76 dependencyName: test-dep 77 dataKey: body.url 78 dest: custom.spec.url 79 80 The sensor definition should look familiar to you. The only difference is the `custom` key under `triggers -> template`. 81 The specification under `custom` key defines the custom trigger. 82 83 The most important fields are, 84 85 1. `serverURL`: This is the URL of the trigger gRPC server. 86 87 1. `spec`: It is a map of string -> string. The spec can be anything you want as per your use-case. The sensor sends 88 the spec to trigger server, and it is upto the trigger gRPC server to interpret the spec. 89 90 1. `parameters`: The parameters override the resource that is fetched by the trigger server. 91 Read more info on parameters [here](https://argoproj.github.io/argo-events/tutorials/02-parameterization/). 92 93 1. `payload`: Payload to send to the trigger server. Read more on payload [here](https://argoproj.github.io/argo-events/sensors/triggers/http-trigger/#request-payload). 94 95 The complete spec for the custom trigger is available [here](https://github.com/argoproj/argo-events/blob/master/api/sensor.md#customtrigger). 96 97 ## Custom Trigger in Action 98 99 Refer to a sample [trigger server](https://github.com/VaibhavPage/tekton-cd-trigger) that invokes TektonCD pipeline on events.