Refactor deploy to build locally, not remotely

This commit is contained in:
Emi Matchu 2021-11-02 18:47:13 -07:00
parent d17c4cea8b
commit edd983c97a
3 changed files with 56 additions and 18 deletions

View file

@ -1,23 +1,53 @@
--- ---
- name: Deploy impress-2020 from `main` on GitHub - name: Deploy impress-2020 from the current local version
hosts: webserver hosts: webserver
vars:
skip_build: no # Can override this by running `yarn deploy-skip-build`
local_app_root: "{{ playbook_dir }}/../.."
tasks: tasks:
- name: Pull impress-2020 from `main` on GitHub # For our deployment, our server resources are more constrained than our
git: # dev machine, and builds are an expensive uncommon RAM & CPU spike. Let's
repo: https://github.com/matchu/impress-2020.git # use our local machine for it! (I found that, running remotely, 2GB RAM
dest: /srv/impress-2020/latest-version # was not enough to build impress-2020 😅)
version: main - name: Run `yarn build` locally to build the current version
- name: Install dependencies with yarn delegate_to: localhost
command: command:
chdir: /srv/impress-2020/latest-version chdir: "{{ local_app_root }}"
cmd: yarn install
register: result
changed_when: '"Already up-to-date." not in result.stdout'
- name: Copy our local .env secrets file to the server
copy:
src: ../../.env
dest: /srv/impress-2020/latest-version
- name: Run `yarn build` to build the production app
command:
chdir: /srv/impress-2020/latest-version
cmd: yarn build cmd: yarn build
when: not skip_build
- name: Generate a version number from the current timestamp
command: date '+%Y-%m-%dT%H:%M:%S'
register: new_app_version
- 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

View file

@ -6,12 +6,14 @@
become: yes become: yes
group: group:
name: web name: web
- name: Add current user to web group - name: Add current user to web group
become: yes become: yes
user: user:
name: "{{ ansible_user_id }}" name: "{{ ansible_user_id }}"
group: web group: web
append: yes append: yes
- name: Create the app folder - name: Create the app folder
become: yes become: yes
file: file:
@ -21,26 +23,31 @@
# may only read it. # may only read it.
group: web group: web
mode: "u=rwx,g=rwx,o=rx" mode: "u=rwx,g=rwx,o=rx"
- name: Add Nodesource apt key - name: Add Nodesource apt key
become: yes become: yes
apt_key: apt_key:
id: 9FD3B784BC1C6FC31A8A0A1C1655A0AB68576280 id: 9FD3B784BC1C6FC31A8A0A1C1655A0AB68576280
url: https://deb.nodesource.com/gpgkey/nodesource.gpg.key url: https://deb.nodesource.com/gpgkey/nodesource.gpg.key
- name: Add Node v16 apt repository - name: Add Node v16 apt repository
become: yes become: yes
apt_repository: apt_repository:
repo: deb https://deb.nodesource.com/node_16.x focal main repo: deb https://deb.nodesource.com/node_16.x focal main
- name: Install Node v16 - name: Install Node v16
become: yes become: yes
apt: apt:
update_cache: yes update_cache: yes
name: nodejs name: nodejs
state: present state: present
- name: Install Yarn - name: Install Yarn
become: yes become: yes
npm: npm:
name: yarn name: yarn
global: yes global: yes
- name: Install node-canvas dependencies - name: Install node-canvas dependencies
become: yes become: yes
apt: apt:

View file

@ -68,6 +68,7 @@
"lint": "next lint --dir src --dir pages", "lint": "next lint --dir src --dir pages",
"deploy-setup": "echo $'Setup requires you to become the root user. You\\'ll need to enter the password for your account on the remote web server below, and you must be part of the `sudoers` user group.' && ansible-playbook -K -i deploy/inventory.cfg deploy/playbooks/setup.yml", "deploy-setup": "echo $'Setup requires you to become the root user. You\\'ll need to enter the password for your account on the remote web server below, and you must be part of the `sudoers` user group.' && ansible-playbook -K -i deploy/inventory.cfg deploy/playbooks/setup.yml",
"deploy": "ansible-playbook -i deploy/inventory.cfg deploy/playbooks/deploy.yml", "deploy": "ansible-playbook -i deploy/inventory.cfg deploy/playbooks/deploy.yml",
"deploy-skip-build": "ansible-playbook -i deploy/inventory.cfg deploy/playbooks/deploy.yml --extra-vars='{\"skip_build\": true}'",
"cypress": "cypress open", "cypress": "cypress open",
"mysql": "mysql --host=impress.openneo.net --user=$(dotenv -p IMPRESS_MYSQL_USER) --password=$(dotenv -p IMPRESS_MYSQL_PASSWORD) --database=openneo_impress", "mysql": "mysql --host=impress.openneo.net --user=$(dotenv -p IMPRESS_MYSQL_USER) --password=$(dotenv -p IMPRESS_MYSQL_PASSWORD) --database=openneo_impress",
"mysql-dev": "mysql --host=localhost --user=impress_2020_dev --password=impress_2020_dev --database=impress_2020_dev", "mysql-dev": "mysql --host=localhost --user=impress_2020_dev --password=impress_2020_dev --database=impress_2020_dev",