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

Zuruck's avatar

Change Forge Deploy Branch dynamically

I am wondering if it's possible to create dynamic staging enviroments with Laravel Forge. I want to be able to issue a command on a Github Pull Request and trigger a deploy on forge using the head branch of the PR. Something like azure static web apps, but for backend applications.

I'm already done with the Github Actions part and was able to pass the branch name via forge deployment trigger URL.

This is my current deployment script, but it seems that I'm unable to checkout and pull to a branch different to the one configured on Forge settings.

cd ...
git fetch
git checkout $FORGE_VAR_BRANCH
git pull origin $FORGE_VAR_BRANCH

$FORGE_COMPOSER install --no-dev --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

Any ideas if this is even possible currently on forge?

0 likes
5 replies
gych's avatar

Try this

git pull origin {{ $FORGE_VAR_BRANCH }}
Zuruck's avatar

@gych Sorry, I've already tried that before, updated the post with the correct script

gych's avatar

@Zuruck No problem, does it give you an error and is the error related to the git pull origin?

Zuruck's avatar

@gych Yes, it gives me cannot checkout branch - error: pathspec '<my-branch>' did not match any file(s) known to git. I've tried some other approaches aswell like reset --hard origin/<my-branch> and it gave me similar errors

gych's avatar

@Zuruck

Try to add this echo to the deploy script under cd ..., so we can confirm that the correct branch name is used

echo "Branch: $FORGE_VAR_BRANCH"

Please or to participate in this conversation.