Jul 26, 2024 3 min read

Jenkins: A Beginner-Friendly Guide to Self-Hosting

Jenkins: A Beginner-Friendly Guide to Self-Hosting
Table of Contents

Jenkins is an open-source automation server that empowers developers and system administrators to build, test, and deploy software projects efficiently. Its self-hosted nature allows for complete control over configurations, ensuring security and flexibility in CI/CD pipelines. In this guide, we’ll walk you through installing Jenkins, configuring it with a reverse proxy, managing logs, setting up backups, updating the app, and leveraging its unique features.

Installing Jenkins

πŸ“¦ Docker/Docker Compose Setup

To deploy Jenkins using Docker Compose, create a docker-compose.yml file tailored for Jenkins.


version: '3'

services:

jenkins:

image: jenkins/jenkins:lts

container_name: jenkins-server

ports:

- "8080:8080"

- "50000:50000"

volumes:

- jenkins_data:/var/jenkins_home

environment:

JAVA_OPTS: "-Djenkins.install.runSetupWizard=false"

volumes:

jenkins_data:

Run the following commands to deploy Jenkins:


nano docker-compose.yml

## Start Jenkins using Docker Compose

docker-compose up -d

## Verify that the container is running

docker ps

Access Jenkins at http://<your_server_ip>:8080.

πŸš€ Manual Installation

For manual installation on an Ubuntu-based server, follow these steps:


## Update and install prerequisites

sudo apt update

sudo apt install -y openjdk-11-jdk wget gnupg

## Add Jenkins's repository key and source

wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -

echo "deb https://pkg.jenkins.io/debian binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list

## Install Jenkins

sudo apt update

sudo apt install -y jenkins

## Start and enable the Jenkins service

sudo systemctl start jenkins

sudo systemctl enable jenkins

## Check the status

sudo systemctl status jenkins

Access Jenkins at http://<your_server_ip>:8080.

Configuring Nginx as a Reverse Proxy

🌐 Nginx Configuration

Create an Nginx server block to forward traffic to Jenkins:


## Install Nginx if not already installed

sudo apt install -y nginx

## Create a new Nginx configuration file

sudo nano /etc/nginx/sites-available/jenkins

## Add the following configuration:

server {

server_name yourdomain.com;

location / {

proxy_pass http://127.0.0.1:8080;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-Proto $scheme;

}

}

## Enable the configuration

sudo ln -s /etc/nginx/sites-available/jenkins /etc/nginx/sites-enabled/

## Test and reload Nginx

sudo nginx -t

sudo systemctl reload nginx

πŸ”’ SSL/TLS Setup

Secure Jenkins using Let's Encrypt:


## Install Certbot

sudo apt install -y certbot python3-certbot-nginx

## Obtain and install an SSL certificate

sudo certbot --nginx -d yourdomain.com

## Test automatic certificate renewal

sudo certbot renew --dry-run

πŸ› οΈ Testing and Reloading Nginx

After making changes, validate the configuration and reload Nginx:


sudo nginx -t

sudo systemctl reload nginx

Logging and Debugging Jenkins

πŸ—ƒοΈ Enabling Debug Logs

To enable debug-level logging, modify Jenkins’s logging configuration:

  1. Navigate to Manage Jenkins > System Log > Add New Log Recorder.

  2. Add a logger with the name DEBUG and set the log level to ALL.

πŸ“„ Viewing Logs

Access logs for Jenkins using Docker or the filesystem:


## View Docker logs

docker logs jenkins-server

## If installed manually, view logs in the filesystem

sudo tail -f /var/log/jenkins/jenkins.log

πŸ› οΈ Troubleshooting Common Issues

If Jenkins fails to start, check logs for errors like port conflicts:


## Check which process is using port 8080

sudo netstat -tuln | grep 8080

πŸ“€ Exporting Logs

Send logs to an external system like ELK Stack:


## Example: Forward logs using Filebeat

sudo apt install filebeat

sudo nano /etc/filebeat/filebeat.yml

## Configure Jenkins log paths under "paths"

Backup and Restore

πŸ—‚οΈ File-Based Backups

Backup Jenkins’s home directory:


## Create a tarball of the Jenkins home directory

sudo tar -czvf jenkins_backup.tar.gz /var/jenkins_home

πŸ”„ Database Backups

If you use a database plugin for Jenkins, back it up with its specific tool (e.g., mysqldump for MySQL).

πŸ“… Automated Backup Scripts

Set up periodic backups using cron:


## Create a backup script

echo 'sudo tar -czvf /backups/jenkins_$(date +%Y%m%d).tar.gz /var/jenkins_home' > backup.sh

chmod +x backup.sh

## Add to cron

crontab -e

## Add the following line to run daily at midnight

0 0 * * * /path/to/backup.sh

Updating and Upgrading Jenkins

⬆️ Updating Docker Images

Update Jenkins when using Docker:


## Pull the latest Jenkins image

docker pull jenkins/jenkins:lts

## Stop and remove the existing container

docker stop jenkins-server

docker rm jenkins-server

## Redeploy with updated image

docker-compose up -d

πŸ› οΈ Manual Updates

To update a manually installed Jenkins:


## Update the package index

sudo apt update

## Upgrade Jenkins

sudo apt install --only-upgrade jenkins

πŸ” Checking for Updates

Check for available updates within Jenkins by navigating to Manage Jenkins > Plugin Manager > Updates.

Leveraging Jenkins’s Unique Features

πŸ”§ Enabling APIs

Enable Jenkins REST APIs for automation:

  1. Go to Manage Jenkins > Configure Global Security.

  2. Enable Token-based API Authentication.

  3. Generate a token under your user settings.

Example API usage with curl:


curl -u your_username:your_api_token http://yourdomain.com/job/your_job_name/build

🌟 Advanced Configurations

Integrate third-party tools like GitHub or Slack:

  • Install the GitHub Integration and Slack Notification plugins.

  • Configure under Manage Jenkins > Configure System.

Wrapping Up

This guide has equipped you with practical steps to deploy, configure, and manage Jenkins in a self-hosted environment. By leveraging its powerful features, you gain complete control over your CI/CD pipelines. Start implementing the provided examples to customize Jenkins for your unique workflows and maximize its automation capabilities.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Selfhosted Ninja.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.