tektoncd / triggers

Event triggering with Tekton!
Apache License 2.0
557 stars 420 forks source link

Tekton Bitbucket Event listener only run when the master branch is changed #1607

Open tppalani opened 1 year ago

tppalani commented 1 year ago

Hi Team,

1) I have successfully configured Event listener with bitbucket server. 2) But the problem is when new changes has been detected inside my repository pipeline getting triggered, Instead i want trigger the pipeline when the changes has merged or committed in only master branch.

Here the code.

Trigger-binding.yaml

apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerBinding
metadata:
  name: bitbucket-trigger-binding
  namespace: devops
spec:
  params:
  - name: gitrevision
    value: $(body.changes[0].ref.displayId)
  - name: gitrepositoryurl
    value: $(body.repository.links.clone[1].href)

Eventlistener.yaml

apiVersion: triggers.tekton.dev/v1beta1
kind: EventListener
metadata:
  name: bitbucket-event-listener
  namespace: devops
spec:
  serviceAccountName: tekton-triggers-example-sa
  triggers:
    - name: bitbucket-trigger-binding
      interceptors:
        - ref:
            name: "bitbucket"
          params:
            - name: secretRef
              value: null
            - name: eventTypes
              value:
                - repo:refs_changed
      bindings:
        - ref: bitbucket-trigger-binding
      template:
        ref: bitbucket-trigger-template

Trigger template

apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerTemplate
metadata:
  name: bitbucket-trigger-template
  namespace: devops
spec:
  params:
  - name: revision
  - name: url
    default: "https://bitbucket.tekton-poc.git"
  - name: flags
    default: --insecure
  resourcetemplates:
  - apiVersion: tekton.dev/v1beta1
    kind: PipelineRun
    metadata:
      generateName: trigger-eventlistener-
    spec:
      pipelineRef:
        name: test-pipeline-triggers
      podTemplate:
        securityContext:
          runAsUser: 0
          fsGroup: 1001
      params:
        - name: repo_full_name
          value: "tekton-poc"
        - name: revision
          value: "eventlistener-trigger"
        - name: subdirectory
          value: "source"
        - name: url
          value: "https://bitbucket.tekton-poc.git"
tppalani commented 1 year ago

Hi @savitaashture can you please help me on this.

vinamra28 commented 1 year ago

hi @tppalani, probably one interceptor won't work alone, you'll have to use CEL expressions as well in order to filter the request based on custom requirements such as

    - ref:
        name: "cel"
      params:
        - name: "filter"
          value: body.ref == 'refs/heads/main'

so your resultant EventListener would turn up as

apiVersion: triggers.tekton.dev/v1beta1
kind: EventListener
metadata:
  name: bitbucket-event-listener
  namespace: devops
spec:
  serviceAccountName: tekton-triggers-example-sa
  triggers:
    - name: bitbucket-trigger-binding
      interceptors:
        - ref:
            name: "bitbucket"
          params:
            - name: secretRef
              value: null
            - name: eventTypes
              value:
                - repo:refs_changed
        - ref:
              name: "cel"
           params:
            - name: "filter"
              value: body.ref == 'refs/heads/main'
      bindings:
        - ref: bitbucket-trigger-binding
      template:
        ref: bitbucket-trigger-template

@dibyom @savitaashture @khrm please correct me if I missed something here

tppalani commented 1 year ago

Hi @vinamra28 Thanks for the quick response. As per your instruction i have updated my event listener yaml file but now the pipeline is not getting triggered if target branch updated.

dibyom commented 1 year ago

@tppalani just making sure - @vinamra28 's example used value: body.ref == 'refs/heads/main' - if you branch name is master, you'd want to use refs/heads/master. If that is not the issue, then could you please provide logs from the eventlistener?

tekton-robot commented 1 year ago

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale with a justification. Stale issues rot after an additional 30d of inactivity and eventually close. If this issue is safe to close now please do so with /close with a justification. If this issue should be exempted, mark the issue as frozen with /lifecycle frozen with a justification.

/lifecycle stale

Send feedback to tektoncd/plumbing.

tekton-robot commented 1 year ago

Stale issues rot after 30d of inactivity. Mark the issue as fresh with /remove-lifecycle rotten with a justification. Rotten issues close after an additional 30d of inactivity. If this issue is safe to close now please do so with /close with a justification. If this issue should be exempted, mark the issue as frozen with /lifecycle frozen with a justification.

/lifecycle rotten

Send feedback to tektoncd/plumbing.

tekton-robot commented 1 year ago

Rotten issues close after 30d of inactivity. Reopen the issue with /reopen with a justification. Mark the issue as fresh with /remove-lifecycle rotten with a justification. If this issue should be exempted, mark the issue as frozen with /lifecycle frozen with a justification.

/close

Send feedback to tektoncd/plumbing.