Pipeline: [Question] How to get pipleine.spec.tasks.name in task?

Created on 19 Nov 2020  路  4Comments  路  Source: tektoncd/pipeline

---
piVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: my-pipeline
spec:
  tasks:
    - name:  echo1
      taskRef:
        name: my-echo
    - name:  echo2
      taskRef:
        name: my-echo

----
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: my-echo
spec:
  steps:
    - name: echo
      image: ubuntu
      command:
        - echo
      args:
        -  $(context.xxx)   ###  how to get the name "echo1/echo2" defined in the pipeline 

I know of a way to pass this value by adding an extra parameter to the Task/Pipeline, as follows.

---
piVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: my-pipeline2
spec:
  tasks:
    - name:  echo1
      taskRef:
        name: my-echo2
      params:
        - name: name
          value: echo1         
    - name:  echo2
      taskRef:
        name: my-echo2
      params:
        - name: name
          value: echo2

----
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: my-echo2
spec:
  params:
    - name: name
      type: string
  steps:
    - name: echo
      image: ubuntu
      command:
        - echo
      args:
        -  $(params.name)

Pipeline's content is exposed to our users. Above this way, our user needs to configure the same content(echo1/echo2) twice. This would seem strange and ugly...
Is there a better way?

kinquestion

Most helpful comment

(Though this is potentially a feature we could consider adding)

All 4 comments

According to https://github.com/tektoncd/pipeline/blob/master/docs/variables.md there isn't currently a variable for pipelineTasks.

(Though this is potentially a feature we could consider adding)

(Though this is potentially a feature we could consider adding)

We bypass the problem by adjusting our business. Still look forward to this feature, Thanks!

There is a label on the pod that executes the task called tekton.dev/pipelineTask
If an environment variable would work for you then you can do something like this:

---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: my-echo
spec:
  steps:
    - name: echo
      image: ubuntu
      script: |
        #!/bin/bash
        echo $TASKRUN
      env:
        - name: "TASKRUN"
          valueFrom:
            fieldRef:
              fieldPath: metadata.labels['tekton.dev/pipelineTask']

But I have no idea how "stable" that label is. It is not listed here: https://tekton.dev/docs/pipelines/labels/#automatic-labeling

Was this page helpful?
0 / 5 - 0 ratings