Skip to content

Automated CI/CD pipeline with Jenkins for dev & production branches. Includes testing, Docker builds, publishing to GitHub Packages & Docker Hub, and branch protection.

Notifications You must be signed in to change notification settings

imamaaa/multibranch-cicd-jenkins-with-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multibranch CI/CD Pipeline with Jenkins, Polling and Docker

CI/CD Status Jenkins Docker GitHub Packages Branch Protection

Project Overview

This project implements an automated CI/CD pipeline for a multi-branch GitHub repository using Jenkins and Docker. It establishes separate automated workflows for development (dev) and production (main) branches, enabling continuous integration and deployment with proper separation of concerns between environments.

The pipeline automatically builds, tests, and creates Docker images that are published to different registries based on the branch. Development builds are published to GitHub Packages while production builds can be optionally deployed to Docker Hub.

Key Features

  • Branch-Specific Workflows: Separate CI/CD pipelines for development (dev) and production (main) branches
  • Automated Testing: Continuous integration with automated tests for every code change
  • Docker Integration: Automatic Docker image building with environment-specific tagging
  • Multi-Registry Support: Publishes to GitHub Packages for development and Docker Hub for production
  • Parameterized Production Deploys: Optional deployment with customizable image tagging for production
  • Code Quality Gates: Branch protection rules ensure quality checks pass before merging to production
  • Automated Triggering: Jenkins polling configuration detects code changes automatically

Tech Stack

  • Version Control: GitHub with multi-branch workflow
  • CI/CD: Jenkins Multibranch Pipeline
  • Containerization: Docker with multi-registry support
  • Image Registries: GitHub Packages and Docker Hub
  • Automation: Jenkinsfile declarative pipelines
  • Monitoring: Slack integration (configured but pending notifications)
  • Security: Branch protection rules

How It Works

Development Workflow (dev branch)

  1. Developers commit code to the dev branch
  2. Jenkins polls for changes and automatically detects new commits
  3. The development pipeline is triggered, which:
    • Clones the repository
    • Builds the project
    • Runs unit and integration tests
    • Creates a Docker image tagged as dev
    • Publishes the image to GitHub Packages

Production Workflow (main branch)

  1. Development code is submitted to the main branch via Pull Request
  2. Branch protection rules enforce review requirements
  3. Once merged, Jenkins detects changes through polling
  4. The production pipeline executes with parameters:
    • Verifies code changes
    • Builds and tests the application
    • Creates a Docker image with customizable tag
    • Optionally deploys the image to Docker Hub when enabled

Setup Instructions

Prerequisites

  • Jenkins server with:
    • Multibranch Pipeline plugin
    • Docker Pipeline plugin
    • Git integration
  • Docker installed on Jenkins server
  • GitHub account with repository access
  • Docker Hub account (for production deployments)

Jenkins Configuration

  1. Create a new Multibranch Pipeline job in Jenkins
  2. Configure source code management:
    • Add GitHub repository URL
    • Configure credentials for repository access
  3. Set up branch sources:
    • Configure to discover branches and PRs
    • Add main and dev as branch specifiers
  4. Set up build configuration:
    • Mode: by Jenkinsfile
    • Script path: Jenkinsfile (for each branch)
  5. Configure polling trigger:
    • Set appropriate interval (e.g., H/5 * * * * for every 5 minutes)
  6. Save the configuration

GitHub Repository Setup

  1. Create repository with two branches: main and dev
  2. Add branch protection rules for main:
    • Require pull request reviews before merging
    • Require status checks to pass before merging
  3. Add Jenkinsfiles to both branches:
    • Jenkinsfile in the main branch
    • Jenkinsfile.dev in the dev branch

Future Improvements

  • Replace polling with GitHub webhooks for immediate pipeline triggering
  • Fix Slack notification issues to enable real-time build status alerts
  • Add vulnerability scanning for Docker images
  • Implement automated rollback mechanisms for failed deployments

Pipeline Visualization

graph TD
    A[Code Change] --> B{Branch?}
    B -->|dev| C[Dev Pipeline]
    B -->|main| D[Main Pipeline]
    
    C --> C1[Build]
    C1 --> C2[Test]
    C2 --> C3[Create Docker Image]
    C3 --> C4[Push to GitHub Packages]
    
    D --> D1[Build]
    D1 --> D2[Test]
    D2 --> D3[Create Docker Image]
    D3 --> D4{Deploy?}
    D4 -->|Yes| D5[Push to Docker Hub]
    D4 -->|No| D6[End Pipeline]
Loading

Security Considerations

  • Credentials are managed securely in Jenkins credentials store
  • Branch protection rules enforce code review before merging to main
  • Docker images use minimal base images to reduce attack surface

About

Automated CI/CD pipeline with Jenkins for dev & production branches. Includes testing, Docker builds, publishing to GitHub Packages & Docker Hub, and branch protection.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •