JENKINS CI/CD PIPELINE FOR PYTHON PROJECT
Published: 2024-02-23 09:10:12

Hey Reader,

My name is Seenevasaraj, and I am working as Software Developer at Luxoft India. The various project at Luxoft, I am implementing automation in all viable way. Through this article I desired to provide an explanation about Jenkins and how it is helps in CI/CD process and how you can setup python environment in jenkins.


**<u>What is CI/CD?</u>**
CI/CD is development approach that automates the integration testing and delivery of code updates to production environments. The CI/CD process typically includes the following steps: 
1. **Continuous Integration**: Developers merge their code changes into a shared repository regularly, often several times a day. 

2. **ContinuousTesting**: Automated tests are run on the merged code to check for errors andensure the codeworks as expected.

3. **ContinuousDelivery(orDeployment)**: Once the testing passes the code is automatically deployed to a production environment or a testing environment that closely resembles production. This way developers can quickly and reliably deliver new features bug fixes, and updates to production, while maintaining a high quality of software.


<u>**CI/CD depends on**</u>
1. **Version ControlSystem**: - Git (or) other centralized systems allow developers to commit their code changes
2. **Automated Build Tools**: - Tools like Jenkins,Travis CI or GitLab CI automatically build the application when code changes are pushed to the repository
3. **Automated Testing**: - Unit tests integration ,other tests run automatically during the CI/CD process to ensure code quality
4. **Artifact Repositories**: - A storage location for built artifacts like Dockerimages or compiled binaries produced during the CI/CD process 


CI/CD (continuous integration and continuous delivery) is an essential modern softwaredevelopment practice that streamlines processes enhances code quality and makes deployments more reliable It enables Agile&DevOps teams to rapidly develop test and deliver software

**<u>What Is jenkins?</u>**
Jenkins is a popular open-source tool that helpsSsoftware development teams automate their tasks It is written in Java and assists in building testing and deploying applications With Jenkins teams can implement ContinuousIntegration(CI) and ContinuousDelivery(CD) pipelines enabling them to automate the development process ensuring the quality and consistency of their software.

1. **ContinuousIntegration:** - Jenkins integrates code changes into a centralized repository continually. - It triggers automatic builds when changes are pushed to version control systems. 

2. **Extensibility:** - Jenkins has large ecosystem of plugins that enhance its capabilities Plugins integrate with version control systems, build tools testingframeworks and deployment platforms. 

3. **BuildAutomation:** - Jenkins automates the build process for various programming languages and build tools. - It compiles code, executes tests, and generates deployable artifacts

4. Jenkins allows teams to define build pipelines using code providing version control transparency and flexibility for complex CI/CD workflows 

5. Jenkins supports distributed builds enabling teams to efficiently scale their build infrastructure horizontally resulting in faster build times and optimal resource usage.

6. Jenkins integrates with external systems like issue trackers collaboration platforms and cloud providers facilitating seamless collaboration issue tracking, and cloud resource management.

7. **Monitoring and Reporting:** Jenkins has dashboards and reports that let you keep track of the status of your builds and pipelines It also gives you insights into how long builds take how tests are doing and other things. This can help teams find bottlenecks and make their CI/CD processes more efficient
 
8. **Security and Access Control:** Jenkins has strong security features like authentication, authorization and encryption It can also be integrated with LDAP Active Directory and other identity providers This lets organizations set up access controls and keep track of who did what

<u>**Declaring Python Path In Jenkins**</u>

In order to execute python scripts in jenkins we have to declare our python.exe and pip.exe path in jenkins environmental variable section.

1. Log in Jenkins instance and enter the dashboard. 

2. On the left-hand menu click the "Manage Jenkins" link.

3. Under the "Manage Jenkins" page, select the option that says "Configure System." Here you can modify the global settings of your Jenkins instance.

4. Locate the "Global properties" Section: - Scroll down the page until you find the "Global properties" section. 

5. Activate"Environment variables"- In the "Global properties" section check the "Environment variables" box to enable it.Add an Environment Variable- Click "Add" button to create a new environment variable.
Specify Name and Value- In the "Name" field, enter the name of the environment variable (e.g. "MY_VARIABLE"). - In the "Value" field enter the value for the environment variable (e.g."my_value")

After declaring your python path, you can use this variable across shell or batch scripts as user requirements.


**<u>Jenkins New PIPELINE Creation</u>**

1. **Install Jenkins:** * Download and install Jenkins on your server or computer if not already installed.Refer to the official Jenkins website for installation instructions. 

2. **Install Pipeline Plugin:** * The Pipeline plugin supports continuous delivery pipelines in Jenkins Ensure it installed on your Jenkins instance From the Jenkins dashboard go to "Manage Jenkins">"Manage Plugins">"Available." * Search for "Pipeline" and install the plugin.

3. Log in Jenkins account and choose "New Item" to create a new job Put a name for your pipeline task and mark "Pipeline" as the task type, Click "OK".

4. Set up the Pipeline: - In the pipeline configuration, you can pick how to write your pipeline using Jenkinsfile or Scripted Pipeline. - If you want to use a Jenkinsfile, mark "Pipeline script from SCM" as the definition and tell Jenkins where your Jenkinsfile is, like in Git. - If you'd rather write the pipeline script directly in the Jenkins job settings, choose "Pipeline script" and put your script in the provided box.

5. Crafting a Pipeline Script: Shape your CI/CD pipeline script using either the Declarative or Scripted syntax. - With the Declarative Pipeline approach, structure and conciseness are at your disposal. - The Scripted Pipeline option grants you the flexibility of Groovy scripting. Visualize your pipeline's journey through stages and steps, employing directives to orchestrate each stage's behavior. Example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uycf9jh9nat2mvblaqmx.png)

6. **Save and Run Pipeline**: - Save the configuration of your pipeline. - Jenkins automatically starts a build based on the trigger you set (like a webhook or cron job). - Manually start the pipeline using the "Build Now" button in the Jenkins job dashboard. 


**<u>Conclusion</u>**
Jenkins is automation tool that helps software development teams implement Continuous Integration(CI)&Continuous Delivery (CD) practices, it allows them to automate various tasks associated with software development and deployment making the process more efficient and streamlined