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

 

note, you might want to run your script as sudo

 

sudo nano /etc/sudoers

jenkins ALL=(ALL) NOPASSWD: ALL

 

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.

SaveSave

Advertisements

Capturing time-lapse from mjpeg streamer or any jpg using aws lambda

Posted on Updated on

We have a raspberry pi running jpeg streamer through octoprint for checking the status of a remote 3d printer, it is also a pretty unusual print and unusually large so I thought we should capture the process for posterity.

I really did not want to spin up a server to capture the 96 hour print (1 image every 60 seconds/1 minute) so I thought why not write a was lambda function (server less!), it will be much cheaper and easier – no command line crons and server to set up and ssh into etc.

Here is how I done it:

Firstly rather than the stream action, we need to use the snapshot function (action=snapshot).

Next we create an S3 bucket to save our images I called mine “timelapse-camera-1”.

Screen Shot 2018-05-24 at 22.22.21

Following that we move onto was lambda and set up our role and function. I went into the membership menu, roles and manually attached this policy.

Screen Shot 2018-05-24 at 22.22.58

I then wrote the following code in lambda, saved and tested.

BUCKET_NAME = "timelapse-camera-1"
from __future__ import print_function

import boto3
import time
import urllib

BUCKET_NAME = "timelapse-camera-1"
TMP_FILE = "/tmp/tmp.jpg"

def lambda_handler(event, context):
camera_file = urllib.URLopener()
try:
camera_file.retrieve("http://000.000.000/webcam/?action=snapshot", TMP_FILE)

timestr = time.strftime("%Y%m%d-%H%M%S")

s3 = boto3.resource('s3')
s3.meta.client.upload_file(TMP_FILE, BUCKET_NAME, timestr + '.jpg')

return 'saved ' + timestr + '.jpg'

except Exception as e:
print(e)
raise e

Next we need to run our function every minute by calling lambda from was cloud watch.

There we go, easy 🙂 just remember to turn it off when your done.

First batch done this year

Posted on

Here are some of the first prints from this year… progress… always nice to see 🙂

 

Sending stuff to the printers :)

Posted on Updated on

I told you it was busy 🙂 Here is what I just sent to the printers – over 300 parts.

screen-shot-2017-02-19-at-17-18-01

More 3D Design on St Pancras

Posted on Updated on

Well I managed to do some work over Christmas on St Pancras but the list seems never ending! Here are some of the bridges and first pass of the old Imperial Gas Works Co Gasometers (still needs some work) – oh and of course the hotel entrance.

Prototyping St Pancras V3

Posted on Updated on

If you have been following you’ll know I am prototyping new methods of building St Pancras from the original plans and here is the first prototype of this new version. Lots still to do but this is remarkably accurate.

Language skills can get you behind the scenes!

Posted on Updated on

We had a super time at RIAT this weekend, practicing my awful Polish skills got us a behind the scenes look and talk through one of their aircraft, really fun!