Jenkins CI/CD Pipeline
This project uses a Jenkins declarative pipeline to automate the entire deployment process, from source code checkout to running the containerized application. The pipeline ensures consistent, repeatable deployments with automatic notifications.Complete Jenkinsfile
Pipeline Structure
Agent Configuration
- Tells Jenkins to run the pipeline on any available agent
- For simple setups, this runs on the Jenkins master node
- Can be configured to run on specific agents with Docker or Node.js capabilities
Pipeline Stages
Stage 1: Checkout
Clone the source code from GitHub.What happens:
- Jenkins clones the repository from GitHub
- Checks out the
mainbranch - Pulls the latest code including Dockerfile, docker-compose.yml, and application source
- Creates a fresh workspace for the build
This stage runs on every pipeline execution, ensuring you always build from the latest code.
Stage 2: Build Docker Image
Build the Docker image using the multi-stage Dockerfile.What happens:
- Executes
docker buildin the workspace directory - Uses the multi-stage Dockerfile to build the image
- Tags the resulting image as
react-express-app - Runs all three stages:
- Frontend build (React compilation)
- Backend build (Express setup)
- Production assembly
- npm install for frontend dependencies
- React production build
- npm install for backend dependencies
- Final image assembly
Docker layer caching speeds up subsequent builds. If dependencies haven’t changed, cached layers are reused.
Stage 3: Run Container
Stop any existing container and start a new one with the fresh image.Command breakdown:Stopping the old container:
docker ps -q --filter "name=react-express-app"- Find container by namegrep -q .- Check if a container was founddocker stop react-express-app- Stop the running containerdocker rm react-express-app- Remove the stopped container|| true- Don’t fail if no container exists (first deployment)
-d- Run in detached mode (background)-p 5000:5000- Map port 5000 from container to host--name react-express-app- Name the containerreact-express-app- Use the image built in Stage 2
Post-Build Actions
The pipeline includes post-build notifications:- All stages completed without errors
- Container is running and accessible at
http://localhost:5000 - Displays success message in Jenkins console
- Any stage failed (checkout, build, or deployment)
- Displays failure message
- Build is marked as failed in Jenkins
These can be extended to send Slack notifications, emails, or trigger other actions.
Setting Up Jenkins
Install Required Plugins
Ensure Jenkins has these plugins:
- Git Plugin - For repository checkout
- Docker Plugin - For Docker commands
- Pipeline Plugin - For declarative pipeline syntax
Configure Docker Access
Jenkins needs permission to run Docker commands:This adds the
jenkins user to the docker group.Create Pipeline Job
In Jenkins:
- Click “New Item”
- Enter job name:
react-express-app-pipeline - Select “Pipeline” project type
- Click “OK”
Configure Pipeline
In the job configuration:Option 1: Pipeline from SCM
- Definition: Pipeline script from SCM
- SCM: Git
- Repository URL:
https://github.com/mani-6666/Mini-Project.git - Branch:
main - Script Path:
Jenkinsfile
- Definition: Pipeline script
- Paste the complete Jenkinsfile content
Running the Pipeline
Monitor Progress
Watch the pipeline stages execute:
- Checkout (blue)
- Build Docker Image (blue)
- Run Container (blue)
View Console Output
Click on the build number, then “Console Output” to see:
- Git clone logs
- Docker build progress
- Container startup messages
Pipeline Execution Flow
Webhook Integration (Optional)
Automate deployments on every Git push:Configure GitHub Webhook
In GitHub repository settings:
- Go to Settings → Webhooks → Add webhook
- Payload URL:
http://your-jenkins-url/github-webhook/ - Content type:
application/json - Trigger: “Just the push event”
main branch automatically triggers the pipeline.
Troubleshooting
Build Fails at Docker Build Stage
Check:- Docker is installed and running on Jenkins agent
- Jenkins user has Docker permissions
- Dockerfile syntax is correct
Container Fails to Start
Check:- Port 5000 is available (not used by another process)
- Docker image built successfully
- Application code has no runtime errors
Permission Denied Errors
Solution:Enhancing the Pipeline
Consider these improvements:Automated Testing
Add a test stage before building:
Environment Variables
Use Jenkins credentials for sensitive data:
Slack Notifications
Send notifications to Slack channels on success/failure
Multi-Environment
Deploy to dev, staging, and production environments
Next Steps
Docker Build
Deep dive into the multi-stage Dockerfile
Docker Compose
Learn about container orchestration