Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute local python scripts in Jenkins UI

I am new to Jenkins, recently want to schedule a job to execute a local python script. I do not have a source control yet so I selected "None" in Source Code Management when creating the job in the Jenkins UI.

I did some research about how to execute python scripts in Jenkins UI and I tried using Python Plugin to execute python scripts as build steps. But it failed. (But actually I don't want to use this Plugin since my script takes input arguments so I think I need to select something like "execute shell" in BUILD field -- I tried but also failed) Could anyone help me to find out how to properly run/call a local python script?

PS: I am also not clear about the Jenkins Workspace and how it works? Will appropriate if someone could clarify it for me.

Here is the Console output I got after the fail build:

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
like image 524
PIZZA PIZZA Avatar asked Jan 30 '17 08:01

PIZZA PIZZA


People also ask

How do I run a local python file in Jenkins?

Click on Add build step and choose Execute Batch Command. Depending on the environment that Jenkins operates on (Windows or UNIX), choose either Execute Windows Batch Command or Execute Shell. Scroll all the way down and click Save or Apply. You are all set!

How do I run a Python script from the UI path?

You cannot directly run a python script through UiPath. It isn't a reliable approach or a stable approach to follow. Python script: The script has to be in form of a function. Example : def(a,b): return(a+b) I have transformed your script into a function.

How do I run a local Python script on a remote server?

Using the paramiko library - a pure python implementation of SSH2 - your python script can connect to a remote host via SSH, copy itself (!) to that host and then execute that copy on the remote host. Stdin, stdout and stderr of the remote process will be available on your local running script.


1 Answers

instead of handle local script file on each server, you can actually copy all the python script into the "execute shell" under the Build section. it has to start with the relevant python shebang. For example:

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

you can also add parameters in the job and use environment variables in your python script. for example

parameter1 = os.environ['parameter1']
like image 69
dsaydon Avatar answered Oct 25 '22 05:10

dsaydon