- Run a Python Script from Jenkins
- Jenkins Run Python Script in Pipeline
- Conclusion
- About the author
- John Otieno
- How to execute local python scripts in jenkins ui?
- Method 1: Use the Jenkins Execute Shell
- How to Execute Local Python Scripts in Jenkins UI using «Execute Shell»
- Method 2: Use the Jenkins Python Plugin
- Steps to execute local Python scripts in Jenkins UI using Jenkins Python Plugin
- Method 3: Use the Jenkins Pipeline Script
- Saved searches
- Use saved searches to filter your results more quickly
- SharonKarichaly/Jenkins-Python-Pipeline
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
Run a Python Script from Jenkins
Jenkins is a continuous integration and delivery platform that helps automate the software development process. One way to use Jenkins is by defining a Jenkinsfile in the root of your project and checking it into version control. This Jenkinsfile allows you to define your pipeline as code, which can be versioned, reused, and shared across projects.
This tutorial will show you how to run a Python script from a Jenkins pipeline. We will be using the Jenkins Pipeline syntax to accomplish this.
Before we get started, make sure that you have the following prerequisites:
- A Jenkins instance
- A Python script that you want to run
- The Python interpreter installed on the Jenkins controller or Agent
Jenkins Run Python Script in Pipeline
We can run a Python script within a Jenkins pipeline using the sh command in Jenkins. Let us see how we can do this.
Start by creating a new Jenkins pipeline. To do this, go to the Jenkins dashboard, click on the “New Item” link, and then choose the “Pipeline” option.
Give the pipeline a name and click the “OK” button.
Next, we will need to define the Jenkins pipeline. There are two ways to do this:
We will be using the Declarative Pipeline syntax in this tutorial.
To define the pipeline, we need to specify a series of stages where each stage represents a specific step in the pipeline.
In this case, we will create a single stage that runs our Python script. An example pipeline is as shown in the example below:
pipeline {
agent {
label ‘python’
}
stages {
stage ( ‘Run Python Script’ ) {
steps {
sh ‘python3 script.py’
}
}
}
}
Let us go through each section of this Jenkinsfile in more detail:
- The pipeline block allows us to define the start of our pipeline.
- Next, we use the agent block to specify the agent used to run our pipeline. In this case, we are using the label directive to specify that we want to use a Jenkins agent with the label “python.” This ensures that the pipeline will run on a machine with the Python interpreter installed.
- The stages block defines a series of stages in our pipeline. In this case, we only have a stage called “Run Python Script.”
- In the next section, the steps block to define the steps that will be executed within the stage. We are using the sh directive to run a shell command in this case. The command we are running is python3 script.py which will execute our Python script.
Once we have defined the pipeline, we can save and run it by clicking the “Build Now” button on the Jenkins dashboard.
If the pipeline runs successfully, we should see the output of the Python script in the Jenkins console output.
We can also use the Console Output to diagnose any errors and fix them for the job to run successfully.
Conclusion
In this article, you learned how to use the sh directive in a Jenkins pipeline to run a Python script.
About the author
John Otieno
My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list
How to execute local python scripts in jenkins ui?
If you want to execute a local Python script within the Jenkins UI, there are a few methods to achieve this. In this tutorial, we will go through several solutions to help you run your Python scripts within the Jenkins environment.
Method 1: Use the Jenkins Execute Shell
How to Execute Local Python Scripts in Jenkins UI using «Execute Shell»
To execute local Python scripts in Jenkins UI, you can use the «Execute Shell» build step. Here are the steps to do this:
- Add a new build step to your Jenkins job by clicking on «Add build step» and selecting «Execute shell».
- In the «Command» field, enter the command to run your Python script. For example, if your script is named «myscript.py» and is located in the same directory as your Jenkins job, you can use the following command:
python myscript.py $filename
export MY_VAR=value python myscript.py
Here’s an example of a «Execute Shell» build step that runs a Python script with command-line arguments and environment variables:
export MY_VAR=value python myscript.py $filename
This will set the «MY_VAR» environment variable to «value» and pass the «filename» command-line argument to the «myscript.py» Python script.
Method 2: Use the Jenkins Python Plugin
Steps to execute local Python scripts in Jenkins UI using Jenkins Python Plugin
- Install the Jenkins Python Plugin in your Jenkins instance.
- Create a new Jenkins job and configure it as a «Freestyle project».
- In the «Build» section of the job configuration, click «Add build step» and select «Execute Python script».
- In the «Script file» field, enter the path to your local Python script.
- Optionally, you can add any arguments or parameters to the script in the «Arguments» field.
- Save the job configuration and run the job.
In the Jenkins job configuration, set the «Script file» field to the path of the above Python script.
[workspace] $ python /path/to/script.py Hello, world! Finished: SUCCESS
You can also use the sh command to execute Python scripts in a Jenkins job:
This will execute the Python script as a shell command.
Note: Make sure that the Python environment in your Jenkins instance has all the necessary dependencies installed to run your Python script.
Method 3: Use the Jenkins Pipeline Script
To execute local Python scripts in Jenkins UI using the Jenkins Pipeline Script, you can follow the below steps:
- Create a new pipeline job in Jenkins and select «Pipeline script» as the definition.
- In the pipeline script, use the «sh» command to execute the Python script. Here’s an example:
pipeline agent any stages stage('Execute Python Script') steps sh 'python /path/to/your/script.py' > > > >
pipeline agent any stages stage('Execute Python Script') steps sh 'python /path/to/your/script.py arg1 arg2' > > > >
- If you want to capture the output of the Python script, you can use the «returnStdout» parameter of the «sh» command. Here’s an example:
pipeline agent any stages stage('Execute Python Script') steps script def output = sh ( script: 'python /path/to/your/script.py arg1 arg2', returnStdout: true ) println output > > > > >
In this example, the output of the Python script is captured in the «output» variable and printed using the «println» command.
pipeline agent any stages stage('Execute Python Script') steps script def output = python ( script: '/path/to/your/script.py arg1 arg2', returnStdout: true ) println output > > > > >
In this example, the «python» command is used to execute the Python script and capture its output in the «output» variable.
These are some examples of how to execute local Python scripts in Jenkins UI using the Jenkins Pipeline Script. You can customize these examples according to your requirements.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Python script execution job using the Jenkins pipeline
SharonKarichaly/Jenkins-Python-Pipeline
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
How to execute python script using the Jenkins pipeline
Jenkins is a continuous open-source integration written in Java. it is a Continuous Integration and Continuous delivery tool. We can orchestrate application deployments using Jenkins with a wide range of freely available community plugins and native Jenkins workflows.
Jenkins is commonly used for the following.
- Continuous Integration for application and infrastructure code.
- Continuously deliver pipeline to deploy the application to different environments using Jenkins Pipeline as a code
- Infrastructure component deployment and management.
- Run batch operations using Jenkins jobs.
- Run ad-hoc operations like backups, cleanups, remote script execution, event triggers, etc
Jenkins pipeline allows us to define a complete list of events that happen in the code lifecycle. Starting from the build, to testing and deployment. We can use a set of plugins that help in the implementation of certain processes as a continuous delivery pipeline. Where pipelines are defined using code by using groovy language to define the processes that would run in the pipeline.
Here we are executing a python script job using the Jenkins pipeline
In this project I pushed my project code to GitHub which is linked with Jenkins, which is responsible to execute a python script job using the Jenkins pipeline
This is a simple python script used to fetch the below details from a server.
2.IP address of the machine
3.Total available memory of the machine
4.Load average of the machine
5.Disk usage of the machine
I used socket and subprocess modules in the python to get this details.
Stage 1: Use checkout option in pipeline syntax generator to link the repository with the Jenkins server and select the branch where code is present. since it’s a public repository, I am not using any credentials/tokens.
Stage 2: Use shell script option in pipeline syntax generator to execute the python script
Stage 3: Use shell script option in pipeline syntax generator to redirect the terminal output to a file
Build and verify the stage logs
Login to the server and verify the details in /var/lib/jenkins/workspace/Repository_folder/
About
Python script execution job using the Jenkins pipeline