# Jenkins CI/CD Pipeline Stack

> Jenkins with build agents, SonarQube for code quality, and Nexus for artifact management

- Category: DevOps & CI/CD
- Difficulty: advanced
- Components: jenkins, jenkins-agent, sonarqube, nexus, postgres
- Tags: jenkins, ci-cd, sonarqube, nexus, devops, build
- Source: https://docker.recipes/devops/jenkins-cicd-pipeline

## Overview

Jenkins is the world's leading open-source automation server, originally developed by Kohsuke Kawaguchi in 2004 as Hudson before becoming Jenkins in 2011. With over 1800+ plugins and a massive community, Jenkins enables organizations to implement sophisticated CI/CD pipelines that automate building, testing, and deploying applications across diverse technology stacks. Its extensible architecture and Pipeline as Code capabilities make it the backbone of DevOps automation for millions of projects worldwide.

This comprehensive CI/CD stack combines Jenkins with essential enterprise tooling to create a complete software delivery pipeline. Jenkins orchestrates the automation workflows, while distributed Jenkins agents handle the actual build execution to scale processing power. SonarQube performs deep static code analysis to catch bugs, security vulnerabilities, and code smells before they reach production. Nexus Repository serves as the central artifact management hub, storing and distributing build outputs, dependencies, and container images. PostgreSQL provides enterprise-grade data persistence for SonarQube's analysis results and historical metrics.

This stack is ideal for medium to large development teams who need professional-grade CI/CD infrastructure with code quality gates and artifact management. Organizations adopting DevOps practices, migrating from legacy build systems, or requiring compliance with code quality standards will benefit from this integrated approach. The combination provides the foundation for implementing GitOps workflows, automated testing pipelines, security scanning, and release management across multiple projects and teams.

## Features

- Jenkins Pipeline as Code with Jenkinsfile support for version-controlled build definitions
- Distributed build execution using Jenkins inbound agents for scalable processing power
- SonarQube static code analysis covering 30+ programming languages with security vulnerability detection
- Nexus Repository for Maven, npm, Docker, and custom artifact storage with repository proxying
- Quality gates integration between Jenkins and SonarQube to block deployments on code quality failures
- Blue Ocean modern UI for visual pipeline creation and monitoring
- PostgreSQL-backed SonarQube data persistence with advanced query capabilities for reporting
- Jenkins credential management system for secure API keys, passwords, and certificates

## Use cases

- Enterprise software delivery pipelines with multiple development teams and complex deployment requirements
- Legacy system modernization where existing Jenkins infrastructure needs integration with quality tools
- Compliance-driven development environments requiring code quality metrics and audit trails
- Multi-language development shops needing centralized artifact management across Java, JavaScript, Python, and .NET projects
- Organizations implementing shift-left security practices with automated vulnerability scanning in CI/CD
- DevOps transformation initiatives requiring observable, measurable build and deployment processes
- Container-based application delivery with Docker image scanning and registry management

## Prerequisites

- Docker host with minimum 8GB RAM (Jenkins 2GB + SonarQube 4GB + Nexus 1GB + PostgreSQL 1GB)
- Available ports 8080 (Jenkins), 9000 (SonarQube), 8081 (Nexus), and 50000 (Jenkins agent communication)
- Basic understanding of CI/CD concepts, build pipelines, and version control systems
- Familiarity with Jenkins Pipeline syntax (Groovy-based) for creating Jenkinsfile build scripts
- Knowledge of static code analysis principles and quality gate configuration
- Experience with artifact repository concepts and dependency management tools like Maven or npm

## docker-compose.yml

```yaml
services:
  jenkins:
    image: jenkins/jenkins:lts-jdk17
    container_name: jenkins
    restart: unless-stopped
    ports:
      - "${JENKINS_PORT:-8080}:8080"
      - "50000:50000"
    volumes:
      - jenkins_home:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - JAVA_OPTS=-Djenkins.install.runSetupWizard=false
    user: root

  jenkins-agent:
    image: jenkins/inbound-agent:latest
    container_name: jenkins-agent
    restart: unless-stopped
    environment:
      - JENKINS_URL=http://jenkins:8080
      - JENKINS_AGENT_NAME=agent-1
      - JENKINS_SECRET=${AGENT_SECRET}
      - JENKINS_AGENT_WORKDIR=/home/jenkins/agent
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - agent_workspace:/home/jenkins/agent
    depends_on:
      - jenkins

  sonarqube:
    image: sonarqube:lts-community
    container_name: sonarqube
    restart: unless-stopped
    ports:
      - "${SONAR_PORT:-9000}:9000"
    environment:
      - SONAR_JDBC_URL=jdbc:postgresql://sonar-db:5432/sonar
      - SONAR_JDBC_USERNAME=${SONAR_DB_USER:-sonar}
      - SONAR_JDBC_PASSWORD=${SONAR_DB_PASSWORD:-sonar}
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
    depends_on:
      - sonar-db

  sonar-db:
    image: postgres:15-alpine
    container_name: sonar-db
    restart: unless-stopped
    environment:
      - POSTGRES_USER=${SONAR_DB_USER:-sonar}
      - POSTGRES_PASSWORD=${SONAR_DB_PASSWORD:-sonar}
      - POSTGRES_DB=sonar
    volumes:
      - sonar_db_data:/var/lib/postgresql/data

  nexus:
    image: sonatype/nexus3:latest
    container_name: nexus
    restart: unless-stopped
    ports:
      - "${NEXUS_PORT:-8081}:8081"
    volumes:
      - nexus_data:/nexus-data

volumes:
  jenkins_home:
  agent_workspace:
  sonarqube_data:
  sonarqube_extensions:
  sonar_db_data:
  nexus_data:
```

## .env

```bash
# Jenkins CI/CD Pipeline Stack
JENKINS_PORT=8080
SONAR_PORT=9000
NEXUS_PORT=8081

# Jenkins Agent (get from Jenkins UI after setup)
AGENT_SECRET=your-agent-secret

# SonarQube Database
SONAR_DB_USER=sonar
SONAR_DB_PASSWORD=sonar
```

## Usage notes

- Jenkins at http://localhost:8080
- Initial Jenkins password: docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
- SonarQube at http://localhost:9000 (admin/admin)
- Nexus at http://localhost:8081 (admin password in logs)
- Configure Jenkins to use SonarQube and Nexus
- Install Docker Pipeline and SonarQube Scanner plugins in Jenkins

## Troubleshooting

- Jenkins shows 'Reverse proxy setup is broken' warning: Configure JENKINS_OPTS with proper URL or disable the check with -Dhudson.diagnosis.ReverseProxySetupMonitor.enabled=false
- SonarQube fails to start with 'max virtual memory areas too low': Increase vm.max_map_count on Docker host with 'sysctl -w vm.max_map_count=524288'
- Jenkins agent fails to connect with 'Connection refused': Verify JENKINS_SECRET environment variable matches the agent secret from Jenkins master configuration
- Nexus shows 'Unable to write to nexus-data directory': Fix volume permissions with 'chown -R 200:200 nexus_data' on the Docker host
- SonarQube database connection errors: Ensure PostgreSQL is fully initialized before SonarQube starts by adding health checks or startup delays
- Jenkins Pipeline fails with Docker permission denied: Add Jenkins user to docker group or run Jenkins container with proper Docker socket permissions

