Skip to content

Instantly share code, notes, and snippets.

@alexolinux
Created January 16, 2024 11:34
Show Gist options
  • Select an option

  • Save alexolinux/e78ba16a8e83f2899e5bc6bb1afb62fe to your computer and use it in GitHub Desktop.

Select an option

Save alexolinux/e78ba16a8e83f2899e5bc6bb1afb62fe to your computer and use it in GitHub Desktop.
Script to install Jenkins - Based on official documents from January, 2024
#!/bin/bash
#https://www.jenkins.io/doc/book/installing/linux/
# Functions
command_exists() {
command -v "$1" >/dev/null 2>&1
}
check_command_status() {
if [ $? -eq 0 ]; then
echo "$1 successful."
else
echo "Failed to $1. Check journalctl for logs."
exit 1
fi
}
# Requirements
packages=("wget" "fontconfig")
for package in "${packages[@]}"; do
if ! command_exists "$package"; then
echo "Installing $package..."
sudo yum install -y "$package"
else
echo "$package is already installed. Skipping $package installation."
fi
done
# Java is required
if ! command_exists java || [[ "$(java -version 2>&1 | grep 'java version')" == "" ]]; then
echo "Java not found or invalid version. Installing Java..."
sudo yum install -y java-17-openjdk
else
echo "Java is already installed. Skipping Java installation."
fi
# Jenkins repo
if [ ! -f "/etc/yum.repos.d/jenkins.repo" ]; then
echo "Adding Jenkins repository..."
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
else
echo "Jenkins repository already configured. Skipping repository setup."
fi
# Update packages
echo "Upgrading system packages..."
sudo yum upgrade -y
# Jenkins install
if ! command_exists jenkins; then
echo "Installing Jenkins..."
sudo yum install -y jenkins
else
echo "Jenkins is already installed. Skipping Jenkins installation."
fi
# Jenkins Daemon
echo "Reloading systemd..."
sudo systemctl daemon-reload
check_command_status "Reload systemd"
sudo systemctl enable --now jenkins.service
check_command_status "Enable and start Jenkins service"
echo "Installation completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment