0

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?

BugsBuggy
  • 115
  • 1
  • 9
  • I don't understand your (allegedly working) bash script. You assign a variable named `myvar`, but you never actually use it anywhere. The first step you are doing instead is to run a program named `params.myvar`. You make `printf` to use the stdout of this program for feeding the pipe. This means you could equally well have written `params.myvar | tee ...`. If you want to discuss this problem, start with a meaningful bash script - then we can think of how to translate this to Python. – user1934428 Nov 23 '22 at 14:03
  • You don't "mimic the pipe operation", you just write the output to both the desired output file and stdout. You do what `| tee` does, in other words. – kindall Nov 23 '22 at 14:04
  • @user1934428 we're talking about a tekton pipeline task. What I actually do is described here: https://tekton.dev/docs/pipelines/pipelines/#emitting-results-from-a-pipeline – BugsBuggy Nov 23 '22 at 14:53
  • @kindall I found this https://shallowsky.com/blog/programming/python-tee.html. I'll try it out and report back whether it helped. – BugsBuggy Nov 23 '22 at 14:57
  • After your edit, your question becomes more clear, and I retracted the closing vote. – user1934428 Nov 24 '22 at 07:05

2 Answers2

0

First thing I would look at .... your sys.stdout.write kind of suggests you are writing stuff ... to stdout. While you mean to write this to your RESULTS ($(results.myresult)).

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

I found a working solution! This is how a working example would look like using python:

script: |
  #!/usr/bin/python3
  import subprocess
  with open('$(results.myresult.path)', 'w') as f:
      result = subprocess.run(['printf', 'This is a test!'], stdout=f)
  print(result.stdout)
BugsBuggy
  • 115
  • 1
  • 9