impress-2020/deploy/playbooks/deploy.yml
Matchu c13114b0cc Fix old version cleanup to sort by modified date
I was sorting by version name, which _was_ reliable until I changed my version name format, and then a deploy cleaned up the version it had just deployed because it no longer sorted to the end!

Rather than be dependent on a stable version name format, and set myself up for more surprises, I'm going to instead use the folder's modified time. While there could be surprising ways for folder timestamps to be updated, I expect it to be _more_ reliable overall.
2021-11-02 21:16:46 -07:00

70 lines
2.4 KiB
YAML

---
- name: Deploy impress-2020 from the current local version
hosts: webserver
vars:
skip_build: no # Can override this by running `yarn deploy-skip-build`
local_app_root: "{{ playbook_dir }}/../.."
tasks:
# 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
command:
chdir: "{{ local_app_root }}"
cmd: yarn build
when: not skip_build
- name: Generate a version name from the current timestamp
command: date '+%Y-%m-%d-%s'
register: new_app_version
- name: Print out the new version name
debug:
msg: "Deploying new version: {{ new_app_version.stdout }}"
- name: Save new remote folder path to a variable
set_fact:
remote_app_root: "/srv/impress-2020/versions/{{ new_app_version.stdout }}"
- 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:
src: "{{ local_app_root }}"
dest: "{{ remote_app_root }}"
rsync_opts:
- "--exclude=.git"
- "--filter=':- .gitignore'"
- name: Copy local app's build files to new remote folder
ansible.posix.synchronize:
src: "{{ local_app_root }}/build"
dest: "{{ remote_app_root }}/build"
- name: Copy local app's .env secrets file to new remote folder
copy:
src: "{{ local_app_root }}/.env"
dest: "{{ remote_app_root }}"
- name: Run `yarn install` to install dependencies remotely
command:
chdir: "{{ remote_app_root }}"
cmd: yarn install
- name: Find older versions to clean up
# Print out all but the 5 last-recently-updated versions
command:
chdir: /srv/impress-2020/versions
cmd: bash -c 'ls -t | tail -n +6'
register: versions_to_clean_up
- name: Clean up older versions
file:
path: "/srv/impress-2020/versions/{{ item }}"
state: absent
with_items: "{{ versions_to_clean_up.stdout_lines }}"