I have the following python script myscript.py:
#!/usr/bin/python
def main():
return "hello"
if __name__ == '__main__':
main()
And I run that script from the Jenkins groovy:
returnValue = sh(returnStdout: true, script: "python3.9 -u scripts/myscript.py")
log.info("returnValue: ${returnValue}")
I expect to see returnValue: hello
in the logs but I see returnValue:
I understand that returnStdout: true
returns standard output from the python script, and since there is no standard output the returnValue
is null resulting in returnValue:
To prove my point, I modify my python script to print
“hello” instead of returning it as following:
#!/usr/bin/python
def main():
print("hello")
if __name__ == '__main__':
main()
And now I get returnValue: hello
from Jenkins log.
Even though the implementation with the print
instead of return
works it just doesn’t feel right and very confusing.
Is this behavior correct?
Or is there a way to actually use return
keyword in the python script and store the returned value in the variable inside the Jenkins groovy file?