pythonlinuxbashshelltekton

How to emit result in Tekton with python script?


I my Tekton pipeline I want to emit a result so that $(results.myresult) can be used in the next pipeline task. The code looks like this:

apiVersion: tekton.dev/v1beta1
kind: Task
  name: foo
  namespace: bar
...
spec:
  results:
    - name: myresult
  script: |
    #!/usr/bin/env bash 
    # do some meaningful stuff here before emitting the result
    myvar="foobar"
    printf $(params.myvar) | tee $(results.myresult)

However, I want to use python instead of a bash script. How can I emit the result variable? I guess I'd have to use something like this in python:

var myvar = "foobar"
sys.stdout.write(myvar)

But how can I write this into $(results.myresult) and mimic the combination of pipe and tee from Linux in python?


Solution

  • This works as well.

    script: |
      #!/usr/bin/env python3
    
      with open('$(results.myresult.path)', 'w') as f:
          f.write('myresult')