Skip to content

Instantly share code, notes, and snippets.

@WillSams
Last active January 29, 2019 22:08
Show Gist options
  • Save WillSams/b4423a58bfe8fe837238110cc3697b5e to your computer and use it in GitHub Desktop.
Save WillSams/b4423a58bfe8fe837238110cc3697b5e to your computer and use it in GitHub Desktop.
Install Jenkins CI & Configure Open-JDK 8 on Debian-based System
#!/bin/bash
echo "==================================================================="
echo
echo "Installs Jenkins & Open-JDK 8"
echo "You may be prompted for root credentials to complete the install."
echo
echo "==================================================================="
set -o nounset # unset variables are errors
SCRIPTVERSION="2019.01.29"
SCRIPTNAME="install-jenkins.sh"
SCRIPTFULLNAME="$0"
PORT='8080'
HEAPSIZE='512'
echoerror() { printf "\033[1;31m * ERROR\033[0m: %s\\n" "$@" 1>&2; }
usage() {
cat << EOT
Usage : ${SCRIPTNAME} [options]
Options:
-h Display this message
-v Display script version
-p Http port number desired for the Jenkins instance. Default is 8080.
-s Heap size desired for Jenkins to avoid performance issues with builds. Default is 512 MB.
EOT
} # ---------- end of function usage ----------
while getopts ':hvp:s:' opt
do
case "${opt}" in
h ) usage; exit 0 ;;
v ) echo "$0 -- Version $SCRIPTVERSION"; exit 0 ;;
p ) PORT=$OPTARG ;;
s ) HEAPSIZE=$OPTARG ;;
\?) echo
echoerror "Option does not exist : $OPTARG"
usage
exit 1
;;
esac # --- end of case ---
done
shift $((OPTIND-1))
if [ -f /etc/default/jenkins ]; then
echo "Jenkins is already installed on $HOSTNAME."
else
echo "************************ JENKINS INSTALL **************************"
echo "Installing Jenkins."
echo "You may be prompted for root credentials to complete the install."
echo "******************************************************************"
#Install Jenkins & compliant Java 8 dependency
wget -qO- https://pkg.jenkins.io/debian/jenkins.io.key | sudo bash -c "apt-key add"
sudo bash -c "echo 'deb http://pkg.jenkins.io/debian-stable binary/' >> /etc/apt/sources.list.d/jenkins.list"
sudo bash -c "apt update && apt upgrade -y"
sudo bash -c "apt install openjdk-8-jdk jenkins -y"
#Set JAVA_HOME to openJDK-8
sudo bash -c "update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java"
sudo bash -c "echo 'JAVA_HOME=\"/usr/lib/jvm/java-8-openjdk-amd64/jre\"' >> /etc/environment"
source /etc/environment
#Set port value in Jenkins configuration if an alternate port was passed to the script
if [ "$PORT" != "8080" ]; then
sudo bash -c "sed -i -e 's/HTTP_PORT=8080/HTTP_PORT=$PORT/g' /etc/default/jenkins"
fi
#Set maximum heap size for Jenkins
sudo bash -c "sed -i -e 's/#JAVA_ARGS=\"-Xmx256m\"/JAVA_ARGS=\"-Xmx$HEAPSIZEm\"/g' /etc/default/jenkins"
sudo bash -c "ufw allow $PORT && ufw enable"
sudo bash -c "systemctl enable jenkins.service && service jenkins restart"
INITPASSWD=$( sudo bash -c "cat /var/lib/jenkins/secrets/initialAdminPassword" )
echo "$SCRIPTFULLNAME ($SCRIPTVERSION) complete."
echo "Browse http://$HOSTNAME:$PORT to unlock Jenkins with this password:"
echo $INITPASSWD
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment