简体   繁体   中英

Jenkins runs imported python script before main python script

I have a main build script which every jenkins job executes. This main script gets the job name(from Jenkins) and then executes the relevant script for that job name. Right now, I'm using subprocess.check_output to call the relevant script. I was thinking that instead of this, I should just import the script and then call the functions inside. I import it like this:

sys.path.insert(0,os.path.abspath(importLocation))
print("path", sys.path, os.path.abspath(importLocation))
import build

I also tried insert(-1,...) to put it at the end.The way I use it is that I have a couple of print statements in the main script before executing functions in this one.

print("Starting script...")
build.run()

This works fine on the console as it displays "Starting script" before running it. Unfortunately in Jenkins, it always shows build.run()'s output in the console before displaying anything from the main script. I even tried putting build.run() at the very bottom, in an if-statement etc.

Any ideas on how to make it run/show up in Jenkins in the right order?

The child process flushes its output buffers on exit but the prints from the parent are still in the parent's buffer. The solution is to flush the parent buffers before running the child:

print("Starting script...")
sys.stdout.flush()
build.run()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM