Some checks failed
🚀 Build & Deploy Image / Determine Build Necessity (push) Successful in 30s
Security Vulnerability Scan / Check for Dependency Changes (push) Failing after 37s
🚀 Build & Deploy Image / Run Tests & Quality Checks (push) Has been skipped
Security Vulnerability Scan / Composer Security Audit (push) Has been skipped
🚀 Build & Deploy Image / Build Runtime Base Image (push) Failing after 13m31s
🚀 Build & Deploy Image / Build Docker Image (push) Has been cancelled
🚀 Build & Deploy Image / Auto-deploy to Staging (push) Has been cancelled
🚀 Build & Deploy Image / Auto-deploy to Production (push) Has been cancelled
- Add retry logic (5 retries, 10s delay) to git clone and update tasks - Handle 504 Gateway Timeout errors from Gitea gracefully - Fail with clear error message if all retries are exhausted - Prevents workflow failures due to temporary Gitea unavailability
168 lines
5.4 KiB
YAML
168 lines
5.4 KiB
YAML
---
|
|
- name: Deploy Application Code via Git
|
|
hosts: "{{ deployment_hosts | default('production') }}"
|
|
gather_facts: yes
|
|
become: no
|
|
|
|
vars:
|
|
application_code_dest: "/home/deploy/michaelschiemer/current"
|
|
git_repository_url_default: "https://git.michaelschiemer.de/michael/michaelschiemer.git"
|
|
# Determine branch based on environment
|
|
git_branch: >-
|
|
{%- if deployment_environment == 'staging' -%}
|
|
{{ git_branch | default('staging') }}
|
|
{%- else -%}
|
|
{{ git_branch | default('main') }}
|
|
{%- endif -%}
|
|
git_token: "{{ git_token | default('') }}"
|
|
# Deployment environment (staging or production)
|
|
deployment_environment: "{{ deployment_environment | default('production') }}"
|
|
|
|
tasks:
|
|
- name: Set git_repo_url from provided value or default
|
|
set_fact:
|
|
git_repo_url: "{{ git_repository_url if (git_repository_url is defined and git_repository_url != '') else git_repository_url_default }}"
|
|
|
|
- name: Ensure Git is installed
|
|
ansible.builtin.apt:
|
|
name: git
|
|
state: present
|
|
update_cache: no
|
|
become: yes
|
|
|
|
- name: Ensure application code directory exists
|
|
file:
|
|
path: "{{ application_code_dest }}"
|
|
state: directory
|
|
owner: "{{ ansible_user }}"
|
|
group: "{{ ansible_user }}"
|
|
mode: '0755'
|
|
become: yes
|
|
|
|
- name: Check if repository already exists
|
|
stat:
|
|
path: "{{ application_code_dest }}/.git"
|
|
register: git_repo_exists
|
|
|
|
- name: Check if destination directory exists
|
|
stat:
|
|
path: "{{ application_code_dest }}"
|
|
register: dest_dir_exists
|
|
|
|
- name: Remove destination directory if it exists but is not a git repo
|
|
file:
|
|
path: "{{ application_code_dest }}"
|
|
state: absent
|
|
when: dest_dir_exists.stat.exists and not git_repo_exists.stat.exists
|
|
become: yes
|
|
|
|
- name: Clone repository (if not exists)
|
|
ansible.builtin.git:
|
|
repo: "{{ git_repo_url }}"
|
|
dest: "{{ application_code_dest }}"
|
|
version: "{{ git_branch }}"
|
|
force: no
|
|
update: no
|
|
when: not git_repo_exists.stat.exists
|
|
environment:
|
|
GIT_TERMINAL_PROMPT: "0"
|
|
vars:
|
|
ansible_become: no
|
|
register: git_clone_result
|
|
retries: 5
|
|
delay: 10
|
|
until: git_clone_result is succeeded
|
|
ignore_errors: yes
|
|
|
|
- name: Fail if git clone failed after retries
|
|
fail:
|
|
msg: "Failed to clone repository after 5 retries. Gitea may be unreachable or overloaded. Last error: {{ git_clone_result.msg | default('Unknown error') }}"
|
|
when:
|
|
- not git_repo_exists.stat.exists
|
|
- git_clone_result is failed
|
|
|
|
- name: Update repository (if exists)
|
|
ansible.builtin.git:
|
|
repo: "{{ git_repo_url }}"
|
|
dest: "{{ application_code_dest }}"
|
|
version: "{{ git_branch }}"
|
|
force: yes
|
|
update: yes
|
|
when: git_repo_exists.stat.exists
|
|
environment:
|
|
GIT_TERMINAL_PROMPT: "0"
|
|
vars:
|
|
ansible_become: no
|
|
register: git_update_result
|
|
retries: 5
|
|
delay: 10
|
|
until: git_update_result is succeeded
|
|
ignore_errors: yes
|
|
|
|
- name: Fail if git update failed after retries
|
|
fail:
|
|
msg: "Failed to update repository after 5 retries. Gitea may be unreachable or overloaded. Last error: {{ git_update_result.msg | default('Unknown error') }}"
|
|
when:
|
|
- git_repo_exists.stat.exists
|
|
- git_update_result is failed
|
|
|
|
- name: Set ownership of repository files
|
|
file:
|
|
path: "{{ application_code_dest }}"
|
|
owner: "{{ ansible_user }}"
|
|
group: "{{ ansible_user }}"
|
|
recurse: yes
|
|
become: yes
|
|
|
|
- name: Ensure executable permissions on PHP scripts
|
|
file:
|
|
path: "{{ application_code_dest }}/{{ item }}"
|
|
mode: '0755'
|
|
loop:
|
|
- worker.php
|
|
- console.php
|
|
ignore_errors: yes
|
|
|
|
- name: Verify worker.php exists
|
|
stat:
|
|
path: "{{ application_code_dest }}/worker.php"
|
|
register: worker_php_stat
|
|
|
|
- name: Verify console.php exists
|
|
stat:
|
|
path: "{{ application_code_dest }}/console.php"
|
|
register: console_php_stat
|
|
|
|
- name: Verify composer.json exists
|
|
stat:
|
|
path: "{{ application_code_dest }}/composer.json"
|
|
register: composer_json_stat
|
|
|
|
- name: Get current Git commit hash
|
|
shell: |
|
|
cd {{ application_code_dest }} && git rev-parse HEAD
|
|
register: git_commit_hash
|
|
changed_when: false
|
|
when: git_repo_exists.stat.exists
|
|
|
|
- name: Display file verification results
|
|
debug:
|
|
msg: |
|
|
File Verification:
|
|
- worker.php: {{ 'EXISTS' if worker_php_stat.stat.exists else 'MISSING' }}
|
|
- console.php: {{ 'EXISTS' if console_php_stat.stat.exists else 'MISSING' }}
|
|
- composer.json: {{ 'EXISTS' if composer_json_stat.stat.exists else 'MISSING' }}
|
|
- Git Branch: {{ git_branch }}
|
|
- Git Commit: {{ git_commit_hash.stdout | default('N/A') }}
|
|
|
|
- name: Fail if critical files are missing
|
|
fail:
|
|
msg: |
|
|
Critical files are missing after Git deployment:
|
|
{% if not worker_php_stat.stat.exists %}- worker.php{% endif %}
|
|
{% if not console_php_stat.stat.exists %}- console.php{% endif %}
|
|
{% if not composer_json_stat.stat.exists %}- composer.json{% endif %}
|
|
when:
|
|
- not worker_php_stat.stat.exists or not console_php_stat.stat.exists or not composer_json_stat.stat.exists
|
|
|