Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

secondman's avatar

Envoyer Type Deployment on Forge

Despite my best efforts I cannot get my client to spring for Envoyer, so I'm setting out to build a similar workflow just using the Forge deployment script.

This seemed like an easy task, but apparently I don't have the needed git commands to make this work.

My initial attempt looks like so:

# make a deploy dir
mkdir /home/forge/deploying

# copy existing .git directory to deploy
cp -r /home/forge/example.com/.git /home/forge/deploying/.git

# cd into deploying
cd /home/forge/deploying

# fetch and pull
git fetch --all
git pull origin develop

# typical forge commands
$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

php artisan migrate --force
npm install
npm run dev
php artisan config:clear
php artisan cache:clear
php artisan queue:restart

# since npm has compiled theres no need to keep all these files
rm -rf node_modules

# delete the old site
rm -rf /home/forge/example.com

# change the name of the deploying dir
mv /home/forge/deploying /home/forge/example.com

I know that Envoyer is more elegant with this, providing several backups and other features and I may try that once I get something basic to work.

Right now it dies at composer install because it's not pulling in all the files, only any changed files.

Any help?

0 likes
1 reply
secondman's avatar
secondman
OP
Best Answer
Level 17

Ok here we go.

This works really well on all the sites I implemented it on.

Hopefully it will help you out if you're in a situation where you can't use Envoyer for some reason.

# delete our old deploy directory if it exists
if [ -d "/home/forge/deploy" ] 
then 
	rm -rf /home/forge/deploy
fi

# create a deploy directory
mkdir /home/forge/deploy

# move into the directory
cd /home/forge/deploy

# clone your repository branch 
git clone -b $FORGE_SITE_BRANCH [email protected]:Example/example.git .

# copy needed files that aren't committed to git
cp /home/forge/example.com/.env /home/forge/deploy/.env
cp -r /home/forge/example.com/storage /home/forge/deploy

# install dependencies with composer
$FORGE_COMPOSER install --no-interaction --prefer-dist --optimize-autoloader

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

# run artisan and node commands
php artisan migrate --force
npm install
npm run production
php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan queue:restart

# now that scripts are compiled remove the node directory
rm -rf node_modules

# delete our old backup directory
if [ -d "/home/forge/backup" ] 
then 
	rm -rf /home/forge/backup
fi

# backup the current build
mv /home/forge/example /home/forge/backup

# deploy the new build
mv /home/forge/deploy /home/forge/example.com

# move into new build directory
cd /home/forge/example.com

# link your storage directory
php artisan storage:link

Cheers.

1 like

Please or to participate in this conversation.