Deploy_build_before for multiple site boxes

So, for boxes with multiple sites…
I have some sites using grunt and also with some other slight requirements for deployment, and they all have different theme names at least.

Here’s what I ended up doing…

In deploy.yml, simply add a -{{ site }} to the deploy_build_before path and create your different build_before files accordingly.

May be obvious but hopefully will help someone.

8 Likes

Good idea! Never thought about this.

Reminder:

  • Edit deploy.yml
    deploy_build_before: "{{ playbook_dir }}/deploy-hooks/{{ site }}-build-before.yml"
  • $ cp deploy-hooks/build-before.yml deploy-hooks/[domain.com]-build-before.yml
4 Likes

With latest changes to Trellis hook system,
this would be now added to the hooks list in ‘roles/deploy/defaults.yml’:

[...]
deploy_build_before:
  - "{{ playbook_dir }}/deploy-hooks/build-before.yml"
  - "{{ playbook_dir }}/deploy-hooks/sites/{{ site }}-build-before.yml" # <- this
[...]
8 Likes

I use Trellis with multi sites but I have the following structure which I personally prefer.

Instead of prefixing the hook with site I use sub-folders in deploy-hooks depending on what hook it is.

deploy_build_before:
  - "{{ playbook_dir }}/deploy-hooks/build-before/{{ site }}.yml"

deploy_build_after:
  - "{{ playbook_dir }}/deploy-hooks/build-after/{{ site }}.yml"
  # - "{{ playbook_dir }}/deploy-hooks/sites/{{ site }}-build-after.yml"

deploy_finalize_before:
  - "{{ playbook_dir }}/deploy-hooks/finalize-before/{{ site }}.yml"

deploy_finalize_after:
  - "{{ playbook_dir }}/deploy-hooks/finalize-after/{{ site }}.yml"

so when adding a new site I simply create staging.example.com or example.com depending on environment.

5 Likes

I just noticed in


that the hooks are defined in group_vars/all/deploy-hooks.yml.
Is this a better place because it interferes less with existing trellis files?

@strarsis yes, Trellis has changed since my original post here, that is the best way to go about it now.
in group_vars/all/deploy-hooks.yml

deploy_build_before:
  - "{{ playbook_dir }}/deploy-hooks/{{ site }}-build-before.yml"

…or however you prefer to structure the filenames. You could put them in site directories like:

deploy_build_before:
  - "{{ playbook_dir }}/deploy-hooks/{{ site }}/build-before.yml"

I realize another year has gone by, just posting this for anyone that might find it. There’s now instructions in the trellis docs for this: https://roots.io/trellis/docs/deploys/#custom-tasks

2 Likes

Hey, I’ve set this up and it’s successfully looking for the right build file for each site’s deploy, however I’m getting an error that “deploy_helper is undefined”. Guessing this is referring to {{ deploy_helper.new_release_path }}.

How do I pass this variable onto each build-before file?

Please always share your code/what you modified when looking for help.

Are you using the right hook name? deploy_before won’t work but deploy_build_before will work.

1 Like

Thanks that’s it. I had incorrectly copied deploy_before from the docs, but I see the correct hook is in that post above, which I should’ve copied…

Hey, I’m trying to do something similar but I’m a little unsure about how to set this up now that’s it been over two years since this post.

I have a multi-site configured with two different sage themes for two different sites. I’m trying to understand how I can setup my before build file to handle building both themes on deploys.

I’ve tried a handful of different methods found on this discourse but nothing appears to work properly. Any idea how this can be achieved now?

Hi joshb,
The instructions for this are the same since the last post. That said, you might be in uncharted territory with it being multisite. This post wasn’t about wp multisite, but multiple single installs on one server.
If you start a new post with info about what you have tried already, I can try to help.