Skip to content
On this page

Deployments

Applications that are being deployed on a single server are 'good practice' to reduce costs.

However, this comes with some "issues". Mainly versioning of installer packages.

Basic deployment script

Default, forge will give you a starters deployment script. This will deploy your application. However, due to the nature of our projects, we want some more secret sauce to run.

bash
cd /home/forge/[project]
git pull origin $FORGE_SITE_BRANCH
$FORGE_COMPOSER install --no-interaction --prefer-dist --optimize-autoloader

( flock -w 10 9 || exit 1
    echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock

if [ -f artisan ]; then
    $FORGE_PHP artisan migrate --force
fi

Specific NodeJS version deployment

A lot of times, Front-end developers will use a specific NodeJS version for an application. It's bad practice to just run npm install && npm run build on whatever version the server has. Which is why, we can use NVM to set the correct version. All there needs to be done is some server-side prepping and the use of the deployment script below.

Server side prep steps:

  1. Install NVM: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash (make sure you use the latest tag)
  2. Create an .nvmrc file in the project
  3. Use the deploy script below 🤩🤩🤩
bash
cd $FORGE_SITE_PATH

git pull origin $FORGE_SITE_BRANCH

$FORGE_COMPOSER install --no-interaction --prefer-dist --optimize-autoloader --no-dev

if [[ -f .nvmrc ]]; then

    . ~/.nvm/nvm.sh
    # get desired and current version
    rc_v=$(cat .nvmrc)

    echo "Changing nodejs version to the .nvmrc version"
    
    nvm use $rc_v --silent || nvm install $(cat .nvmrc)

    # compile the assets
    npm ci
    npm run build

    # revert to the old version
    echo "Reverting to nvm default version"
    nvm use default
fi

if [ -f artisan ]; then
    $FORGE_PHP artisan co:ca
    $FORGE_PHP artisan view:cache
    $FORGE_PHP artisan route:cache
    $FORGE_PHP artisan event:cache

    $FORGE_PHP artisan migrate --force
fi

( flock -w 10 9 || exit 1
    echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock

To be clear: This script needs an .nvmrc file, containing the version of nodejs. If there is no .nvmrc file present, no assets will be build (except if you take the npm ci and npm run build and place those outide of the if statement.)

if the nodejs version is not present, it will be installed the first deployment. So now you will build in the version you specify.

This allows us to run multiple projects on a single server and still build in the correct target nodejs version.

troubleshooting

The deployment fails and I need to do something with a command
not to worry! It's most likely that you just need to run nvm use --delete-prefix v[version] --silent once on the server.

Restart deamons

Restarting a deamon (for example a service that runs but needs to be restarted after a deploy), we can simply use this: sudo -S supervisorctl restart daemon-[deamon_id_in_forge]:* This will restart the deamon.