Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run python script in jenkins

I want to run a python script from Jenkins using Jenkinsfile. Is there any way to run it directly from Jenkinsfile.

I found python plugin(Click Here) in Jenkins to run a script, but there is no proper documentation for this plugin. It would be very helpful if anyone explains how to integrate this plugin with Jenkinsfile.

like image 244
Abhishek Parmar Avatar asked May 19 '20 12:05

Abhishek Parmar


People also ask

Does Jenkins work with Python?

In the Python ecosystem there are tools which can be integrated into Jenkins for testing/reporting such as: nose2 and pytest for executing unit tests and generating JUnit-compatible XML test reports and Cobertura-compatible code coverage reports.

Can Jenkins run scripts?

Jenkins features a Groovy script console which allows one to run arbitrary Groovy scripts within the Jenkins controller runtime or in the runtime on agents.

Is there any way to run Python script from jenkinsfile?

Is there any way to run it directly from Jenkinsfile. I found python plugin ( Click Here) in Jenkins to run a script, but there is no proper documentation for this plugin. It would be very helpful if anyone explains how to integrate this plugin with Jenkinsfile. Show activity on this post. Adds the ability to execute python scripts as build steps.

How to execute a python script in Jenkins with Groovy script?

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: // and executing shell command with passed `pathValue` variable into it.

How to pass parameters from Jenkins pipeline to Python script?

You also can pass parameters via Jenkins UI as dsaydon mentioned in his answer. Example pipeline step with creating new virtual environment and run script after installing of all requirements You also can pass variables from your pipeline (groovy-script) into sh command and, consequently, to your python script as arguments.

How to use the Python terminal in Jenkins?

Instead of using the window terminal, we can use the python terminal directly by installing the python plugin. Manage jenkins → Manage plugins → Available → Search for python and install that After that, the add build option in the configure project will be like And you can write and build your code directly there!


Video Answer


1 Answers

Adds the ability to execute python scripts as build steps. Other than that, this plugin works pretty much like the standard shell script support

Per the docs of the plugin. Though I have not used this plugin through pipeline, from job perspective, you have to just provide .py script (filename and path), in a same way you provide for shell/powershell script.

Similarly, even for python, you'll be executing the script on a node, which will be either Linux or Windows.

So, it would work as below :

stage('build') {
    steps {
        sh 'python abc.py'
    }
}

References : https://www.jenkins.io/doc/pipeline/tour/hello-world/

Lookout for "Python" block.

like image 161
saurabh14292 Avatar answered Oct 23 '22 01:10

saurabh14292