๐Ÿ”ง DevOps

Jenkins

Jenkins CI

An open-source CI/CD automation server. Automates build, test, and deployment with pipelines. Plugin ecosystem.

Detailed Explanation

Jenkins is an open-source CI/CD automation server, started in 2004 at Sun Microsystems, and is the most widely used in the world. Through an ecosystem of more than 1,800 plugins, it can integrate with almost any development tool, and because it is written in Java, it runs on multiple platforms.

The core of Jenkins is the pipeline. Using a Groovy DSL called a Jenkinsfile, you define the build, test, and deployment stages as code. Declarative Pipeline has good readability and is beginner-friendly, while Scripted Pipeline allows complex logic to be implemented flexibly. Pipeline code is managed together in the source repository, so version control is possible.

In terms of scalability, Jenkins supports a Master-Agent architecture. The Master orchestrates pipeline execution, and the actual builds are performed on Agents (workers). In Kubernetes environments, build environments can be dynamically provisioned with Pod Agents, which makes resource efficiency high.

Recently, SaaS solutions such as GitHub Actions, GitLab CI, and CircleCI have been rising, but Jenkins is still the standard for companies with on-premises environments or complex legacy builds. The management overhead is high, but its freedom of customization is unrivaled.

Code Example

// Jenkinsfile - Declarative Pipeline ์˜ˆ์ œ
pipeline {
    agent {
        kubernetes {
            yaml '''
            apiVersion: v1
            kind: Pod
            spec:
              containers:
              - name: maven
                image: maven:3.9-eclipse-temurin-17
                command: ['sleep', 'infinity']
              - name: docker
                image: docker:24-dind
                securityContext:
                  privileged: true
            '''
        }
    }

    environment {
        DOCKER_REGISTRY = 'registry.company.com'
        IMAGE_NAME = 'api-server'
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Build & Test') {
            steps {
                container('maven') {
                    sh 'mvn clean verify -Dmaven.test.failure.ignore=false'
                }
            }
            post {
                always {
                    junit '**/target/surefire-reports/*.xml'
                    jacoco execPattern: '**/target/*.exec'
                }
            }
        }

        stage('Build Image') {
            when {
                branch 'main'
            }
            steps {
                container('docker') {
                    sh """
                        docker build -t ${DOCKER_REGISTRY}/${IMAGE_NAME}:${BUILD_NUMBER} .
                        docker push ${DOCKER_REGISTRY}/${IMAGE_NAME}:${BUILD_NUMBER}
                    """
                }
            }
        }

        stage('Deploy to Staging') {
            when {
                branch 'main'
            }
            steps {
                sh "kubectl set image deployment/api-server api=${DOCKER_REGISTRY}/${IMAGE_NAME}:${BUILD_NUMBER} -n staging"
            }
        }
    }

    post {
        failure {
            slackSend channel: '#build-alerts', color: 'danger',
                      message: "Build Failed: ${env.JOB_NAME} #${env.BUILD_NUMBER}"
        }
    }
}

How It's Said in Practice

DevOps engineer: "We need to upgrade the Jenkins master, but we've been putting it off because of plugin compatibility. When should we schedule the maintenance window?"

Developer: "Before that, I think we need to fix the parts of the Jenkinsfile syntax that are being deprecated first. For example, using `agent` instead of the `node` block."

DevOps engineer: "Sounds good. Let's verify on a test Jenkins instance first, then set up a rollback plan and proceed."

Interviewer: "What methods did you use to reduce build time in Jenkins?"

Candidate: "I ran independent tests concurrently with parallel stages and reduced dependency download time by using caching. I also used Kubernetes Pod Agents to provision build environments quickly."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "You must not hardcode credentials in a Jenkinsfile. Change it to a withCredentials block."

Developer: "Yes, I'll register them in the Jenkins Credentials Store and update the code to reference them with the credentials() function."

Cautions

Learn More