Comment on page

Create a deployment script

The following deployment script performs the following steps
  1. 1.
    Creates a new deployment folder
  2. 2.
    Clones the repository into this deployment folder
  3. 3.
    Copies the .env file from the master folder
  4. 4.
    Runs Composer Install so that the project has the same dependencies as your committed version (see note below regarding Composer version)
  5. 5.
    Creates links for storage to the master storage folder and from storage/app/public to the public/storage folder
  6. 6.
    Finally creates a symlink from Live to the deployment folder
build.sh
1
#!/bin/sh
2
3
UNIX_TIME=$(date +%s)
4
DEPLOYMENT_DIRECTORY=$UNIX_TIME
5
6
PROJECT=$(pwd)
7
8
mkdir -p $DEPLOYMENT_DIRECTORY
9
git clone --depth 1 -b main [email protected]:novateltd/example.org.git $DEPLOYMENT_DIRECTORY
10
11
cd $DEPLOYMENT_DIRECTORY
12
13
cp ../master/.env .
14
15
php81 /usr/local/bin/composer.phar install --no-dev
16
17
cd ..
18
19
#link to the latest deployment from Live
20
ln -v -s -n -f -T $DEPLOYMENT_DIRECTORY Live
21
22
# create a link from deployment storage to master/storage
23
rm -rf $DEPLOYMENT_DIRECTORY/storage
24
ln -v -s -n -f -T $PROJECT/master/storage $DEPLOYMENT_DIRECTORY/storage
25
26
27
#echo link public to storage
28
ln -v -s -n -f -T $PROJECT/master/storage/app/public $DEPLOYMENT_DIRECTORY/public/storage
29
30
cd Live
31
32
php81 artisan cache:clear
33
php81 artisan route:cache
34
php81 artisan config:cache
35
36
echo Deployed. Migrations required?
37
echo
Copy the above code and create the file in your websites folder using nano or the SiteGround file manager.

Configuring the script for your repo

On line 9, you will need to replace this with the git clone equivalent for your repository and branch.
The parameter --depth 1 ensures that we don't waste time cloning git history. We only need the latest version of the code.
The parameter -b main is the git branch that you would like to deploy.
The project name is the SSH version of the clone command for your specific repo.

php version for Artisan

You can see that this is a PHP8.1 project and all artisan commands must specify the php version to be used. You may create a bashrc alias for this to revert it to just php

Make your script executable

When you create a file, it will not have the execute flag set. You cannot change this from the Site Tools file manager, you must do it from the command line with;
chmod +x build.sh
You also, cannot run a bash script with just its name, it must be preceded with ./
So to run the build script, run ./build.sh

migrating your database

After running the build script for the first time, you will want to migrate your database so that it is ready for action!
All artisan commands should be run from the Live folder
cd Live
php81 artisan migrate

Composer Version issues

If you are following this guide and NOT using SiteGround, you may run into an issue where the version of composer available to you is not up to date enough for a Laravel installation. If this is the case, follow the advice at https://stackoverflow.com/questions/20894518/how-do-i-install-composer-on-a-shared-hosting/29436655#29436655