Skip to content

Instantly share code, notes, and snippets.

@shehabic
Created November 27, 2020 03:38
Show Gist options
  • Save shehabic/a61e69e2396c15302c6b9dbccb4962fb to your computer and use it in GitHub Desktop.
Save shehabic/a61e69e2396c15302c6b9dbccb4962fb to your computer and use it in GitHub Desktop.
Ansible Laravel Deployment Playbook (for Centos) but Ubuntu should be similar as well
# This is an example file to display laravel project directly through Ansible
# assuming that you have a folder /var/www/my_website with "shared" and "releases" subfolders,
# "shared" subfolder has "bootstrap/cache" and "storage/cache", "storage/sessions", "storage/views" subfolders as well as "logs"
#
# - also assuming that your ssh user has access to github (his keys are added to github)
# - also assuming you have installed composer and git on your deployment server:
---
- hosts: all
tasks:
# Typical usage
- name: Initialize the deploy root and gather facts
community.general.deploy_helper:
path: /var/www/sites/my_website/
keep_releases: 5
shared_path: /var/www/sites/my_website/shared
- name: Clone the project to the new release folder
git:
repo: git@github.com:shehabic/my_website.git
dest: '{{ deploy_helper.new_release_path }}'
- name: Add an unfinished file, to allow cleanup on successful finalize
file:
path: '{{ deploy_helper.new_release_path }}/{{ deploy_helper.unfinished_filename }}'
state: touch
- name: Add symlinks from the new release to the shared folder
file:
path: '{{ deploy_helper.new_release_path }}/{{ item.path }}'
src: '{{ deploy_helper.shared_path }}/{{ item.src }}'
state: link
with_items:
- path: bootstrap/cache
src: bootstrap/cache
- path: storage/framework
src: storage/framework
- path: storage/logs
src: storage/logs
- name: Perform some build steps, like running your dependency manager for example
command: 'composer install --no-dev --no-scripts --optimize-autoloader'
register: command_output
args:
chdir: '{{ deploy_helper.new_release_path }}'
- debug: var=command_output
# Here create folder "web-confid" and put your production .env file inside it.
- name: Copy env config file
copy:
src: ./web-configs/my_website.env
dest: '{{ deploy_helper.new_release_path }}/.env'
become: yes
- name: Make artisan file executable
file:
path: '{{ deploy_helper.new_release_path }}/artisan'
state: file
mode: u+x
- name: Clear compiled cache and data
command: 'php artisan clear-compiled'
args:
chdir: '{{ deploy_helper.new_release_path }}'
- name: Cache Configs
command: 'php artisan config:cache'
args:
chdir: '{{ deploy_helper.new_release_path }}'
- name: Optimiza Dependency Container
command: 'php artisan config:cache'
args:
chdir: '{{ deploy_helper.new_release_path }}'
- name: Generate news cahe
command: 'php artisan news:generate-cache -vvv'
register: command_output
args:
chdir: '{{ deploy_helper.new_release_path }}'
- debug: var=command_output
- name: Finalize the deploy, removing the unfinished file and switching the symlink
community.general.deploy_helper:
path: /var/www/sites/my_website/
release: '{{ deploy_helper.new_release }}'
state: finalize
clean: True
- name: restart php-fpm
service:
name: php-fpm
state: restarted
become: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment