2021-11-02 16:29:52 -07:00
|
|
|
---
|
2021-11-02 18:47:13 -07:00
|
|
|
- name: Deploy impress-2020 from the current local version
|
2021-11-02 16:29:52 -07:00
|
|
|
hosts: webserver
|
2021-11-02 18:47:13 -07:00
|
|
|
vars:
|
|
|
|
skip_build: no # Can override this by running `yarn deploy-skip-build`
|
|
|
|
local_app_root: "{{ playbook_dir }}/../.."
|
2021-11-02 21:29:14 -07:00
|
|
|
remote_versions_root: "/srv/impress-2020/versions"
|
2021-11-02 16:29:52 -07:00
|
|
|
tasks:
|
2021-11-02 18:47:13 -07:00
|
|
|
# For our deployment, our server resources are more constrained than our
|
|
|
|
# dev machine, and builds are an expensive uncommon RAM & CPU spike. Let's
|
|
|
|
# use our local machine for it! (I found that, running remotely, 2GB RAM
|
|
|
|
# was not enough to build impress-2020 😅)
|
|
|
|
- name: Run `yarn build` locally to build the current version
|
|
|
|
delegate_to: localhost
|
2021-11-02 16:29:52 -07:00
|
|
|
command:
|
2021-11-02 18:47:13 -07:00
|
|
|
chdir: "{{ local_app_root }}"
|
|
|
|
cmd: yarn build
|
|
|
|
when: not skip_build
|
|
|
|
|
2021-11-02 19:02:36 -07:00
|
|
|
- name: Generate a version name from the current timestamp
|
2021-11-02 21:14:47 -07:00
|
|
|
command: date '+%Y-%m-%d-%s'
|
2021-11-02 18:47:13 -07:00
|
|
|
register: new_app_version
|
|
|
|
|
2021-11-02 19:02:36 -07:00
|
|
|
- name: Print out the new version name
|
|
|
|
debug:
|
|
|
|
msg: "Deploying new version: {{ new_app_version.stdout }}"
|
|
|
|
|
2021-11-02 18:47:13 -07:00
|
|
|
- name: Save new remote folder path to a variable
|
|
|
|
set_fact:
|
2021-11-02 21:29:14 -07:00
|
|
|
remote_app_root: "{{ remote_versions_root }}/{{ new_app_version.stdout }}"
|
2021-11-02 18:47:13 -07:00
|
|
|
|
|
|
|
- name: Create new remote folder for the new version
|
|
|
|
file:
|
|
|
|
path: "{{ remote_app_root }}"
|
|
|
|
state: directory
|
|
|
|
|
|
|
|
- name: Copy local app's source files to new remote folder
|
|
|
|
ansible.posix.synchronize:
|
2021-11-02 23:02:22 -07:00
|
|
|
src: "{{ local_app_root }}/"
|
2021-11-02 18:47:13 -07:00
|
|
|
dest: "{{ remote_app_root }}"
|
|
|
|
rsync_opts:
|
|
|
|
- "--exclude=.git"
|
|
|
|
- "--filter=':- .gitignore'"
|
|
|
|
|
2021-11-02 23:02:22 -07:00
|
|
|
- name: Copy local app's .next build files to new remote folder
|
2021-11-02 18:47:13 -07:00
|
|
|
ansible.posix.synchronize:
|
2021-11-02 23:02:22 -07:00
|
|
|
src: "{{ local_app_root }}/.next/"
|
|
|
|
dest: "{{ remote_app_root }}/.next"
|
2021-11-02 18:47:13 -07:00
|
|
|
|
|
|
|
- name: Copy local app's .env secrets file to new remote folder
|
2021-11-02 23:02:22 -07:00
|
|
|
ansible.posix.synchronize:
|
2021-11-02 18:47:13 -07:00
|
|
|
src: "{{ local_app_root }}/.env"
|
2021-11-02 23:02:22 -07:00
|
|
|
dest: "{{ remote_app_root }}/.env"
|
2021-11-02 18:47:13 -07:00
|
|
|
|
2022-08-16 11:38:20 -07:00
|
|
|
- name: Check whether we can reuse the currently deployed node_modules.
|
2021-11-02 23:56:50 -07:00
|
|
|
# First, compare the files. Then, ensure that node_modules is in a good
|
2022-08-16 11:38:20 -07:00
|
|
|
# state and will be linkable. Finally, ensure that it's one of the 5 most
|
|
|
|
# recent versions, so it won't be cleaned up. If this succeeds, we can do
|
|
|
|
# the link cheat! If this fails, proceed and run `yarn install` instead.
|
2021-11-03 16:33:47 -07:00
|
|
|
command: |
|
2021-11-02 23:56:50 -07:00
|
|
|
bash -c "
|
|
|
|
cmp '{{ remote_app_root }}/yarn.lock' '/srv/impress-2020/current/yarn.lock' &&
|
2022-08-16 11:38:20 -07:00
|
|
|
test -e /srv/impress-2020/current/node_modules &&
|
|
|
|
(ls -dt /srv/impress-2020/versions/* | head -n 5 | grep $(realpath /srv/impress-2020/current/node_modules/..))"
|
2021-11-02 23:56:50 -07:00
|
|
|
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 🤞
|
2021-11-03 16:33:47 -07:00
|
|
|
command: |
|
2021-11-03 17:34:07 -07:00
|
|
|
bash -c "ln -s $(realpath /srv/impress-2020/current/node_modules) {{ remote_app_root }}/node_modules"
|
2021-11-02 23:56:50 -07:00
|
|
|
when: "can_reuse_node_modules_when_successful.rc == 0"
|
|
|
|
|
|
|
|
- name: Run `yarn install` to install dependencies in new remote folder
|
2021-11-02 16:45:07 -07:00
|
|
|
command:
|
2021-11-02 18:47:13 -07:00
|
|
|
chdir: "{{ remote_app_root }}"
|
|
|
|
cmd: yarn install
|
2021-11-02 23:56:50 -07:00
|
|
|
when: "can_reuse_node_modules_when_successful.rc > 0"
|
2021-11-02 19:02:36 -07:00
|
|
|
|
2021-11-02 21:36:42 -07:00
|
|
|
- name: Update the `current` folder to point to the new version
|
|
|
|
file:
|
|
|
|
src: "{{ remote_app_root }}"
|
|
|
|
dest: /srv/impress-2020/current
|
|
|
|
state: link
|
|
|
|
|
2021-11-02 22:49:45 -07:00
|
|
|
- name: Reload the app in pm2
|
|
|
|
command: pm2 reload impress-2020
|
|
|
|
|
2021-11-02 19:02:36 -07:00
|
|
|
- name: Find older versions to clean up
|
2022-08-16 11:38:20 -07:00
|
|
|
# Print out all but the 5 last-recently-updated versions.
|
|
|
|
# NOTE: If we change this count, change the count we use when checking
|
|
|
|
# whether to reuse node_modules too!
|
|
|
|
# TODO: This *will* still break *older* versions that pointed to an old
|
|
|
|
# `node_modules` that got cleaned up, which could make rollback a
|
|
|
|
# pain. We don't roll back often for DTI so that's probably fine,
|
|
|
|
# but it'd be better to do something a bit more robust - or just
|
|
|
|
# drop it and accept the install perf hit every time. (It's only
|
|
|
|
# like 40 seconds on this last test, which isn't as bad as I
|
|
|
|
# remember, especially when deploys aren't super common.)
|
2021-11-02 19:02:36 -07:00
|
|
|
command:
|
2021-11-02 21:29:14 -07:00
|
|
|
chdir: "{{ remote_versions_root }}"
|
2021-11-02 21:16:46 -07:00
|
|
|
cmd: bash -c 'ls -t | tail -n +6'
|
2021-11-02 19:02:36 -07:00
|
|
|
register: versions_to_clean_up
|
|
|
|
|
|
|
|
- name: Clean up older versions
|
|
|
|
file:
|
2021-11-02 21:29:14 -07:00
|
|
|
path: "{{ remote_versions_root }}/{{ item }}"
|
2021-11-02 19:02:36 -07:00
|
|
|
state: absent
|
|
|
|
with_items: "{{ versions_to_clean_up.stdout_lines }}"
|