Deployment with the new Trellis hooks

Hi there,

I’m in a little bit of trouble so I would need your help with the following thing.
I’m using trellis to deploy my sites to staging and production environments. I set up my workflow based on your example-project.com git repo.

I just run into something which is because the new trellis hooks. There is two hooks you have in the example-ptoject.com repo:

  • project_pre_build_commands_local
  • project_local_files

I tried to change these based on your notes but I couldn’t win this battle. Im not a big yml/ansible ninja so please apologize.

I tried something like this:

Created a build-before.yml file
Added this file to the deploy.yml file deploy_build_before: "{{ playbook_dir }}/roles/deploy/hooks/build-before.yml"
And tried to figure out how should it look like based on your example codes, so i created this:

---
- name: Run npm install
  command: npm install
  args:
    chdir: "{{ deploy_helper.current_path }}/web/app/themes/footprint"
- name: Run bower install
  command: bower install
  args:
    chdir: "{{ deploy_helper.current_path }}/web/app/themes/footprint"
- name: Run gulp
  command: gulp --production
  args:
    chdir: "{{ deploy_helper.current_path }}/web/app/themes/footprint"

This is not working, because the path is wrong.

Could you pls help me how should it look like with the new hooks? Also what about with the project_local_files hook?

Or maybe it would be good to update the example-project.com repo with the new codes.

Thank you!

1 Like

Thanks for pointing out the outdated example project docs. Would you mind opening an issue on that repo as a reminder?

Couple points:

  • Per the docs you should keep your custom hooks outside of the roles folder. Example: deploy_build_before: "{{ playbook_dir }}/deploy-hooks/build-before.yml"

  • If you were using the local commands before, you’ll need to add some additional things to the hook file to make them run locally. And you still want to use project.local_path.

---
- name: Run npm install
  command: npm install
  connection: local
  args:
    chdir: "{{ project.local_path }}/web/app/themes/footprint"
- name: Run bower install
  command: bower install
  connection: local
  args:
    chdir: "{{ project.local_path }}/web/app/themes/footprint"
- name: Run gulp
  command: gulp --production
  connection: local
  args:
    chdir: "{{ project.local_path }}/web/app/themes/footprint"
- name: Copy project local files
  synchronize: src="{{ item.src }}"
               dest="{{ deploy_helper.new_release_path }}/{{ item.dest }}"
               group=no
               owner=no
               rsync_opts=--chmod=Du=rwx,--chmod=Dg=rx,--chmod=Do=rx,--chmod=Fu=rw,--chmod=Fg=r,--chmod=Fo=r
  with_items:
  - src: "{{ project.local_path }}/web/app/themes/footprint/dist"
    dest: web/app/themes/sage
1 Like

Thank you Scott, now its all clear!

@bokorir thanks for reporting the issue. Could you keep us updated in this thread if the tasks I gave above work? And any changes you might need to do. Then I can use that to update the example project.

Works without changes

Sorry for late answer. Works without changes. Thanks!

FYI this was changed up a bit here:

1 Like

I have this working perfectly, but don’t understand how it works. In the trellis docs it says it clones from git repo, is that correct? Or is it using rsync to sync local to staging/prod? It seems like what it’s doing is this: running come commands locally before syncing local with remote. Is the git remote involved in the deploy? Why is the build-before.yml commented out by default? Are there different ways to deploy with trellis. I’m a little confused as you can see.

Thanks again for this stuff. It’s super awesome!!!

The dist folder in Sage by default is git ignored. So that has to be built and then rsynced

Your codebase is deployed via a Git clone but the assets are built locally and rsynced. So it’s a combination of both if you’re using Sage.

Thanks. I see now. Curious how come the build-before.yml is commented out by default. It seems like that would be a mandatory part of deploying, or are you saying it’s just a template for a starting point?

I’m assuming you’re talking about Trellis itself. It’s commented out because Trellis isn’t Roots/Sage specific. It’s enabled in our https://github.com/roots/roots-example-project.com though.

1 Like

This is what confuses me. I wanted to trigger a Wordpress DB update for a domain specifically wp search-replace example.com example.dev. I can see that there is an existing hook in {{ playbook_dir }}/roles/deploy/hooks/finalize-after.yml as per statement above I’m thinking to create another hook file under {{ playbook_dir }}/deploy-hooks/finalize-after.yml but this will override the default finalize-after.yml. So what is the correct approach? Create a new file or just modify the existing one?

Also where do I learn about the hooks and how to use each pattern? I can see that there seem to be a certain logic used in the yaml file but can’t seem to understand it fully. I’m still beginner at Trellis workflow by the way.

You should probably create a new one starting from the existing one.

edit: https://roots.io/trellis/docs/deploys/#hooks

Thanks! I knew about that already. But the challenge is I notice that there is some kind of language/operation involved in the yaml hook file.

For example:

- name: Warn about updating network database.
  debug:
    msg: "Updating the network database could take a long time with a large number of sites."
  when: wp_installed | success and project.multisite.enabled | default(false)

As you can see in the when part there is a checking regarding wp_installed. I’m guessing this is to check when WP is installed then run this block. But the succeeding pipes doesn’t seem to make sense to me especially the default(false) part. Where do I learn more about this?

Trellis just Ansible so all of this can be learned from the Ansible docs. The “piping” is Jinja filters: http://docs.ansible.com/ansible/playbooks_filters.html

1 Like