From 4f372af132bce9dee118542c0c9c63277cf8dec8 Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 2 Nov 2021 23:56:50 -0700 Subject: [PATCH] Optimization: Reuse node_modules when possible `yarn install` is slow, I'd like to skip it! Vercel and other hosts do a similar cheat here. --- deploy/playbooks/deploy.yml | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/deploy/playbooks/deploy.yml b/deploy/playbooks/deploy.yml index 4fd2663..0edd1e6 100644 --- a/deploy/playbooks/deploy.yml +++ b/deploy/playbooks/deploy.yml @@ -52,10 +52,34 @@ src: "{{ local_app_root }}/.env" dest: "{{ remote_app_root }}/.env" - - name: Run `yarn install` to install dependencies remotely + - name: Check whether yarn.lock is the same as the current deployed version + # First, compare the files. Then, ensure that node_modules is in a good + # state and will be linkable. If this succeeds, we can do the link cheat! + # If this fails, keep going, and run `yarn install` instead. + command: > + bash -c " + cmp '{{ remote_app_root }}/yarn.lock' '/srv/impress-2020/current/yarn.lock' && + realpath /srv/impress-2020/current/node_modules" + failed_when: no + register: can_reuse_node_modules_when_successful + + - name: Link node_modules to current deployed version, if they match + # Linking is a pretty wild cheat to make this much faster than copying! + # We're counting on the assumptions that 1) versions' dependencies don't + # change after the fact, and 2) the app doesn't mutate node_modules while + # running. So, these two copies of node_modules *should* just stay + # permanently the way they are forever, so linking shoouulld be safe 🤞 + command: > + bash -c " + ln -s $(realpath /srv/impress-2020/current/node_modules) + {{ remote_app_root }}/node_modules" + when: "can_reuse_node_modules_when_successful.rc == 0" + + - name: Run `yarn install` to install dependencies in new remote folder command: chdir: "{{ remote_app_root }}" cmd: yarn install + when: "can_reuse_node_modules_when_successful.rc > 0" - name: Update the `current` folder to point to the new version file: