added ansible playbook
This commit is contained in:
91
ansible/roles/weekly/tasks/main.yaml
Normal file
91
ansible/roles/weekly/tasks/main.yaml
Normal file
@@ -0,0 +1,91 @@
|
||||
---
|
||||
- name: Check if base_dir exists
|
||||
stat:
|
||||
path: "{{ prod_base_dir }}"
|
||||
register: base_dir_status
|
||||
|
||||
- name: Fetch latest code
|
||||
git:
|
||||
repo: "{{ git_repo }}"
|
||||
dest: "{{ prod_base_dir }}"
|
||||
version: "master"
|
||||
update: yes
|
||||
force: true
|
||||
register: git_result
|
||||
|
||||
- debug:
|
||||
var: git_result
|
||||
|
||||
- name: Download and installs all libs and dependencies
|
||||
community.general.composer:
|
||||
command: install
|
||||
arguments: --no-dev --optimize-autoloader
|
||||
working_dir: "{{ prod_base_dir }}"
|
||||
php_path: /usr/bin/keyhelp-php84
|
||||
|
||||
- name: Install node_modules
|
||||
npm:
|
||||
path: "{{ prod_base_dir }}"
|
||||
state: present
|
||||
when: git_result.changed
|
||||
|
||||
- name: Build frontend
|
||||
command: "npm run build"
|
||||
args:
|
||||
chdir: "{{ prod_base_dir }}"
|
||||
|
||||
- name: Clear config cache
|
||||
command: "keyhelp-php84 artisan config:clear"
|
||||
args:
|
||||
chdir: "{{ prod_base_dir }}"
|
||||
|
||||
- name: Clear application cache
|
||||
command: "keyhelp-php84 artisan cache:clear"
|
||||
args:
|
||||
chdir: "{{ prod_base_dir }}"
|
||||
|
||||
- name: Create database backup directory
|
||||
file:
|
||||
path: "{{ prod_base_dir }}/backups"
|
||||
state: directory
|
||||
mode: '0755'
|
||||
|
||||
- name: Backup database before migrations
|
||||
shell: |
|
||||
cd {{ prod_base_dir }}
|
||||
DB_USERNAME=$(grep DB_USERNAME .env | cut -d '=' -f2)
|
||||
DB_PASSWORD=$(grep DB_PASSWORD .env | cut -d '=' -f2)
|
||||
DB_DATABASE=$(grep DB_DATABASE .env | cut -d '=' -f2)
|
||||
BACKUP_FILE="{{ prod_base_dir }}/backups/db_backup_$(date +%Y%m%d_%H%M%S).sql"
|
||||
mysqldump -u "$DB_USERNAME" -p"$DB_PASSWORD" "$DB_DATABASE" > "$BACKUP_FILE"
|
||||
echo "$BACKUP_FILE"
|
||||
register: backup_result
|
||||
|
||||
- name: Display backup location
|
||||
debug:
|
||||
msg: "Database backed up to: {{ backup_result.stdout }}"
|
||||
|
||||
- name: Run database migrations safely
|
||||
command: "keyhelp-php84 artisan migrate:safe --force"
|
||||
args:
|
||||
chdir: "{{ prod_base_dir }}"
|
||||
register: migrate_result
|
||||
failed_when: migrate_result.rc != 0
|
||||
|
||||
- name: Display migration result
|
||||
debug:
|
||||
var: migrate_result
|
||||
|
||||
- name: Remove old database backups (keep last 10)
|
||||
shell: |
|
||||
cd {{ prod_base_dir }}/backups
|
||||
ls -t db_backup_*.sql | tail -n +11 | xargs -r rm
|
||||
ignore_errors: yes
|
||||
|
||||
- name: Run version fetch command
|
||||
command: "keyhelp-php84 artisan version:fetch"
|
||||
args:
|
||||
chdir: "{{ prod_base_dir }}"
|
||||
|
||||
- name: Reload PHP-FPM to clear OPcache
|
||||
command: sudo /usr/bin/systemctl reload keyhelp-php84-fpm.service
|
||||
Reference in New Issue
Block a user