Skip to content

Instantly share code, notes, and snippets.

@mig5
Last active August 9, 2016 07:21
Show Gist options
  • Save mig5/d96aac18b157bea567a2 to your computer and use it in GitHub Desktop.
Save mig5/d96aac18b157bea567a2 to your computer and use it in GitHub Desktop.
Ansible playbook to deploy Drupal
---
- hosts: all
gather_facts: no
tasks:
- name: Backup database
shell: "{{ item }}"
with_items:
- mkdir -p ~jenkins/dbbackups
- drush @{{ shortname }}_{{ env }} sql-dump --skip-tables-key=common | gzip > ~jenkins/dbbackups/{{ shortname }}_{{ env }}_prior_to_{{ build }}.sql.gz
- name: Clone repo as new build
git:
repo: "{{ repo }}"
dest: /home/jenkins/{{ shortname}}_{{ env }}_{{ build }}
version: "{{ branch }}"
accept_hostkey: yes
- name: Move build into position
command: mv /home/jenkins/{{ shortname}}_{{ env }}_{{ build }} /var/www/{{ shortname}}_{{ env }}_{{ build }}
sudo: true
- name: Assign symlinks
command: "{{ item }} chdir=/var/www/{{ shortname}}_{{ env }}_{{ build }}/www/sites/{{ url }}"
with_items:
- ln -s /var/www/shared/{{ shortname }}_{{ env}}.settings.inc settings.php
- ln -s /var/www/shared/{{ shortname }}_{{ env}}_files files
- name: Harden permissions
shell: "{{ item }}"
with_items:
- find /var/www/{{ shortname}}_{{ env }}_{{ build }} -type d -print0 | xargs -0 -r chmod 555
- find /var/www/{{ shortname}}_{{ env }}_{{ build }} -type f -print0 | xargs -0 -r chmod 444
sudo: true
- name: Test new build
shell: chdir=/var/www/{{ shortname}}_{{ env }}_{{ build }}/www/sites/{{ url }} drush status
- name: Maintenance mode on
command: drush @{{ shortname }}_{{ env }} -y vset maintenance_mode 1
- name: Apply updates
shell: chdir=/var/www/{{ shortname}}_{{ env }}_{{ build }}/www/sites/{{ url }} drush -y updatedb
register: result
ignore_errors: true
- debug: var=result.stderr.split('\n')
- name: Revert database
shell: if [ -f ~jenkins/dbbackups/{{ shortname}}_{{ env }}_prior_to_{{ build }}.sql.gz ]; then drush -y @{{ shortname }}_{{ env }} sql-drop; zcat ~jenkins/dbbackups/{{ shortname}}_{{ env }}_prior_to_{{ build }}.sql.gz | drush @{{ shortname }}_{{ env }} sql-cli; exit 1; fi
when: result|failed
- name: Maintenance mode off
command: drush @{{ shortname }}_{{ env }} -y vset maintenance_mode 0
- name: Clear cache
command: drush @{{ shortname }}_{{ env }} -y cc all
- name: Take new build live
command: "{{ item }} chdir=/var/www"
with_items:
- unlink live.{{ shortname }}.{{ env }}
- ln -s {{ shortname}}_{{ env }}_{{ build }} live.{{ shortname }}.{{ env }}
- drush @{{ shortname }}_{{ env }} cc all
sudo: true
- name: Remove old builds to conserve disk space
command: /usr/local/bin/remove_old_builds.sh -d /var/www -r {{ shortname }} -b {{ env }} -k 5
sudo: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment