Jenkins: Comprehensive Guide

Jul 17, 2024

Jenkins: Comprehensive Guide

Introduction

  • Overview of Jenkins and its significance in DevOps
  • Engineers' salary range: $150,000 to $250,000 per year
  • Jenkins is used for automation of various tasks including code pipelines

Jenkins Basics

  • Jenkins is an automation platform for build, test, and deploy software using pipelines
  • It offers a web GUI for creating jobs and managing functionalities like source control, build actions, and triggers

Jenkins Infrastructure

  • Master Server: Controls the pipelines, schedules builds to agents
  • Agents: Run the build in their workspace
  • Workflow: Developer commits code -> Jenkins triggers pipeline -> Build distributed to agents
  • Types of Agents: Permanent and Cloud Agents
    • Permanent Agents: Standalone servers (Linux/Windows) with Java and SSH configured
    • Cloud Agents: Dynamic agents from Docker, Kubernetes, AWS, etc.

Setting Up Jenkins

  • Two main options: install directly on OS or use container environments like Docker/Kubernetes
  • Recommended to use Docker
  • Key setup steps: Build Docker image, create network, launch container, unlock Jenkins, install plugins

Jenkins UI Overview

  • Main Features: Dashboard, Manage Jenkins, New Item, etc.
  • Breadcrumbs: Easy navigation
  • Manage Tools: Plugins, Nodes, Credentials, Security settings
  • Job Creation: Freestyle Projects and Pipelines
    • Freestyle: Simple, UI-driven, suitable for beginners
    • Pipelines: Uses Jenkinsfiles with Groovy syntax, suitable for complex automated workflows

Creating Jenkins Jobs

  • Freestyle Projects: Use UI and plugins to define steps
    • Example: Echo “Hello World”, setup build environment, execute shell script
  • Pipeline Jobs: Define steps in Jenkinsfile
    • Example Pipeline Stages: Clone, Build, Test, Deploy

Advanced Jenkins Setup

  • Agents Configuration:
    • Permanent Node Agents
    • Cloud Agents using Docker
      • Ensure Docker setup on local/remote
      • Create and configure Docker agent templates
  • Build Triggers: Automatic builds using webhooks or SCM polling

Jenkinsfile and Pipelines

  • Defining Pipelines in Jenkinsfile:
    • Use Groovy syntax, declarative recommended for beginners
    • Define stages, agents, and steps

Example Jenkinsfile Contents:

pipeline {
  agent { label 'docker-agent-python' }
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
        sh 'cd my_app && pip install -r requirements.txt'
      }
    }
    stage('Test') {
      steps {
        echo 'Testing...'
        sh 'cd my_app && python3 hello.py'
      }
    }
    stage('Deploy') {
      steps {
        echo 'Deploying...'
      }
    }
  }
}
  • Pipeline Workflow:
    • Stages for building, testing, and deploying
    • Example involved running a Python script, and checking its output

Blue Ocean Plugin

  • Enhances CI/CD pipeline visualization
  • Easy navigation and troubleshooting of pipelines

Conclusion

  • Core skills covered: Setting up Jenkins, configuring agents, creating jobs, and writing Jenkins pipelines
  • Encouragement to explore further content and practical applications of Jenkins