G
GuideDevOps
Lesson 4 of 11

Jenkins

Part of the CI/CD Pipelines tutorial series.

Jenkins is the industry standard for on-premise and self-hosted automation. It uses a Jenkinsfile to define the pipeline logic using a Groovy-based syntax.

1. Declarative Pipeline (Recommended)

Modern Jenkins uses "Declarative Pipelines," which are easier to read and maintain than the older "Scripted Pipelines."

Jenkinsfile:

pipeline {
    agent any
 
    environment {
        APP_ENV = 'production'
    }
 
    stages {
        stage('Build') {
            steps {
                echo 'Building application...'
                sh 'make build'
            }
        }
        stage('Test') {
            steps {
                echo 'Running unit tests...'
                sh 'npm test'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying to server...'
                sh './deploy.sh'
            }
        }
    }
    
    post {
        always {
            echo 'Pipeline finished.'
        }
        failure {
            echo 'Deployment FAILED! Sending notification...'
        }
    }
}

2. Running a Job

Setup the Job

  1. Create a New Item in Jenkins.
  2. Select Pipeline.
  3. In the "Pipeline" section, select Pipeline script from SCM.
  4. Point it to your Git repository.

Check the Execution

Action: Click Build Now in the Jenkins UI.

Result: The Console Output will show:

[Pipeline] { (Build)
[Pipeline] echo
Building application...
[Pipeline] sh
+ make build
[Pipeline] }
[Pipeline] { (Test)
...
Finished: SUCCESS

3. Jenkins Architecture

  • Controller (Master): The central server that manages the UI, configurations, and job scheduling.
  • Agents (Slaves): The workers that actually execute the builds. Agents allow Jenkins to scale across dozens of servers.
  • Plugins: Jenkins has 1,800+ plugins for Docker, Kubernetes, AWS, Slack, and more.

4. Managing Credentials

Never hardcode passwords. Use the Jenkins Credentials Provider.

Action (Jenkinsfile snippet):

    environment {
        DB_PASSWORD = credentials('my-db-password-id')
    }

Summary

  • Jenkinsfile: The code that defines your pipeline.
  • Controller/Agent: How Jenkins scales execution.
  • Plugins: The secret to Jenkins' flexibility.
  • Self-Hosted: You have full control, but you also have to manage the server.