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

jthijs's avatar
Level 11

Envoy multiple servers

Hi,

For a project at work, we are going to use a staging server next to the production server. The staging server, obviously, to test the new features and solved bugs before releasing it to production.

I am going to use GitLab CI/CD to push to my staging and production servers using Laravel Envoy

In my .gitlab-ci.yml file I have the following. During the deploy stage depending on the branch I am it will trigger a different story.

staging:
  stage: deploy
  script:
    - composer global require "laravel/envoy"
    - 'which ssh-agent || ( apt-get update -Y && apt-get install openssh-client -y)'
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - /root/.composer/vendor/bin/envoy run staging --commit="$CI_COMMIT_SHA"
  environment:
    name: deploy
    url: http://staging.example.com
  when: on_success
  only:
    - beta

production:
  stage: deploy
  script:
    - composer global require "laravel/envoy"
    - 'which ssh-agent || ( apt-get update -Y && apt-get install openssh-client -y)'
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SSH_PRIVATE_KEY_PROD")
    - mkdir -p ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - /root/.composer/vendor/bin/envoy run production --commit="$CI_COMMIT_SHA"
  environment:
    name: deploy
    url: http://www.example.com
  when: manual
  only:
    - master

The location where the files are stored are different depending on the server. One is in /var/www directory the other is in the users public_html directory in the domains homedir

So I my Envoy.blade.php file

@servers([
    'stage' => ['[email protected]'],
    'prod' => ['[email protected]']
])

@story('staging')
clone_stage_repo
@endstory

@story('production')
clone_prod_repo
@endstory

@task('clone_stage_repo', ['on' => 'stage'])
...
@endtask

@task('clone_prod_repo', ['on' => 'prod'])
...
@endtask

My issue is now that when I merge my development branch into the beta branch the staging story should start And when I merge my beta branch into my master branch the production story should start.

When on my tasks I don't add the ['on' => 'stage'] it works for 1 server

But when I add it I get the following error

Server [stage] is not defined.

When adding the clone_prod_repo story with the ['on' => 'prod'] I get the following in my pipeline

$ /root/.composer/vendor/bin/envoy run production --tag=production --commit="$CI_COMMIT_SHA"
Cleaning up project directory and file based variables
Job succeeded

My question are the following:

  1. Would it be possible to run a story only on 1 server
  2. Can you use this setup to run different stories depending in what stage you are in?
  3. Are the multiple servers used to do the same tasks on all servers?

I've been looking over the Envoy page, searching articles and forums. The all mention you can use multiple servers but as I can interpret just run the same task on all servers that are mentioned.

0 likes
0 replies

Please or to participate in this conversation.