feat: Integrate Ansible playbooks into CI/CD workflows
- Add deploy-application-code.yml for Git-based code deployment - Add install-composer-dependencies.yml for dependency installation - Add deploy-image.yml for Docker image deployment - Update build-image.yml to use Ansible playbooks - Update manual-deploy.yml to use Ansible playbooks - Add ANSIBLE_VAULT_PASSWORD secret handling
This commit is contained in:
123
deployment/ansible/playbooks/deploy-application-code.yml
Normal file
123
deployment/ansible/playbooks/deploy-application-code.yml
Normal file
@@ -0,0 +1,123 @@
|
||||
---
|
||||
- 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: "{{ 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: 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: Clone repository (if not exists)
|
||||
ansible.builtin.git:
|
||||
repo: "{{ git_repository_url }}"
|
||||
dest: "{{ application_code_dest }}"
|
||||
version: "{{ git_branch }}"
|
||||
force: no
|
||||
update: no
|
||||
owner: "{{ ansible_user }}"
|
||||
group: "{{ ansible_user }}"
|
||||
when: not git_repo_exists.stat.exists
|
||||
environment:
|
||||
GIT_TERMINAL_PROMPT: "0"
|
||||
vars:
|
||||
ansible_become: no
|
||||
|
||||
- name: Update repository (if exists)
|
||||
ansible.builtin.git:
|
||||
repo: "{{ git_repository_url }}"
|
||||
dest: "{{ application_code_dest }}"
|
||||
version: "{{ git_branch }}"
|
||||
force: yes
|
||||
update: yes
|
||||
owner: "{{ ansible_user }}"
|
||||
group: "{{ ansible_user }}"
|
||||
when: git_repo_exists.stat.exists
|
||||
environment:
|
||||
GIT_TERMINAL_PROMPT: "0"
|
||||
vars:
|
||||
ansible_become: no
|
||||
|
||||
- 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
|
||||
|
||||
Reference in New Issue
Block a user