Auto-deploy Laravel using Bitbucket Pipelines and Ubuntu Server

# 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)
$ sudo nano ~/.bashrc
# 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
# Create script at the root
$ cd
$ nano deploy.sh
# make executable
$ chmod +x deploy.sh
# Executing script
$ ./deploy.sh
#!/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'
# 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'

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store