What is Jenkins?
Jenkins is an open-source automation server widely used to automate parts of the software development lifecycle. It is primarily used for continuous integration (CI) and continuous delivery (CD), enabling developers to automatically build, test, and deploy applications.
Jenkins supports hundreds of plugins to integrate with virtually every tool in the software development ecosystem, making it highly customisable and adaptable to various workflows. Jenkins can manage and monitor builds, trigger automated testing, and push changes to production without manual intervention, significantly improving development speed and reliability.
Key Features:
Extensive Plugin Ecosystem – Integrates with tools like Git, Docker, Kubernetes, Maven, and more.
Pipeline as Code – Enables the definition of build and deployment pipelines through Jenkinsfiles.
Scalability – Can distribute workloads across multiple machines to handle large builds and tests.
Extensible – Custom plugins allow for integration with numerous other tools, enabling a highly flexible CI/CD workflow.
Prerequsites
- Virtual Machine running Ubuntu 22.04 or newer
Update Package Repository and Upgrade Packages
sudo apt update
sudo apt upgrade
Become root
sudo -i
Adoptium Java 17
Add Adoptium repository
wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | tee /etc/apt/keyrings/adoptium.asc
echo "deb [signed-by=/etc/apt/keyrings/adoptium.asc] https://packages.adoptium.net/artifactory/deb $(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | tee /etc/apt/sources.list.d/adoptium.list
Install Java 17
Update repository and install Java
apt update
apt install temurin-17-jdk
/usr/bin/java --version
exit
Install Jenkins
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get -y install jenkins
Start Jenkins
sudo systemctl daemon-reload
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
Print initial admin password
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Copy Temporary password
Firewall Settings if required
YOURPORT=8080
PERM="--permanent"
SERV="$PERM --service=jenkins"
firewall-cmd $PERM --new-service=jenkins
firewall-cmd $SERV --set-short="Jenkins ports"
firewall-cmd $SERV --set-description="Jenkins port exceptions"
firewall-cmd $SERV --add-port=$YOURPORT/tcp
firewall-cmd $PERM --add-service=jenkins
firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --reload
Enable TLS
Install Nginx
sudo apt install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx
Configure Nginx reverse proxy
sudo vim /etc/nginx/sites-available/jenkins.devopsfoundry.com
upstream jenkins {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name jenkins.devopsfoundry.com;
access_log /var/log/nginx/jenkins.access.log;
error_log /var/log/nginx/jenkins.error.log;
proxy_buffers 16 64k;
proxy_buffer_size 128k;
location / {
proxy_pass http://jenkins;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
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 https;
}
}
sudo ln -s /etc/nginx/sites-available/jenkins.devopsfoundry.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
Install Certbot and obtain SSL certificate
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d jenkins.devopsfoundry.com
Cloudflare Tunnel Config
tunnel: j-tunnel
credentials-file: /home/jenkins/.cloudflared/3e9dfb6f-63c9-4902-b48d-83aa2abb386a.json
ingress:
- hostname: jenkins.devopsfoundry.com
service: http://127.0.0.1:8080 # Another service using HTTP
- service: http_status:404 # Default for unmatched requests
Sample Jenkinsfile
pipeline {
agent any
stages {
stage('Hello World') {
steps {
echo 'Hello World'
}
}
}
}
Setup Jenkins Agent
Run the following commands on your Jenkins agent node:
sudo adduser jenkins
sudo usermod -aG sudo jenkins
sudo apt install -y fontconfig openjdk-17-jre
Grant admin user passwordless sudo access
sudo visudo
jenkins ALL=(ALL) NOPASSWD:ALL
Generate SSH key pair on Jenkins-UI/Master server
ssh-keygen -t rsa -b 4096 -C "jenkins-agent"
Copy the public key to the Jenkins agent
ssh-copy-id jenkins@<agent-ip-address>
Enter password for Jenkins-agent if prompted
Ensure SSH key-based authentication works, then logout
ssh jenkins@<agent-ip-address>
Configure known hosts for Jenkins-UI Complete this on UI
sudo mkdir -p /var/lib/jenkins/.ssh/
Change permission for known_hosts
sudo chown -R jenkins:jenkins /var/lib/jenkins/.ssh
sudo ssh-keyscan -H <agent-ip-address> >> /var/lib/jenkins/.ssh/known_hosts
Create Jenkins credential with SSH username with private Key
Add private key directly
cat jenkinsAgent_rsa.pub >> ~/.ssh/authorized_keys
Ensure that the permissions of the ~/.ssh directory is secure, as most ssh daemons will refuse to use keys that have file permissions that are considered insecure:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys ~/.ssh/jenkinsAgent_rsa