Easily Deploy Laravel Application Using Bitbucket Pipelines On Each Commit

Kumar Ravi
3 min readFeb 2, 2019

--

After wandering around the internet for hours, I was not able to find a simple tutorial for someone who is fairly new to Continuous deployment so I decided to implement it and write down the steps as I go.

This is not a full implementation which includes test and stuff, This is just optional of forge like deployment where you commit your code and it gets pushed on the server.

Goal:
1. Push your master branch on bitbucket

2. Run tasks on the server like:

git pull origin master

composer install

yarn

yarn run production

chmod 777 -R storage

chmod 777 -R bootstrap

And save as much time as we can since we only get 1 hour of free usage.

first, install node js in your droplet

sudo apt-get install curl python-software-properties
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
sudo apt-get install nodejs

install yarn

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn

install composer

sudo apt-get install composer

having node and yarn in your droplet is necessary for the deploy.sh file to run somehow it was not working by nvm’s node setup so make sure your install it with above commands.

install PHP and common extensions

sudo add-apt-repository -y ppa:ondrej/phpsudo apt-get updatesudo apt-get install php7.2 php7.2-cli php7.2-commonsudo apt-get install php7.2-curl php7.2-gd php7.2-json php7.2-mbstring php7.2-intl php7.2-mysql php7.2-xml php7.2-zip

also, install Git if you haven’t

first, you need to enable pipelines on your bitbucket account

second, you have to create an ssh key for your pipelines so that your pipeline can connect to your server

copy your public key and paste into your droplet authorized_keys file.

you will find this file in the ~/.ssh folder of your digital ocean droplet or any other file depending on your hosting.

then enter your server IP in known hosts and click fetch, this will update the fingerprint from your server.

now our goal here is that we have to connect to our server and run a deploy.sh file each time we have a commit on our repo.

so let's create a deploy.sh, file in the root folder of our project

#!/bin/sh

# run deploy specific commands
git pull origin master &&
composer install &&
yarn &&
yarn run production &&
chmod 777 -R storage &&
chmod 777 -R bootstrap

now for bitbucket to run pipelines on your project on each commit you need the bitbucket-pipelines.yml file in the root folder of your repository.

image: php:7.2-fpm

pipelines:
default:
- step:
script:
- apt-get update -y
- apt-get install -y ssh
- ssh root@$serverIpAddress 'cd ~/your-project/ && sh deploy.sh'

notice the $ sign before serverIpAddress you can create a variable in your pipeline setting and assign a value to that variable for better flexibility.

After this, you can make the commit on your project and it will deploy the code to your server.

This tutorial covers the tough part, of course, you can run PHPUnit and stuff by following bitbucket tutorial on laravel deployment here: https://confluence.atlassian.com/bitbucket/laravel-with-bitbucket-pipelines-913473967.html

what they don’t cover is the deployment part, rest you can find many tutorials on the web.

--

--

Kumar Ravi
Kumar Ravi

Written by Kumar Ravi

entrepreneur by heart developer by profession

No responses yet