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
|
|
|
|
2021-11-02 23:56:50 -07:00
|
|
|
- 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.
|
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' &&
|
|
|
|
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 🤞
|
2021-11-03 16:33:47 -07:00
|
|
|
command: |
|
2021-11-02 23:56:50 -07:00
|
|
|
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
|
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
|
2021-11-02 21:16:46 -07:00
|
|
|
# Print out all but the 5 last-recently-updated versions
|
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 }}"
|