-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-git.yml
141 lines (124 loc) · 4.87 KB
/
deploy-git.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
---
- hosts: 127.0.0.1
connection: local
vars:
repo_root: "{{ deploy_repo_root | default('/var/www/html') }}"
drupal_root: "{{ deploy_drupal_root | default(repo_root ~ '/drupal') }}"
backup_dir: "{{ deploy_backup_dir | default(repo_root ~ '/../backups') }}"
git_branch: "{{ deploy_git_branch | default }}"
git_tag: "{{ deploy_git_tag | default }}"
sql_dump_timeout: "{{ drush_sqldump_timeout | default(180) }}"
tasks:
- name: Ensure a deploy branch XOR tag is defined
ansible.builtin.assert:
that: >-
(git_branch and not git_tag)
or
(not git_branch and git_tag)
msg: "A deploy branch xor tag MUST be defined!"
- block:
- name: Git - Get current remote URL
ansible.builtin.command:
cmd: git remote get-url origin
chdir: "{{ repo_root }}"
register: git_remote
changed_when: false
- ansible.builtin.set_fact:
git_repo_url: "{{ git_remote.stdout }}"
when: (git_repo_url is undefined) or (git_repo_url == "")
- name: Git - Ensure no uncommitted changes
ansible.builtin.command:
cmd: git status --porcelain --untracked-files=no
chdir: "{{ repo_root }}"
register: git_status
changed_when: false
failed_when: >
(git_status.rc != 0) or
(git_status.stdout_lines | length > 0)
- name: Git - Fetch
ansible.builtin.command:
cmd: git fetch --tags --prune --prune-tags
chdir: "{{ repo_root }}"
register: git_fetch
changed_when: "git_fetch.stdout_lines | length > 0"
- name: Git - Get current branch
ansible.builtin.command:
cmd: git branch --show-current
chdir: "{{ repo_root }}"
register: git_branch_show
changed_when: false
- name: Git - Get current tag
ansible.builtin.command:
cmd: git describe --exact-match --tags
chdir: "{{ repo_root }}"
register: git_describe_tags
changed_when: false
failed_when:
- git_describe_tags.rc != 0
- "'no tag exactly matches' not in git_describe_tags.stderr"
- ansible.builtin.set_fact:
git_current_branch: "{{ git_branch_show.stdout | trim }}"
git_current_tag: "{{ git_describe_tags.stdout | trim }}"
- ansible.builtin.debug:
msg: "Current branch is: '{{ git_current_branch | default('N/A') }}'"
- ansible.builtin.debug:
msg: "Current tag is: '{{ git_current_tag | default('N/A') }}'"
- when: git_branch
block:
- name: Git - Check for new commits when branch matches
ansible.builtin.git:
repo: "{{ git_repo_url }}"
dest: "{{ repo_root }}"
version: "{{ git_ref }}"
update: false
register: git_check_update
when: "git_branch == git_current_branch"
- when: "git_check_update.before == git_check_update.after"
block:
- ansible.builtin.debug:
msg: "Branch already at the latest commit. Commit was: {{ git_check_update.before }}"
- ansible.builtin.meta: end_play
- when: git_tag
block:
- when: "git_current_tag == git_tag"
block:
- ansible.builtin.debug:
msg: "Already at the required tag: {{ git_current_tag }}"
- ansible.builtin.meta: end_play
- name: Drush - Run backup
block:
- name: Ensure backups directory exists
tags: backup
ansible.builtin.file:
path: "{{ backup_dir }}"
state: directory
mode: '0755'
- name: Drush - Run backup and cleanup old backups
tags: backup
ansible.builtin.shell:
# We have some trouble with the drush sql:dump command hanging,
# therefore we addd a "maximun runtime" usint the "timeout" utility.
# @see: https://github.com/nymedia/internal-operations-support/issues/35
cmd: |
BACKUP_FILE="{{ backup_dir }}/db-backup-$(date +%Y-%m-%d_%H-%M-%S).sql"
timeout "{{ sql_dump_timeout }}" drush sql:dump --no-ansi --no-interaction \
--gzip --result-file="$BACKUP_FILE"
ls -t {{ backup_dir }}/db-backup-*.sql.gz | tail -n +4 | xargs rm -f
chdir: "{{ drupal_root }}"
register: drush_sqldump
# Because of the "timeout" workaround allow 3 failures before giving up.
retries: 3
delay: 10
until: drush_sqldump.stderr.find("[success]") != -1
- name: Git - Checkout new version
ansible.builtin.git:
repo: "{{ git_repo_url }}"
dest: "{{ repo_root }}"
version: "{{ git_tag or git_branch }}"
update: true
register: git_checkout
- name: Composer build
ansible.builtin.shell:
cmd: composer build
chdir: "{{ repo_root }}"
register: composer_build