0

I have this kind of yaml file to define a trigger `

apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerTemplate
metadata:
  name: app-template-pr-deploy
spec:
  params:
    - name: target-branch
    - name: commit
    - name: actor
    - name: pull-request-number
    - name: namespace
  resourcetemplates:
    - apiVersion: tekton.dev/v1alpha1
      kind: PipelineRun
      metadata:
        generateName: app-pr-$(tt.params.actor)-
        labels:
          actor: $(tt.params.actor)
      spec:
        serviceAccountName: myaccount
        pipelineRef:
          name: app-pr-deploy
        podTemplate:
          nodeSelector:
            location: somelocation
        params:
          - name: branch
            value: $(tt.params.target-branch)
**          - name: namespace
            value: $(tt.params.target-branch)**
          - name: commit
            value: $(tt.params.commit)
          - name: pull-request-number
            value: $(tt.params.pull-request-number)
        resources:
          - name: app-cluster
            resourceRef:
              name: app-location-cluster

` The issue is that sometime target-branch is like "integration/feature" and then the namespace is not valid I would like to check if there is an unvalid character in the value and replace it if there is.

Any way to do it ?

Didn't find any valuable way to do it beside creating a task to execute this via shell script later in the pipeline.

jerelev
  • 1
  • 4

2 Answers2

0

This is something you could do from your EventListener, using something such as:

apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
metadata:
  name: xx
spec:
  triggers:
  - name: demo
    interceptors:
    - name: addvar
      ref:
        name: cel
      params:
      - name: overlays
        value:
        - key: branch_name
          expression: "body.ref.split('/')[2]"
    bindings:
    - ref: your-triggerbinding
    template:
      ref: your-triggertemplate

Then, from your TriggerTemplate, you would add the "branch_name" param, parsed from your EventListener.

Note: payload from git notification may vary. Sample above valid with github. Translating remote/origin/master into master, or abc/def/ghi/jkl into ghi.

SYN
  • 4,476
  • 1
  • 20
  • 22
0

I've created a separate task that is doing all the magic I needed and output a valid namespace name into a different variable. Then instead of use namespace variable, i use valid-namespace all the way thru the pipeline.

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: validate-namespace-task-v1
spec:
  description: >-
    This task will validate namespaces
  params:
    - name: namespace
      type: string
      default: undefined
  results:
    - name: valid-namespace
      description: this should be a valid namespace
  steps:
    - name: triage-validate-namespace
      image: some-image:0.0.1
      script: |
        #!/bin/bash
        echo -n "$(params.namespace)" | sed "s/[^[:alnum:]-]/-/g" | tr '[:upper:]' '[:lower:]'| tee $(results.valid-namespace.path)

Thanks

jerelev
  • 1
  • 4
  • Starting a pod for string substitution is really missing the point of interceptors, cel, ... or tekton in general. Sure, you may be more familiar with sed. But is it worth slowing down all your pipelines, scheduling a pod, waiting for image to pull, running your command and fetching its result? Can't you do this with CEL, in a fraction of a second? – SYN Feb 19 '23 at 10:47
  • @SYN, I agree with your approach, let me know how to do it because I couldn't figure out the right solution. – jerelev Feb 20 '23 at 17:36