Auto-deploy Laravel using Bitbucket Pipelines and Ubuntu Server

Manny Isles
4 min readApr 25, 2022

In this tutorial, you will learn how to set up Bitbucket Pipelines for a Laravel application and deploy them to your Ubuntu 20.04 server.

Enable Pipelines in Bitbucket

Go to Your repository Settings -> Under the pipelines section, click on Settings -> Click on the Enable Pipelines Switch to enable pipelines.

Pipelines are now enabled.

Next up, we’ll set up SSH keys for our repository.

Set up repository SSH Keys

These are the keys you’ll set up on your production or staging server to enable external logins to your server from bitbucket during the deployment steps which we will discuss later on.

To set these up, got to SSH Keys(still within the Pipelines Section in your repository settings) -> then click on Generate keys. You could use your own keys but there are a couple of sources that have had issues and ended up advice against it. Generating your own keys is the best alternative.

After that, set up Known Hosts. Enter the IP address of your Ubuntu server as the Host address then click Fetch to generate the host’s fingerprint. After the fingerprint is generated, click on the Add Host button.

Next up, we’ll add the public key from our repository to the authorized_keys file of our Ubuntu server.

Adding public key from Bitbucket Repository to Ubuntu server authorized_keys

Login to your Ubuntu server. It’s important to note that SSH login should be enabled for your server. If you haven’t enabled it, kindly follow the steps outlined here.

# Enter .ssh folder$ cd ~/.ssh# Copy key to authorized_keys file
# Open file using your favorite editor e.g. nano$ sudo nano authorized_keys# Add the copied public key from bitbucket on a new line then save(there's a 'Copy public key' button on the SSH keys page)

Permanently add private key identity to the authentication agent

To prevent entering the passphrase to your private key while pulling your bitbucket repositories to your servers, you need to persist your identity using ssh-add. By default, the identity is lost every time you log out hence the need to persist it. This is important if your key has a passphrase set up. If not, you can ignore this part.

Open up your .bashrc using the following command.

$ sudo nano ~/.bashrc

Copy the following contents to the bottom of your .bashrc file.

# SSH Permanent passphrase
SSH_ENV=$HOME/.ssh/environment# start the ssh-agent
function start_agent {
echo "Initializing new SSH agent..."
# spawn ssh-agent
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
echo succeeded
chmod 600 ${SSH_ENV}
. ${SSH_ENV} > /dev/null
/usr/bin/ssh-add
}if [ -f "${SSH_ENV}" ]; then
. ${SSH_ENV} > /dev/null
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi

This keeps the ssh-agent running even when you log out of your server and persists your private key identity. To get it going however, you’ll need to log out and login into your server. One you login, the SSH Agent will be initialized and you’ll be requested to add the passphrase to your private key.

Set up a deployment script on your server
This is the script that we will run to deploy your application. It is basically a list of the commands you use to deploy your application on your server.

Run the following commands to set up an executable deployment script.

# Create script at the root
$ cd
$ nano deploy.sh
# make executable
$ chmod +x deploy.sh
# Executing script
$ ./deploy.sh

A sample Laravel application deployment script:

#!/bin/bash
root="/var/www/example.com"
cd $root
echo -e '\e[1m\e[34mPulling code from remote..\e[0m\n'
git pull origin master
echo -e '\e[1m\e[34m\nInstalling required packages..\e[0m\n'
# Install required packages
composer install
echo -e '\e[1m\e[34m\nAPI deployed\e[0m\n'

Setting up this script is important as bitbucket will run it once it’s logged into your server.

Once you are done with that, we can now set up the bitbucket-pipelines.yml.

Setting up bitbucket-pipelines.yml file for our Laravel application

This script showcases how you can deploy changes based on different branches.

# This is a sample build configuration for PHP.
# Check our guides at https://confluence.atlassian.com/x/e8YWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: php:8.0.2
pipelines:
# default - contains the steps that will run on every push.
# default:
branches:
# You can include your custom branches and the steps you'd like to undertake e.g. testing
#staging:
master:
- step:
caches:
- composer
script:
- apt-get update && apt-get install -y unzip
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
- composer install
- step:
name: Deploy to production
deployment: production
script:
- pipe: atlassian/ssh-run:0.4.0
variables:
SSH_USER: 'your-server-username'
SERVER: 'your-server-ip-address'
COMMAND: './your-deployment-script.sh'

https://medium.com/@nicholaskimuli/continuous-deployment-cd-using-bitbucket-pipelines-and-ubuntu-server-9b685bb64bb5

--

--

Manny Isles
0 Followers

PHP Laravel Developer. I am building a new HRIS and Payroll system for small and medium enterprises.