Build-before hooks and including my theme via composer

edit: I changed the title of this thread to better reflect what the topic has focused on in case somebody else comes across this

I’m struggling to settle into a successful Trellis deployment workflow.

This is what works so far: a single repo containing Trellis, Bedrock, and Sage just like in the roots example project. If I set my site up like that, I can deploy to a DO droplet just fine with no issues.

This is what does not work for me, which is unfortunate because this is what I am trying to do: two separate repos. One with Trellis+Bedrock; Sage in its own repo.

I add the theme directory to the site’s .gitignore so as to not commit theme files to the Trellis+Bedrock repo. That way I can develop the theme in its repo and only have to make commits there.

Everything works fine on my dev machine, which is to be expected because all the files and directories are in place as they should be. But when it comes time to deploy with trellis, things get lost in the mix. I’ve tried to designate my theme as a submodule of my Trellis+Bedrock repo and that didn’t work (but it works fine on my dev machine). I’ve also tried adding my theme via composer and that also didn’t work (but that also works fine on my dev machine).

I would love to give some more specific details about how exactly my methods didn’t work for me, but could somebody help me grasp what the intended workflow is even supposed to be? The only idea I’m married to is having a separate repo for my theme. I just don’t know if I should be using submodules, composer, or some other method entirely. If somebody could point me in the right direction, I’ll take that route and then report back with any errors I encounter in order for more specific assistance.

Why did it not work?

From what I can recall, something along the lines of:

Warning: Could not create directory.

You must be implying composer was the route I should be focused on, so let me try it again and I’ll update with a more specific error with context.

Okay, I just did it with composer and here is where Trellis fails on deploy:

TASK [deploy : Install Composer dependencies] **********************************
System info:
  Ansible 2.2.1.0; Darwin
  Trellis at "Option to install WP-CLI packages"
---------------------------------------------------
MODULE FAILURE
Traceback (most recent call last):
  File "/tmp/ansible_RxZebC/ansible_module_command.py", line 212, in <module>
    main()
  File "/tmp/ansible_RxZebC/ansible_module_command.py", line 155, in main
    os.chdir(chdir)
OSError: [Errno 2] No such file or directory: '/srv/www/mysite.com/r
eleases/20170621003238/web/app/themes/my-theme'

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: 
OSError: [Errno 2] No such file or directory: 
'/srv/www/mysite.com/releases/20170621003238/web/app/themes/my-theme'
fatal: [mysite.com]: FAILED! => {"changed": false, "failed": true, "module_stdout": ""}
    to retry, use: --limit @/Users/Matt/sites/mysite.com/trellis/deploy.retry

PLAY RECAP *********************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=0   
mysite.com        : ok=12   changed=7    unreachable=0    failed=1

Please post your composer.json file from the Bedrock folder

Some additional info that may be useful:

Since we know it fails here
TASK [deploy : Install Composer dependencies]

I looked at the build-before.yml and commented out just that step and now it deploys successfully, but there are no styles for my theme.

When the styles are build from source (by using a task runner) you want to add a hook that builds + transfers them (build artifacts aren’t committed and built + transferred separately):

If I’m not mistaken, I’m already attempting what you’re suggesting in that link. I’m using the latest Trellis, Bedrock, and Sage 9. I’ve uncommented the build-before.yml and replaced sage with my theme name. I’m not doing a multisite so I don’t see a need to use a {{ site }} variable as discussed in that link you provided. Unless I’m misunderstanding what you are suggesting. Here is my build-before.yml for reference, though again, it should look pretty much like the default since all I did was replace the word sage:

- name: Run yarn install
  command: yarn install
  connection: local
  args:
    chdir: "{{ project_local_path }}/web/app/themes/my-theme"

- name: Install Composer dependencies
  command: composer install --no-ansi --no-dev --no-interaction --no-progress --optimize-autoloader --no-scripts
  args:
    chdir: "{{ deploy_helper.new_release_path }}/web/app/themes/my-theme"

- name: Compile assets for production
  command: yarn run build:production
  connection: local
  args:
    chdir: "{{ project_local_path }}/web/app/themes/my-theme"

- name: Copy project local files
  synchronize:
    src: "{{ project_local_path }}/web/app/themes/my-theme/dist"
    dest: "{{ deploy_helper.new_release_path }}/web/app/themes/my-theme"
    group: no
    owner: no
    rsync_opts: --chmod=Du=rwx,--chmod=Dg=rx,--chmod=Do=rx,--chmod=Fu=rw,--chmod=Fg=r,--chmod=Fo=r

It’s that second step that seems to be giving me trouble. If I ssh into my dev machine, there is no srv/www/mysite.com/releases/. There is one on the DO droplet, though. I assume that’s the way it’s supposed to be?

The key is this:

No such file or directory: ‘/srv/www/mysite.com/r
eleases/20170621003238/web/app/themes/my-theme’

Composer install runs during build-after. Your build-before is referencing your theme which is installed via Composer which hasn’t happened yet.

Normally this is fine because the theme is part of the repo.

You’d have to override deploy_build_after as shown here: https://roots.io/trellis/docs/deploys/#custom-tasks

Copy the existing file, then add what you need for building the theme.

1 Like

Thanks! This did the trick! I removed the 2 tasks from build-before that referenced {{ deploy_helper.new_release_path }} and added those 2 tasks to the end of my build-after override. No more problems!

1 Like