triggersyamltekton

Tekton YAML TriggerTemplate - string substitution


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.


Solution

  • 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