Setting up Jenkins on Ubuntu dedicated server

As a developer, often you are blocked by repetitive tasks that need be done. This is something that can become frustrating, but luckly we have some tools to help. In the following lines we will talk about Jenkins, an automation server written in Java.

Assumptions

  • Your Ubuntu has a public IP address
  • Your sudo username is: admin
  • Your host is: jenkins.example.com
  • You have a cname DNS entry for every subdomain of jenkins.example.com pointing to itself.

The setup

Please note that there are commands which are not mandatory and everything can be adapted to the needs of your system (e.g. You might choose not to use nginx)

Sudo user

  • sudo apt-get update
  • adduser admin
  • usermod -aG sudo admin
  • sudo passwd -l root
  • su - admin

The locale

  • locale
  • sudo apt-get install language-pack-en-base
  • sudo dpkg-reconfigure locales

Swap

If using digitalocean, swap is not recommended.

  • sudo swapon --show
  • sudo fallocate -l 4G /swapfile
  • ls -lh /swapfile
  • sudo chmod 600 /swapfile
  • ls -lh /swapfile
  • sudo mkswap /swapfile
  • sudo swapon /swapfile
  • sudo swapon --show

Make swap permanent

  • sudo nano /etc/fstab
    and add at the end:
    /swapfile none swap sw 0 0

Firewall

  • sudo ufw allow 8080
  • sudo ufw allow OpenSSH
  • sudo ufw enable

Setup java

  • sudo apt-get install default-jre

Jenkins

  • sudo apt-get install jenkins
  • sudo systemctl start jenkins
  • sudo systemctl status jenkins
  • sudo nano /var/lib/jenkins/secrets/initialAdminPassword
  • Access `http://jenkins.example.com:8080/` to finish setup.

“Package ‘jenkins’ has no installation candidate”

  • wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
  • sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
  • sudo apt-get update

“ubuntu 18.04 jenkins found an incorrect java version”

  • sudo add-apt-repository ppa:webupd8team/java
  • sudo apt install oracle-java8-installer
  • wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
  • sudo apt-add-repository "deb https://pkg.jenkins.io/debian-stable binary/"
  • sudo apt install jenkins

nginx

  • sudo apt-get update
  • sudo apt-get install nginx
  • sudo ufw allow 'Nginx HTTP'
  • sudo add-apt-repository ppa:certbot/certbot
  • sudo apt-get update
  • sudo apt-get install python-certbot-nginx
  • sudo nano /etc/nginx/sites-available/default
    • replace `server_name _` with server_name jenkins.example.com www.jenkins.example.com;
  • sudo nginx -t
  • sudo systemctl reload nginx
  • sudo ufw allow 'Nginx Full'
  • sudo ufw delete allow 'Nginx HTTP'
  • sudo certbot --nginx -d jenkins.example.com -d www.jenkins.example.com
  • Choose redirect all traffic to https://

Use Jenkins with ssl nginx

  • sudo nano /etc/nginx/sites-available/default
    • find try_files $uri $uri/ =404;
    • replace with:
      # try_files $uri $uri/ =404;
      include /etc/nginx/proxy_params;
      proxy_pass          http://localhost:8080;
      proxy_read_timeout  90s;
      # Fix potential "It appears that your reverse proxy set up is broken" error.
      proxy_redirect      http://localhost:8080 https://jenkins.example.com;
      
  • sudo nano /etc/default/jenkins
    • find
      JENKINS_ARGS="--webroot=/var/cache/$NAME/war --httpPort=$HTTP_PORT"
    • replace with:
      JENKINS_ARGS="--webroot=/var/cache/$NAME/war --httpPort=$HTTP_PORT --httpListenAddress=127.0.0.1"
  • sudo systemctl restart jenkins
  • sudo systemctl restart nginx
  • Go to: https://jenkins.example.com and choose: Manage jenkins > Configure system and update Jenkins location > Jenkins URL to https://jenkins.example.com.

Blueocean

For a better UI/UX experience, you could install the BlueOcean plugin, from the jenkins available plugins.

Docker

  • curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  • sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  • sudo apt-get update
  • apt-cache policy docker-ce
  • sudo apt-get install -y docker-ce

Sources