How to execute local python scripts in Jenkins UI

I am new to Jenkins, I recently want to schedule a task to run a local python script. I don’t have source control yet, so I selected “No” in “Source Control” when creating a job in the Jenkins user interface.

I did some research on how to execute python scripts in the Jenkins user interface, and I tried using Python Plugin to execute python scripts as build steps. But it failed. (But actually I do not want to use this plugin, since my script accepts input arguments, so I think I need to select something like "execute shell" in the BUILD field - I tried, but also failed). Can someone help me find how to run / call the local python script correctly?

PS: I also do not understand what Jenkins Workspace is and how it works? It will be appropriate if someone can clarify this for me.

Here is the console output I received after the build failed:

Started by user Yiming Chen [EnvInject] - Loading node environment variables. Building in workspace D:\Application\Jenkins\workspace\downloader [downloader] $ sh -xe C:\windows\TEMP\hudson3430410121213277597.sh The system cannot find the file specified FATAL: command execution failed java.io.IOException: Cannot run program "sh" (in directory "D:\Application\Jenkins\workspace\downloader"): CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(Unknown Source) at hudson.Proc$LocalProc.<init>(Proc.java:245) at hudson.Proc$LocalProc.<init>(Proc.java:214) at hudson.Launcher$LocalLauncher.launch(Launcher.java:846) at hudson.Launcher$ProcStarter.start(Launcher.java:384) at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:108) at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:65) at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20) at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779) at hudson.model.Build$BuildExecution.build(Build.java:205) at hudson.model.Build$BuildExecution.doRun(Build.java:162) at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:534) at hudson.model.Run.execute(Run.java:1728) at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43) at hudson.model.ResourceController.execute(ResourceController.java:98) at hudson.model.Executor.run(Executor.java:404) Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.<init>(Unknown Source) at java.lang.ProcessImpl.start(Unknown Source) ... 16 more Build step 'Execute shell' marked build as failure Finished: FAILURE 
+5
source share
2 answers

Create a Jenkins task and run your scripts as a shell script from the jenkins task. Like this

 #!/bin/sh python <absolute_path_of_python_script>.py 
+4
source

instead of processing the local script file on each server, you can actually copy the entire python script to the "execution shell" in the "Build" section. it should start with the corresponding python shebang. For instance:

 #!/usr/bin/env python your script... 

you can also add parameters to the task and use environment variables in your python script. eg

 parameter1 = os.environ['parameter1'] 
+3
source

Source: https://habr.com/ru/post/1263564/


All Articles