--- - 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-%dT%H:%M:%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 last 5 versions command: chdir: /srv/impress-2020/versions cmd: bash -c 'ls | head -n -5' 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 }}"