How to emit result in Tekton with python script?
Question:
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?
Answers:
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)
).
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)
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?
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)
).
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)