This repository contains the Jenkins pipeline for automating the continuous integration (CI) and continuous deployment (CD) of a project using a variety of DevOps tools and practices. The pipeline automates tasks such as repository cloning, Docker container management, code compilation, unit testing, code quality analysis with SonarQube, and deployment via Docker Compose. It also sends email notifications on pipeline success or failure.
All the scripts and configurations in this repository are fully commented, with detailed explanations provided within the files. This ensures that each part of the pipeline is easy to understand and modify as needed for different use cases. Whether you're new to Jenkins or experienced with DevOps, you’ll find clear comments throughout to guide you through the setup and execution process.
- Overview
- Prerequisites
- Pipeline Stages
- 1. Clone Git Repository
- 2. Clean Up Old Docker Containers
- 3. Verify and Start Essential Services
- 4. Compile Codebase
- 5. Execute Unit Tests
- 6. Generate JaCoCo Code Coverage Report
- 7. Perform SonarQube Code Analysis
- 8. Publish Artifacts to Nexus Repository
- 9. Build Docker Image for Application
- 10. Push Docker Image to Docker Hub
- 11. Deploy Application Using Docker Compose
- 12. Send Completion Notification Email
- How to Run the Pipeline
- Troubleshooting
- License
The JenkinsDevopsProject repository is designed to streamline the deployment and integration of software projects. This repository provides a Jenkins pipeline that integrates with various tools such as Maven, Docker, SonarQube, Nexus, and Docker Compose. The pipeline automates the following processes:
- Source Code Management (SCM): Clone code from GitHub repository
- Build and Test: Compile the codebase, run unit tests, and generate code coverage reports
- Code Quality: Perform SonarQube code analysis
- Artifact Management: Publish artifacts to Nexus repository
- Containerization: Build and push Docker images to Docker Hub
- Deployment: Deploy the application using Docker Compose
- Notifications: Email notifications for successful or failed builds
Before running this Jenkins pipeline, ensure the following prerequisites are met:
- Jenkins: A running Jenkins server with the necessary plugins installed, including:
- Git Plugin
- Docker Pipeline Plugin
- Email Extension Plugin
- Maven Integration Plugin
- SonarQube Scanner Plugin
- Nexus Artifact Uploader Plugin
- Docker CLI
- Docker: Docker and Docker Compose installed on the server running the Jenkins agent.
- Nexus: A running Nexus repository for storing artifacts.
- SonarQube: A running SonarQube instance for code quality analysis.
- Docker Hub: A Docker Hub account for pushing Docker images.
- Jenkins Credentials: Ensure that all the necessary credentials are set up in Jenkins Credentials Manager. The following credentials should be configured:
- Git Credentials: Set up your Git repository credentials (username and token or SSH key).
- SonarQube Token: Set up a SonarQube token (
SONAR_TOKEN) for authenticating with the SonarQube server. - Nexus Credentials: Set up your Nexus repository credentials (
NEXUS_USERandNEXUS_PASS) for artifact deployment. - Docker Hub Credentials: Set up your Docker Hub credentials for pushing the Docker images.
You can add these credentials via Jenkins > Manage Jenkins > Manage Credentials, ensuring each credential is accessible to the Jenkins job executing this pipeline.
This stage clones the project from a Git repository to the Jenkins workspace using the provided Git credentials. Replace the placeholder values with your specific repository details.
git branch: 'your-branch', credentialsId: 'git-id', url: 'your-git-repository-url'Removes any existing Docker containers from previous builds to ensure a fresh start. Replace the placeholder container names with your specific containers.
sh 'docker rm -f nameofyourcontainer || true'
sh 'docker rm -f nameofyoursqlcontainer || true'Starts the required services (SonarQube, Nexus, Prometheus, and Grafana) if not already running. The pipeline checks the health of each service using HTTP requests.
def services = ['nexus', 'prometheus', 'grafana']This stage compiles the source code using Maven.
sh 'mvn clean compile'Runs unit tests to ensure that the code behaves as expected.
sh 'mvn test'Generates a JaCoCo code coverage report to measure test coverage.
sh 'mvn jacoco:report'Analyzes the code using SonarQube to detect issues and measure code quality.
sh 'mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.token=$SONAR_TOKEN'Publishes the built artifacts to a Nexus repository using Maven.
sh 'mvn deploy -Dnexus.login=$NEXUS_USER -Dnexus.password=$NEXUS_PASS'Builds the Docker image for the application.
def imageName = "your-docker-image-name"
sh "docker build -t ${imageName} ."Pushes the Docker image to Docker Hub.
sh "docker push ${imageName}"Deploys the application using Docker Compose.
sh 'docker-compose up -d'Sends an email notification upon successful or failed completion of the pipeline.
emailext(subject: "Pipeline Build #${BUILD_NUMBER} Status", body: "Build details here", mimeType: 'text/html', to: 'your-email@example.com')- Clone the Repository: Clone the repository to your Jenkins server or configure the repository URL in the Jenkins job.
- Configure Jenkins: Ensure that Jenkins is set up with the required credentials and plugins.
- Run the Pipeline: Trigger the pipeline manually or through a GitHub push trigger.
- Error Starting Docker Containers: Ensure that Docker is installed and the services are correctly defined in the pipeline.
- SonarQube Analysis Issues: Verify that SonarQube is running and accessible from Jenkins.
- Email Notifications Not Working: Check the configuration of the Email Extension Plugin and the SMTP server settings.
This project is licensed under the MIT License - see the LICENSE file for details.