aboutsummaryrefslogtreecommitdiffhomepage
path: root/tasks/deploy.yml
blob: b394e55ba85fc7e10ab0c4d78202edc92f954090 (plain) (blame)
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
- name: Configure Drain
  when: syslog_server is defined or clever_syslog_server is defined
  command: clever-set-drain.sh
  args:
    chdir: "{{ clever_app_root }}"
  environment:
    SYSLOG_UDP_SERVER: "{{ clever_syslog_server | default(syslog_server) }}"
    CONFIGURATION_FILE: "{{ clever_login_file }}"

- name: Configure Domain
  when: domain is defined or clever_domain is defined
  command: clever-set-domain.sh
  args:
    chdir: "{{ clever_app_root }}"
  environment:
    DOMAIN: "{{ clever_domain | default(domain) }}"
    CONFIGURATION_FILE: "{{ clever_login_file }}"

- name: Push Environment
  shell: "clever env import < {{ clever_app_confdir }}/env"
  args:
    chdir: "{{ clever_app_root }}"
  environment:
    CONFIGURATION_FILE: "{{ clever_login_file }}"
  changed_when: false

#TODO: Improve ssh-key validation
- name: Accept Clever-Cloud servers
  shell: "ssh-keyscan -H push-par-clevercloud-customers.services.clever-cloud.com >> ~/.ssh/known_hosts"
  tags:
    - skip_ansible_lint

- name: Deploy to Clever-Cloud
  shell: "clever deploy --force"
  args:
    chdir: "{{ clever_app_root }}"
  environment:
    CONFIGURATION_FILE: "{{ clever_login_file }}"
  register: clever_deploy
  async: 900 # 15 minutes timeout
  poll: 30
  ignore_errors: true
  tags:
    - skip_ansible_lint

- name: First time push to Clever-Cloud needs a full git clone
  command: "git fetch --unshallow"
  args:
    chdir: "{{ clever_app_root }}"
  when:
    - clever_deploy is failed
    - clever_deploy.stderr is defined
    - clever_deploy.stderr is search("Failed to read git object")
  tags:
    - skip_ansible_lint

- name: Deploy to Clever-Cloud
  shell: "clever deploy --force"
  args:
    chdir: "{{ clever_app_root }}"
  environment:
    CONFIGURATION_FILE: "{{ clever_login_file }}"
  when: clever_deploy is failed
  register: clever_deploy
  async: 900 # 15 minutes timeout
  poll: 30
  ignore_errors: true
  tags:
    - skip_ansible_lint

- name: Return deployment logs
  debug:
    var: clever_deploy.stdout
  when: clever_deploy.stdout is defined

- name: Return deployment errors
  debug:
    var: clever_deploy.stderr
  when:
    - clever_deploy is failed
    - clever_deploy.stderr is defined

- shell: clever activity
  args:
    chdir: "{{ clever_app_root }}"
  environment:
    CONFIGURATION_FILE: "{{ clever_login_file }}"
  changed_when: false
  register: clever_activity_result

- debug:
    var: clever_activity_result.stdout_lines

- shell: git show -q --format=format:%H HEAD
  args:
    chdir: "{{ clever_app_root }}"
  changed_when: false
  register: current_commit_sha

# ####
# Expects all configuration to be located in the project's repository.
# Making a git commit bound to the same *configuration* and *executable* version.
# ##
- name: Fail if current commit is not the last deployed one
  fail:
    msg: "The clever deployment failed! Please check latest deploy activity logs above."
  when:
    - clever_deploy is failed
    - clever_deploy.stderr is defined
    - clever_deploy.stderr is search("application is up-to-date")
    - clever_activity_valid_deploy_keyword in clever_activity_result.stdout_lines[-1]
    - current_commit_sha.stdout_lines[-1] in clever_activity_result.stdout_lines[-1]

- name: Fail on deployment errors
  fail:
    msg: "The clever deployment failed! Please check logs above."
  when:
    - clever_deploy is failed
    - clever_deploy.stderr is defined
    - clever_deploy.stderr is not search("application is up-to-date")