Getting Jenkins and Laravel to play together for continuous integration via github

Posted on Updated on

Well what is continuous integration? Google says this:

Continuous Integration (CI) is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect problems early.

So I like laravel, its an elegant PHP framework with loads of great documentation and PHP has no shortage of resource all over the world, essentially it is not a new or niche language you’ll struggle to find people for or have to pay high prices for vogue specialists.

So if we are using laravel and github it makes sense to update our production website when we commit or merge back to our master branch.

Step up Jenkins, Jenkins quotes it is this:

The leading open source automation server, Jenkins provides hundreds of plugins to support building, deploying and automating any project.

So lets start with the building and deploying part for automation, I’m using ubuntu and you need to hit the command line (ssh).

1. Installing Jenkins

ssh -i /Users/{user}/Downloads/{your pem file}.pem   ubuntu@{server_ip}

sudo apt-get install default-jre

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

sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'

sudo apt-get update

sudo apt-get install jenkins

/usr/bin/java -Djava.awt.headless=true -jar /usr/share/jenkins/jenkins.war --webroot=/var/cache/jenkins/war --httpPort=8080 --ajp13Port=-1

You should now be able to access your Jenkins install on port 8080 i.e. http://000.000.000.000:8080

If you have problems check the log

/var/log/jenkins/jenkins.log

2. Installing Apache for laravel

sudo apt-get install apache2

sudo apt-add-repository ppa:ondrej/php

sudo apt-get update

sudo apt install php7.1 php7.1-xml php7.1-mbstring php7.1-mysql php7.1-json php7.1-curl php7.1-cli php7.1-common php7.1-mcrypt php7.1-gd libapache2-mod-php7.1 php7.1-zip

3. Don’t forget composer

sudo apt install composer

4. Make an ssh key for github 

ssh-keygen -t rsa -b 4096 -C "{your email}"

eval "$(ssh-agent -s)"

ssh-add /home/ubuntu/.ssh/id_rsa

Add it to GitHub via your projects keys.

5. Tell apache where your project is

sudo nano /etc/apache2/sites-available/000-default.conf

DocumentRoot /var/www/{project name}/public

Options Indexes FollowSymLinks

AllowOverride All

Require all granted

sudo systemctl restart apache2

5. Turn on mod re-write for your laravel routes

sudo a2enmod rewrite && sudo service apache2 restart

6. Clone the github project

cd /var/www/

sudo git clone https://github.com/{user}/{project name}.git

7. Don’t forget to crate a .env file!

sudo cp  /home/ubuntu/.env /var/www/hapori_2/.env

8. Check your laravel install it should be working now 🙂 debug it if it isn’t!

9. Configuring Jenkins with github – install the plugin

Screen Shot 2018-05-28 at 11.15.54

10. Add a project and then connect to your github project

Screen Shot 2018-05-28 at 11.17.09.png

11. Now configure your build automation

Screen Shot 2018-05-28 at 11.17.09

Here is the code I used, you should add unit tests in here somewhere and perhaps not freshly migrate and seed every deployment unless your prototyping.

sudo rm -rf /home/ubuntu/{project_name}/
sudo -u ubuntu git clone git@github.com:{user}/{project_name}.git /home/ubuntu/{project_name}/

sudo rm -rf /var/www/{project_name}/

sudo mv /home/ubuntu/{project_name}/ /var/www/{project_name}/
sudo cp /home/ubuntu/.env /var/www/{project_name}/.env

sudo chown -R www-data:www-data /var/www/{project_name}/
sudo chmod 755 -R /var/www/{project_name}/storage

cd /var/www/{project_name}/

sudo composer install
sudo php artisan migrate:fresh --seed

12. That is it!

You should now be able to do a commit on your master branch in github and see an auto update to your server. Look at the build info and console log to debug.

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s