Firstly we need to configure the Python Job in Tool Registry. Go to tool registry and select the Jenkins tool you want to use to run the python script.
Before we configure the Python Job, go to the ‘Connection’ tab and make sure the ‘Auto Scaling Enabled?’ option is checked.
Running Python Script using Jenkins Build Job
Go to the ‘Jobs’ tab and click on the ‘Create Job’ button to open the Job Creation modal.
Select the Job Type as ‘Build’ and Build Type and Build Tool as ‘Python’. Select ‘Python 2’ or ‘Python 3’ in the agent labels field, based on the Python version you are running and save the job.
Now go to the Pipelines menu option and create a new pipeline.
Add a new step and select the tool as Jenkins.
In the step configuration, select the Job configured in the tool registry for Python Build and select the account, repo and branch of the script to be executed.
You can to add the path and file name of the file/files to be executed or else give your own custom script.
Save the step configuration after filling out the details and then run the pipeline to execute the Python Script.
Sample Job Config
Sample Step Config
Running Python Script using Command Line
Go to the ‘Jobs’ tab and click on the ‘Create Job’ button to open the Job Creation modal.
Select the Job Type as ‘Shell Script’ and the Script Type as ‘Python’. Select ‘Python 2’ or ‘Python 3’ in the agent labels field, based on the Python version you are running and save the job.
Now go to the Pipelines menu option and create a new pipeline.
Add a new step and select the tool as Command Line.
In the step configuration, Select the relevant Jenkins and select the job configured in the tool registry in the previous step.
Provide the relevant details in the form and save the step. Run the pipeline to execute the Python Script.
Sample Job Config
Sample Step Config