--- - name: Rollback impress to a previous version hosts: webserver become: yes become_user: impress vars: remote_project_root: "/srv/impress" ruby_version: "3.3.7" tasks: - name: Read the second-to-most-recent version command: chdir: "{{ remote_project_root }}/versions" cmd: bash -c 'ls | tail -n 2 | head -n 1' register: new_app_version - name: Print out the new version name debug: msg: "Rolling back to version: {{ new_app_version.stdout }}" - name: Save new remote folder path to a variable set_fact: remote_app_root: "{{ remote_project_root }}/versions/{{ new_app_version.stdout }}" - name: Configure Bundler to run in deployment mode command: chdir: "{{ remote_app_root }}" cmd: /opt/ruby-{{ ruby_version }}/bin/bundle config set --local deployment true # This ensures that, while attempting our current deploy, we don't # accidentally delete gems out from under the currently-running version. # NOTE: From reading the docs, I thiink this is the default behavior, but # I can't be sure? Rather than deep-dive to find out, I'd rather just set # it, to be clear about the default(?) behavior we're depending on. - name: Configure Bundler to *not* clean up old gems when installing command: chdir: "{{ remote_app_root }}" cmd: /opt/ruby-{{ ruby_version }}/bin/bundle config set --local clean false # NOTE: Bundler recommends this, and they're pretty smart about it: if the # Gemfile changes, this shouldn't disrupt the currently-running version, # because we won't clean up its now-unused gems yet, and if we upgrade a # gem it'll install *both* versions of the gem until we clean up. - name: Configure Bundler to use the bundle folder shared by all app versions command: chdir: "{{ remote_app_root }}" cmd: "/opt/ruby-{{ ruby_version }}/bin/bundle config set --local path {{ remote_project_root}}/shared/bundle" - name: Run `bundle install` to install dependencies in remote folder command: chdir: "{{ remote_app_root }}" # The `--local` flag instructs Bundler to use the cached dependencies # in `vendor/cache`, instead of reading from the web, which is much # faster and more reliable! cmd: /opt/ruby-{{ ruby_version }}/bin/bundle install --local - name: Update the `current` folder to point to the new version file: src: "{{ remote_app_root }}" dest: /srv/impress/current state: link # NOTE: This uses the passwordless sudo rule we set up in deploy:setup. # We write it as a command rather than using the built-in `systemd` Ansible # module, to make sure we're invoking it exactly as we wrote in that rule. # # NOTE: We use `sudo` instead of `become_user: root`, because we don't have # permission to *become* the root user; we only have permission to run this # one command as them. - name: Restart the app become: no command: sudo systemctl restart impress