Laravel Jenkins CI

This article covers installation of Jenkins on Ubuntu server and its usage to continuously integrate a Laravel application.  Besides LAMP/LEMP stack we need to install Java, Git, Composer, and Node to successfully use Jenkins. Before starting to install this software, let’s take care of miscellaneous  stuff.

Miscellaneous (can skip this).

Create mysql user and database.

mysql -u root -p
mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
mysql> CREATE DATABASE test
mysql> GRANT ALL PRIVILEGES ON test . * TO 'newuser'@'localhost'; 
mysql> FLUSH PRIVILEGES;

Allocate swap file (if needed)

If your server/instance has less than 2G of RAM, you may want to allocate a swap file to handle composer’s memory intensive downloads.

sudo fallocate -l 1G /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo chmod 600 /swapfile

Prerequisites (depending on your server configuration you may not need to install everything).

Git

sudo apt-get install git –y

Composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Node

sudo su 
curl --silent --location https://deb.nodesource.com/setup_8.x | bash -
apt-get install nodejs –y
/usr/bin/npm install -g npm
exit

Java

sudo apt-get install default-jre -y

Installing Jenkins

Add the key:

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -

Add Jenkins package source to your package manager sources list by amending /etc/apt/sources.list with the following line:
deb https://pkg.jenkins.io/debian-stable binary/
Install and start Jenkins

sudo apt-get update
sudo apt-get install jenkins -y
sudo service jenkins start

Firewall Permissions

Jenkins run on port 8080. If you have your instance on AWS, you need to add port 8080 to your security group. If you have Ubuntu firewall enabled (UFW), you need to add port 8080 to firewall rules. To check if UFW is enabled type

sudo ufw status verbose

If Status: active run

sudo ufw allow 8080

Access Control

Both web server user (www-data) and jenkins user will have to have access to the project files. Let’s add jenkins to www-data (or your web server user)

usermod -aG www-data jenkins

We also will need jenkins user to run sudo commands. On its own Jenkins will not be able to enter root password.

sudo visudo

Add the following line to the file:
jenkins ALL=(ALL) NOPASSWD:ALL

Jenkins Project

Create a new project by clicking on New Item

General

Fill out name and description. If you wish, you can set to discard old builds.

Source Code Management

In source code management check Git and fill in your repository. If your repository is public, you don’t have to do anything else. If you have a private repository, you can use Jenkins credential manager or authenticate Jenkins with ssh.

Build Triggers

Check Build periodically and put @daily
Check Poll SCM and put @hourly
These settings will build your project daily and check for changes in your repo every hour. If there are changes in repo, the project will get rebuilt.

Build

From Add build step drop down menu choose Execute shell. In the command input area I have the following:

composer install
npm install
npm run production
cp /var/lib/jenkins/.env .
php artisan migrate --force
./vendor/bin/phpunit
sudo chown www-data:www-data -R .
sudo chmod 770 -R .

In this window put commands that are applicable to your Laravel project. For example if you do not wish to run any tests, you don’t need to put line that runs phpunit. Then you can also change composer install to composer install –no-dev. You can also cache your routes and config files. What you do here to deploy your project is up to you.

Running Build

After saving your changes, you can run your build by clicking Build Now
Happy Coding!