diff --git a/tasks/start.yaml b/handlers/main.yaml similarity index 54% rename from tasks/start.yaml rename to handlers/main.yaml index 27e37b9..283dafa 100644 --- a/tasks/start.yaml +++ b/handlers/main.yaml @@ -1,12 +1,16 @@ -- name: Start Gitea +- name: Ensure Gitea is running community.docker.docker_compose: project_src: /opt/gitea - register: docker_compose -- name: Check if {{ gitea.hostname }}.{{ gitea.domain }} is available and returning status 200 +- name: Check Gitea HTTP Response ansible.builtin.uri: url: "https://{{ gitea.hostname }}.{{ gitea.domain }}" register: _result until: _result.status == 200 retries: 30 delay: 2 + +- name: Output Docker Compose State + ansible.builtin.debug: + var: docker_compose + when: gitea_docker_compose.changed diff --git a/tasks/main.yaml b/tasks/main.yaml index 36f2d4d..d5d4bd0 100644 --- a/tasks/main.yaml +++ b/tasks/main.yaml @@ -39,44 +39,54 @@ enabled: true state: started -- name: Backup Files +- name: Stop Gitea for Backup + community.docker.docker_compose: + project_src: /opt/gitea + state: present + stopped: true + notify: + - Ensure Gitea is running + +- name: Backup Gitea Directory + ansible.builtin.copy: + src: /opt/gitea/ + dest: "/opt/gitea.backup@{{ lookup('pipe', 'date --utc --iso-8601=seconds') }}" + mode: "700" + remote_src: true + directory_mode: true + register: backup + +- name: Upload Environment File + ansible.builtin.template: + src: environment.env.j2 + dest: "/opt/gitea/environment.env" + owner: root + mode: "600" + +- name: Upload docker-compose.yaml + ansible.builtin.template: + src: docker-compose.yaml.j2 + dest: /opt/gitea/docker-compose.yaml + owner: root + mode: "600" + validate: /usr/bin/docker compose -f %s config -q + notify: + - Output Docker Compose State + +- name: Try to reach Gitea block: - - name: Stop Gitea for Backup + + - name: Start Gitea community.docker.docker_compose: project_src: /opt/gitea - state: present - stopped: true - - name: Backup Gitea Directory - ansible.builtin.copy: - src: /opt/gitea/ - dest: "/opt/gitea.backup@{{ lookup('pipe', 'date --utc --iso-8601=seconds') }}" - mode: "700" - remote_src: true - directory_mode: true - register: backup - -- name: Apply Changes - block: - - name: Upload Environment File - ansible.builtin.template: - src: environment.env.j2 - dest: "/opt/gitea/environment.env" - owner: root - mode: "600" - register: gitea_env - - - name: Upload docker-compose.yaml - ansible.builtin.template: - src: docker-compose.yaml.j2 - dest: /opt/gitea/docker-compose.yaml - owner: root - mode: "600" - validate: /usr/bin/docker compose -f %s config -q - - - name: Start Gitea and try reaching it - ansible.builtin.include_tasks: - file: start.yaml + - name: Check if {{ gitea.hostname }}.{{ gitea.domain }} is available and returning status 200 + ansible.builtin.uri: + url: "https://{{ gitea.hostname }}.{{ gitea.domain }}" + register: _result + until: _result.status == 200 + retries: 30 + delay: 2 rescue: - name: Stop Gitea for Backup @@ -84,6 +94,8 @@ project_src: /opt/gitea state: present stopped: true + notify: + - Ensure Gitea is running - name: Restore Gitea Directory from Backup ansible.builtin.copy: @@ -91,12 +103,5 @@ dest: "{{ backup.src }}" remote_src: true mode: "700" - - - name: Start Gitea and try reaching it - ansible.builtin.include_tasks: - file: start.yaml - - always: - - name: Output Docker Compose State - ansible.builtin.debug: - var: docker_compose + notify: + - Check Gitea HTTP Response