Skip to content

Commit ee72817

Browse files
committed
Clean up failed releases before activation
1 parent e2b8d0d commit ee72817

3 files changed

Lines changed: 81 additions & 20 deletions

File tree

‎tasks/main.yml‎

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,62 @@
11
---
2-
- include_tasks: "{{ ansistrano_before_setup_tasks_file | default('empty.yml') }}"
2+
- name: ANSISTRANO | Mark release as not activated
3+
set_fact:
4+
ansistrano_release_activated: false
35

4-
- include_tasks: setup.yml
6+
- block:
7+
- include_tasks: "{{ ansistrano_before_setup_tasks_file | default('empty.yml') }}"
58

6-
- include_tasks: "{{ ansistrano_after_setup_tasks_file | default('empty.yml') }}"
9+
- include_tasks: setup.yml
710

8-
- include_tasks: "{{ ansistrano_before_update_code_tasks_file | default('empty.yml') }}"
11+
- include_tasks: "{{ ansistrano_after_setup_tasks_file | default('empty.yml') }}"
912

10-
- include_tasks: update-code.yml
13+
- include_tasks: "{{ ansistrano_before_update_code_tasks_file | default('empty.yml') }}"
1114

12-
- include_tasks: "{{ ansistrano_after_update_code_tasks_file | default('empty.yml') }}"
15+
- include_tasks: update-code.yml
1316

14-
- include_tasks: "{{ ansistrano_before_symlink_shared_tasks_file | default('empty.yml') }}"
17+
- include_tasks: "{{ ansistrano_after_update_code_tasks_file | default('empty.yml') }}"
1518

16-
- include_tasks: symlink-shared.yml
19+
- include_tasks: "{{ ansistrano_before_symlink_shared_tasks_file | default('empty.yml') }}"
1720

18-
- include_tasks: "{{ ansistrano_after_symlink_shared_tasks_file | default('empty.yml') }}"
21+
- include_tasks: symlink-shared.yml
1922

20-
- include_tasks: "{{ ansistrano_before_symlink_tasks_file | default('empty.yml') }}"
23+
- include_tasks: "{{ ansistrano_after_symlink_shared_tasks_file | default('empty.yml') }}"
2124

22-
- include_tasks: symlink.yml
23-
when: ansistrano_current_via == "symlink"
25+
- include_tasks: "{{ ansistrano_before_symlink_tasks_file | default('empty.yml') }}"
2426

25-
- include_tasks: rsync-deploy.yml
26-
when: ansistrano_current_via == "rsync"
27+
- include_tasks: symlink.yml
28+
when: ansistrano_current_via == "symlink"
2729

28-
- include_tasks: "{{ ansistrano_after_symlink_tasks_file | default('empty.yml') }}"
30+
- include_tasks: rsync-deploy.yml
31+
when: ansistrano_current_via == "rsync"
2932

30-
- include_tasks: "{{ ansistrano_before_cleanup_tasks_file | default('empty.yml') }}"
33+
- name: ANSISTRANO | Mark release as activated
34+
set_fact:
35+
ansistrano_release_activated: true
3136

32-
- include_tasks: cleanup.yml
37+
- include_tasks: "{{ ansistrano_after_symlink_tasks_file | default('empty.yml') }}"
3338

34-
- include_tasks: "{{ ansistrano_after_cleanup_tasks_file | default('empty.yml') }}"
39+
- include_tasks: "{{ ansistrano_before_cleanup_tasks_file | default('empty.yml') }}"
3540

36-
- include_tasks: anon-stats.yml
41+
- include_tasks: cleanup.yml
3742

38-
- include_tasks: funding.yml
43+
- include_tasks: "{{ ansistrano_after_cleanup_tasks_file | default('empty.yml') }}"
44+
45+
- include_tasks: anon-stats.yml
46+
47+
- include_tasks: funding.yml
48+
rescue:
49+
- name: ANSISTRANO | Remove failed release
50+
file:
51+
path: "{{ ansistrano_release_path.stdout }}"
52+
state: absent
53+
when:
54+
- ansistrano_release_path is defined
55+
- ansistrano_release_path.stdout is defined
56+
- not ansistrano_release_activated
57+
58+
- fail:
59+
msg: >-
60+
Deployment failed in task
61+
{{ ansible_failed_task.name | default('unknown task') }}:
62+
{{ ansible_failed_result.msg | default(ansible_failed_result | string) }}

‎test/rsync-test.yml‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,39 @@
6969
roles:
7070
- { role: local-ansistrano }
7171

72+
- name: When deploying fails before symlink
73+
hosts: all
74+
vars:
75+
ansistrano_release_version: "99999999999999Z"
76+
ansistrano_deploy_to: "/tmp/my-app.com"
77+
ansistrano_after_setup_tasks_file: "{{ playbook_dir }}/tasks/create-shared-paths.yml"
78+
ansistrano_after_update_code_tasks_file: "{{ playbook_dir }}/tasks/create-internal-paths.yml"
79+
ansistrano_before_symlink_tasks_file: "{{ playbook_dir }}/tasks/fail-before-symlink.yml"
80+
ansistrano_shared_paths:
81+
- blah
82+
- foo/bar
83+
- xxx/yyy/zzz
84+
ansistrano_shared_files:
85+
- test.txt
86+
- files/test.txt
87+
ignore_errors: yes
88+
roles:
89+
- { role: local-ansistrano }
90+
91+
- name: Then the failed release should be cleaned up
92+
hosts: all
93+
vars:
94+
ansistrano_deploy_to: "/tmp/my-app.com"
95+
ansistrano_release_version: "99999999999999Z"
96+
tasks:
97+
- name: Assert failed release path does not exist
98+
stat:
99+
path: "{{ ansistrano_deploy_to }}/releases/{{ ansistrano_release_version }}"
100+
register: st
101+
- debug:
102+
msg: "Failed release path was cleaned up"
103+
when: st.stat.exists is defined and not st.stat.exists
104+
72105
# Tests for rsync strategy with "current" via rsync (instead of the default symlink)
73106
- name: When deploying with rsync current strategy
74107
hosts: all

‎test/tasks/fail-before-symlink.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
- name: Fail before symlink step
3+
fail:
4+
msg: "Intentional test failure before symlink"

0 commit comments

Comments
 (0)