23 Jul, 2018

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

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.

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

Use Jenkins with ssl nginx

  • sudo nano /etc/nginx/sites-available/default
    • find try_files $uri $uri/ =404;
    • replace with:
  • 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

Sources