简体   繁体   中英

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

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

#!/bin/sh
python3 <absolute_path_of_python_script>.py

If you are on ubuntu. As there are command python3 specified instead of python.

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']

Another way is creating pipeline and execute sh command, which points to your python script. You also can pass parameters via Jenkins UI as dsaydon mentioned in his answer.

sh command can be as follow (is like you run in command line):

sh 'python.exe myscript.py'

Example pipeline step with creating new virtual environment and run script after installing of all requirements

stage('Running python script'){
sh      '''
        echo "executing python script"
        "'''+python_exec_path+'''" -m venv "'''+venv+'''" && "'''+venv+'''\\Scripts\\python.exe" -m pip install --upgrade pip && "'''+venv+'''\\Scripts\\pip" install -r "'''+pathToScript+'''\\requirements.txt" && "'''+venv+'''\\Scripts\\python.exe" "'''+pathToScript+'''\\my_script.py" --path "'''+PathFromJenkinsUI+'''"
        '''
}

where

sh ''' 
   your command here
   ''' 

means multiline shell command (if you really need it)

You also can pass variables from your pipeline (groovy-script) into sh command and, consequently, to your python script as arguments. Use this way '''+argument_value+''' (with three quotes and plus around variable name)

Example: your python script accepts optional argument path and you want to execute it with specific value which you would like to input in your Jenkins UI. Then your shell-command in groovy script should be as follow:

// getting parameter from UI into `pathValue` variable of pipeline script
// and executing shell command with passed `pathValue` variable into it.

pathValue = getProperty('pathValue') 
sh '"\\pathTo\\python.exe" "my\\script.py" --path "'''+pathValue+'''"' 

To execute a Python script under BUILD option - select execute windows batch command - type these cammands.

I am passing the pythonpath because jenkins was not able to access the environmental variables because of access issues.

set PYTHONPATH=%PYTHONPATH%;C:\Users\ksaha029\AppData\Local\Programs\Python\Python3
python C:\Users\ksaha029\Documents\Python_scripts\first.py

On Mac I just moved the script.py to /Users/Shared/Jenkins/Home/workspace/your_project_name and with chmod 777 /Users/Shared/Jenkins/Home/workspace/your_project_name/script.py I could fix the problem. Also, I did not need to use : #!/bin/sh or #!/usr/bin/env python . Just inside jenkins build I used:

python3 /Users/Shared/Jenkins/Home/workspace/your_project_name/script.py

I should mention that, for one day I was trying tu solve this problem, and I have read all the froum related questions. no one really could help :/ .

Simplest implementation is checking the inject environment variables into the build process box. Then define two variables one for the python another for the script.
For example PYTHONPATH = C:/python37/python.exe TEST1SCRIPT = C:/USERS/USERNAME/Documents/test1.py Executing the windows batch command.
%PYTHONPATH% %TEST1SCRIPT% This way you can run a number of scripts inside one or multiple execute windows batch command segments. There are also way to customize. You can create a wrapper that would run the scripts under Jenkins, this way, script result output could be formatted, if emailing results of the whole test suite.

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