Create Jenkins CI/CD Pipeline on Linux

LinuxLinuxBeginner
Practice Now

Introduction

If you're experiencing issues with the Jenkins service not starting up properly on your Linux system, this tutorial is for you. We'll dive into the common startup problems, diagnose the errors, and provide step-by-step solutions to get your Jenkins server up and running smoothly, even when "sudo systemctl start jenkins" is not working as expected.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/TextProcessingGroup -.-> linux/sed("`Stream Editing`") linux/TextProcessingGroup -.-> linux/awk("`Text Processing`") linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/top("`Task Displaying`") linux/VersionControlandTextEditorsGroup -.-> linux/vim("`Text Editing`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") linux/SystemInformationandMonitoringGroup -.-> linux/service("`Service Managing`") subgraph Lab Skills linux/grep -.-> lab-392790{{"`Create Jenkins CI/CD Pipeline on Linux`"}} linux/sed -.-> lab-392790{{"`Create Jenkins CI/CD Pipeline on Linux`"}} linux/awk -.-> lab-392790{{"`Create Jenkins CI/CD Pipeline on Linux`"}} linux/ps -.-> lab-392790{{"`Create Jenkins CI/CD Pipeline on Linux`"}} linux/top -.-> lab-392790{{"`Create Jenkins CI/CD Pipeline on Linux`"}} linux/vim -.-> lab-392790{{"`Create Jenkins CI/CD Pipeline on Linux`"}} linux/nano -.-> lab-392790{{"`Create Jenkins CI/CD Pipeline on Linux`"}} linux/service -.-> lab-392790{{"`Create Jenkins CI/CD Pipeline on Linux`"}} end

Jenkins Basics

Introduction to Jenkins

Jenkins is a powerful open-source automation server designed for continuous integration and continuous deployment (CI/CD) workflows. As a critical DevOps automation tool, Jenkins enables software development teams to streamline and automate various stages of software delivery.

Core Concepts and Architecture

Jenkins operates on a master-slave architecture that supports distributed build environments. Key components include:

graph TD A[Jenkins Master] --> B[Build Nodes] A --> C[Plugins] A --> D[Job Configurations]
Component Description
Master Central control unit managing jobs and nodes
Nodes Execution environments for build tasks
Plugins Extend Jenkins functionality

Installation on Ubuntu 22.04

## Update system packages
sudo apt update

## Install Java Runtime Environment
sudo apt install openjdk-11-jre-headless

## Import Jenkins repository key
wget -q -O -  | sudo apt-key add -

## Add Jenkins repository
sudo sh -c 'echo deb  binary/ > /etc/apt/sources.list.d/jenkins.list'

## Install Jenkins
sudo apt update
sudo apt install jenkins

Basic Configuration and First Job

After installation, Jenkins provides a web interface for configuration and job management. The initial setup involves:

  1. Accessing web interface at
  2. Retrieving initial admin password
  3. Installing recommended plugins
  4. Creating first build job

Sample Jenkins Pipeline Script

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('Deploy') {
            steps {
                sh 'docker build -t myapp .'
            }
        }
    }
}

This script demonstrates a typical CI/CD workflow with build, test, and deployment stages.

Linux Service Setup

Understanding Linux Services

Linux services are background processes that run continuously, providing essential system functionality or supporting application operations. In Ubuntu 22.04, systemd is the primary service management framework.

Systemd Service Management

graph TD A[Service Definition] --> B[Unit File] B --> C[Service Configuration] C --> D[Service State] D --> E[start/stop/restart]

Jenkins Service Configuration

Command Function
systemctl start jenkins Start Jenkins service
systemctl stop jenkins Stop Jenkins service
systemctl status jenkins Check service status
systemctl enable jenkins Enable automatic startup

Creating Custom Service Unit File

## Create service unit file
sudo nano /etc/systemd/system/myapp.service

[Unit]
Description=Custom Application Service
After=network.target

[Service]
ExecStart=/usr/bin/myapp
User=myuser
Restart=always

[Install]
WantedBy=multi-user.target

Service Management Commands

## Reload systemd configuration
sudo systemctl daemon-reload

## Start service
sudo systemctl start myapp

## Enable service on boot
sudo systemctl enable myapp

## Check service status
sudo systemctl status myapp

Troubleshooting Service Issues

## View service logs
journalctl -u jenkins

## Check service dependencies
systemctl list-dependencies jenkins

## Analyze service performance
systemd-analyze verify jenkins.service

Jenkins Workflow

Jenkins Pipeline Fundamentals

Jenkins Pipeline provides a powerful framework for defining continuous delivery workflows as code. It enables developers to describe complex build, test, and deployment processes using a declarative or scripted syntax.

graph TD A[Code Commit] --> B[Source Control] B --> C[Build Stage] C --> D[Automated Testing] D --> E[Deployment] E --> F[Production Validation]

Pipeline Types

Pipeline Type Characteristics
Declarative Simplified syntax, predefined structure
Scripted More flexible, full Groovy programming capabilities

Sample Declarative Pipeline

pipeline {
    agent any
    
    stages {
        stage('Checkout') {
            steps {
                git '
            }
        }
        
        stage('Build') {
            steps {
                sh 'mvn clean package'
            }
        }
        
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        
        stage('Deploy') {
            steps {
                sh 'docker build -t myapp .'
                sh 'docker push myregistry/myapp'
            }
        }
    }
    
    post {
        success {
            echo 'Deployment completed successfully'
        }
        failure {
            echo 'Deployment failed'
        }
    }
}

Advanced Workflow Strategies

## Jenkins CLI for workflow management
java -jar jenkins-cli.jar -s  build job-name

## Parameterized builds
jenkins-job-builder --conf config.yaml

Deployment Configurations

graph LR A[Development] --> B[Staging] B --> C[Production] C --> D[Rollback/Monitoring]

Environment-Specific Deployments

stage('Deploy') {
    when {
        branch 'main'
    }
    steps {
        script {
            if (env.BRANCH_NAME == 'main') {
                sh 'kubectl apply -f k8s/production/'
            } else if (env.BRANCH_NAME == 'develop') {
                sh 'kubectl apply -f k8s/staging/'
            }
        }
    }
}

Summary

By the end of this guide, you'll have a comprehensive understanding of the Jenkins service startup process on Linux, the ability to identify and resolve various startup issues, and the knowledge to configure and automate the management of your Jenkins service for a more reliable and efficient development environment. Whether you're a DevOps engineer, system administrator, or a developer working with Jenkins on Linux, this tutorial will equip you with the necessary skills to troubleshoot and overcome "sudo systemctl start jenkins not working" problems.

Other Linux Tutorials you may like