#!/usr/bin/env bash
# install-jenkins.sh
# Jenkins — Self-hosted automation server for building, testing, and deploying pipelines.
# Target: Ubuntu / Debian. Safe to re-run.
# Source guide: devops-hub/tools/jenkins.html

set -euo pipefail

if [[ $EUID -eq 0 ]]; then
  echo "Run this as a regular sudo-capable user, not root." >&2
  exit 1
fi

echo "==> Install Java (Jenkins requires a JDK)"
sudo apt update
sudo apt install -y fontconfig openjdk-17-jre

echo "==> Add the Jenkins repository"
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

echo "==> Install Jenkins"
sudo apt-get update
sudo apt-get install -y jenkins

echo "==> Enable and start the service"
sudo systemctl enable --now jenkins

echo "==> Get the initial admin password"
sudo cat /var/lib/jenkins/secrets/initialAdminPassword

echo "==> Done. Jenkins installation finished."
