Search code examples
tekton

Tekton trigger flow from github


I am learning Tekton (for business), coming from github actions (private).

The Tekton docs (or any other tutorial I could find) have instructions on how to automatically start a pipeline from a github push. Basically they all somewhat follow the below flow: (I am aware of PipelineRun/TaskRun etc)

Eventlistener - Trigger - TriggerTemplate - Pipeline

All above steps are basically configuration steps you need to take (and files to create and maintain), one easier than the other but as far as I can see they also need to be taken for every single repo you're maintaining. Compared to github actions where I just need 1 file in my repo describing everything I need this seems very elaborate (if not cumbersome).

Am I missing something ? Or is this just the way to go ? Thanks !


Solution

  • they also need to be taken for every single repo you're maintaining

    You're mistaken here.

    The EventListener receives the payload of your webhook.

    Based on your TriggerBinding, you may map fields from that GitHub payload, to variables, such as your input repository name/URL, a branch or ref to work with, ...

    For GitHub push events, one way to do it would be with a TriggerBinding such as the following:

    apiVersion: triggers.tekton.dev/v1alpha1
    kind: TriggerBinding
    metadata:
      name: github-push
    spec:
      params:
      - name: gitbranch
        value: $(extensions.branch_name) # uses CEL interceptor, see EL below
      - name: gitrevision
        value: $(body.after) # uses body from webhook payload
      - name: gitrepositoryname
        value: $(body.repository.name)
      - name: gitrepositoryurl
        value: $(body.repository.clone_url)
    

    We may re-use those params within our TriggerTemplate, passing them to our Pipelines / Tasks:

    apiVersion: triggers.tekton.dev/v1alpha1
    kind: TriggerTemplate
    metadata:
      name: github-pipelinerun
    spec:
      params:
      - name: gitbranch
      - name: gitrevision
      - name: gitrepositoryname
      - name: gitrepositoryurl
      resourcetemplates:
      - apiVersion: tekton.dev/v1beta1
        kind: PipelineRun
        metadata:
          generateName: github-job-
        spec:
          params:
          - name: identifier
            value: "demo-$(tt.params.gitrevision)"
          pipelineRef:
            name: ci-docker-build
          resources:
          - name: app-git
            resourceSpec:
              type: git
              params:
              - name: revision
                value: $(tt.params.gitrevision)
              - name: url
                value: $(tt.params.gitrepositoryurl)
          - name: ci-image
            resourceSpec:
              type: image
              params:
              - name: url
                value: registry.registry.svc.cluster.local:5000/ci/$(tt.params.gitrepositoryname):$(tt.params.gitrevision)
          - name: target-image
            resourceSpec:
              type: image
              params:
              - name: url
                value: registry.registry.svc.cluster.local:5000/ci/$(tt.params.gitrepositoryname):$(tt.params.gitbranch)
          timeout: 2h0m0s
    

    Using the following EventListener:

    apiVersion: triggers.tekton.dev/v1alpha1
    kind: EventListener
    metadata:
      name: github-listener
    spec:
      triggers:
      - name: github-push-listener
        interceptors:
        - name: GitHub push payload check
          github:
            secretRef:
              secretName: github-secret # a Secret you would create (option)
              secretKey: secretToken    # the secretToken in my Secret matches to secret configured in GitHub, for my webhook
            eventTypes:
            - push
        - name: CEL extracts branch name
          ref:
            name: cel
          params:
          - name: overlays
            value:
            - key: truncated_sha
              expression: "body.after.truncate(7)"
            - key: branch_name
              expression: "body.ref.split('/')[2]"
        bindings:
        - ref: github-push
        template:
          ref: github-pipelinerun
    

    And now, you can expose that EventListener, with an Ingress, to receive notifications from any of your GitHub repository.