From ee72817febeefb44e7c82481ec1ec48a4cb55ea4 Mon Sep 17 00:00:00 2001 From: Ricard Clau Date: Mon, 23 Mar 2026 19:45:31 +0100 Subject: [PATCH] Clean up failed releases before activation --- tasks/main.yml | 64 ++++++++++++++++++++---------- test/rsync-test.yml | 33 +++++++++++++++ test/tasks/fail-before-symlink.yml | 4 ++ 3 files changed, 81 insertions(+), 20 deletions(-) create mode 100644 test/tasks/fail-before-symlink.yml diff --git a/tasks/main.yml b/tasks/main.yml index 0cced59c..685e9dc4 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,38 +1,62 @@ --- -- include_tasks: "{{ ansistrano_before_setup_tasks_file | default('empty.yml') }}" +- name: ANSISTRANO | Mark release as not activated + set_fact: + ansistrano_release_activated: false -- include_tasks: setup.yml +- block: + - include_tasks: "{{ ansistrano_before_setup_tasks_file | default('empty.yml') }}" -- include_tasks: "{{ ansistrano_after_setup_tasks_file | default('empty.yml') }}" + - include_tasks: setup.yml -- include_tasks: "{{ ansistrano_before_update_code_tasks_file | default('empty.yml') }}" + - include_tasks: "{{ ansistrano_after_setup_tasks_file | default('empty.yml') }}" -- include_tasks: update-code.yml + - include_tasks: "{{ ansistrano_before_update_code_tasks_file | default('empty.yml') }}" -- include_tasks: "{{ ansistrano_after_update_code_tasks_file | default('empty.yml') }}" + - include_tasks: update-code.yml -- include_tasks: "{{ ansistrano_before_symlink_shared_tasks_file | default('empty.yml') }}" + - include_tasks: "{{ ansistrano_after_update_code_tasks_file | default('empty.yml') }}" -- include_tasks: symlink-shared.yml + - include_tasks: "{{ ansistrano_before_symlink_shared_tasks_file | default('empty.yml') }}" -- include_tasks: "{{ ansistrano_after_symlink_shared_tasks_file | default('empty.yml') }}" + - include_tasks: symlink-shared.yml -- include_tasks: "{{ ansistrano_before_symlink_tasks_file | default('empty.yml') }}" + - include_tasks: "{{ ansistrano_after_symlink_shared_tasks_file | default('empty.yml') }}" -- include_tasks: symlink.yml - when: ansistrano_current_via == "symlink" + - include_tasks: "{{ ansistrano_before_symlink_tasks_file | default('empty.yml') }}" -- include_tasks: rsync-deploy.yml - when: ansistrano_current_via == "rsync" + - include_tasks: symlink.yml + when: ansistrano_current_via == "symlink" -- include_tasks: "{{ ansistrano_after_symlink_tasks_file | default('empty.yml') }}" + - include_tasks: rsync-deploy.yml + when: ansistrano_current_via == "rsync" -- include_tasks: "{{ ansistrano_before_cleanup_tasks_file | default('empty.yml') }}" + - name: ANSISTRANO | Mark release as activated + set_fact: + ansistrano_release_activated: true -- include_tasks: cleanup.yml + - include_tasks: "{{ ansistrano_after_symlink_tasks_file | default('empty.yml') }}" -- include_tasks: "{{ ansistrano_after_cleanup_tasks_file | default('empty.yml') }}" + - include_tasks: "{{ ansistrano_before_cleanup_tasks_file | default('empty.yml') }}" -- include_tasks: anon-stats.yml + - include_tasks: cleanup.yml -- include_tasks: funding.yml + - include_tasks: "{{ ansistrano_after_cleanup_tasks_file | default('empty.yml') }}" + + - include_tasks: anon-stats.yml + + - include_tasks: funding.yml + rescue: + - name: ANSISTRANO | Remove failed release + file: + path: "{{ ansistrano_release_path.stdout }}" + state: absent + when: + - ansistrano_release_path is defined + - ansistrano_release_path.stdout is defined + - not ansistrano_release_activated + + - fail: + msg: >- + Deployment failed in task + {{ ansible_failed_task.name | default('unknown task') }}: + {{ ansible_failed_result.msg | default(ansible_failed_result | string) }} diff --git a/test/rsync-test.yml b/test/rsync-test.yml index e2f8f313..04d734a1 100644 --- a/test/rsync-test.yml +++ b/test/rsync-test.yml @@ -69,6 +69,39 @@ roles: - { role: local-ansistrano } +- name: When deploying fails before symlink + hosts: all + vars: + ansistrano_release_version: "99999999999999Z" + ansistrano_deploy_to: "/tmp/my-app.com" + ansistrano_after_setup_tasks_file: "{{ playbook_dir }}/tasks/create-shared-paths.yml" + ansistrano_after_update_code_tasks_file: "{{ playbook_dir }}/tasks/create-internal-paths.yml" + ansistrano_before_symlink_tasks_file: "{{ playbook_dir }}/tasks/fail-before-symlink.yml" + ansistrano_shared_paths: + - blah + - foo/bar + - xxx/yyy/zzz + ansistrano_shared_files: + - test.txt + - files/test.txt + ignore_errors: yes + roles: + - { role: local-ansistrano } + +- name: Then the failed release should be cleaned up + hosts: all + vars: + ansistrano_deploy_to: "/tmp/my-app.com" + ansistrano_release_version: "99999999999999Z" + tasks: + - name: Assert failed release path does not exist + stat: + path: "{{ ansistrano_deploy_to }}/releases/{{ ansistrano_release_version }}" + register: st + - debug: + msg: "Failed release path was cleaned up" + when: st.stat.exists is defined and not st.stat.exists + # Tests for rsync strategy with "current" via rsync (instead of the default symlink) - name: When deploying with rsync current strategy hosts: all diff --git a/test/tasks/fail-before-symlink.yml b/test/tasks/fail-before-symlink.yml new file mode 100644 index 00000000..58d2f5cb --- /dev/null +++ b/test/tasks/fail-before-symlink.yml @@ -0,0 +1,4 @@ +--- +- name: Fail before symlink step + fail: + msg: "Intentional test failure before symlink"