aboutsummaryrefslogtreecommitdiffstats
path: root/roles/run-ci/tasks/main.yml
blob: eed27f821cd5c23ee860bdcca695c7d755ad1406 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
---

##
# Prepare base of variables to send
##
- name: prepare variables to sent
  set_fact:
    params:
      {
        'token': "{{ config.trigger_token }}",
        'ref': "{{ config.branch }}",
        'variables[source_job_name]': "{{ step }}",
        'variables[pod]': "{{ inventory_hostname }}",
        'variables[jumphost]': "{{ jumphost }}",
      }

##
# Prepare the artifacts to get
##

- name: add bin artifacts param
  when: artifacts_bin is defined
  set_fact:
    params:
      "{{ params|combine({'variables[artifacts_bin]': artifacts_bin }) }}"

- name: add src artifacts param
  when: artifacts_src is defined
  set_fact:
    params:
      "{{ params|combine({'variables[artifacts_src]': artifacts_src }) }}"

- name: ensure artifacts.zip is not present
  file:
    path: "{{ playbook_dir }}/artifacts.zip"
    state: absent

- name: set healthchecks base url
  set_fact:
    base_url: "{{ gitlab.healthchecks_url }}/ping/{{ healthchecks_id }}"
  when: healthchecks_id is defined

##
# Run the step
##
- name: Run step
  block:
    ##
    # add step parameters in the parameters to send
    ##
    - name: Add step parameters
      set_fact:
        params: "{{ params|combine({key: value}) }}"
      vars:
        key: "variables[{{ item.key }}]"
        value: "{{ item.value }}"
      with_dict: "{{ config.parameters }}"
      when: config.parameters is defined and config.parameters != None

    ##
    # add NOVAULT_LIST parameter in the parameters to send
    ##
    - name: Add NOVAULT_LIST parameter
      set_fact:
        params: "{{ params|combine({key: value}) }}"
      vars:
        key: "variables[NOVAULT_LIST]"
        value: "{{ config.novault |join(\"\n\") }}"
      when: config.novault is defined

    ##
    # Trigger the pipeline
    ##
    - name: "Trigger a new pipeline for step {{ step }}"
      uri:
        url: "{{ config.api }}/trigger/pipeline"
        method: POST
        status_code: 201
        body_format: raw
        body: "{{ params| urlencode }}"
        headers:
          Content-Type: "application/x-www-form-urlencoded"
      register: trigger_out

    - name: set pipeline url
      set_fact:
        pipeline_url: "{{ config.url }}/pipelines/{{ trigger_out.json.id }}"
        api_pipeline_url: "{{ config.api }}/pipelines/{{ trigger_out.json.id }}"

    - name: Echo running pipeline link
      debug:
        msg: "{{ msg.split('\n') }}"
      vars:
        msg: |
          ******************************************************************
          * Pipeline triggered for step '{{ step }}'
          * {{ pipeline_url }}
          ******************************************************************

    - name: set grafana start point
      include_tasks: grafana_start.yml
      when: grafana is defined

    - name: "Wait for pipeline result {{ step }}"
      uri:
        url: "{{ config.api }}/pipelines/{{ trigger_out.json.id }}"
        method: GET
        status_code: 200
        return_content: 'yes'
        headers:
          PRIVATE-TOKEN:
            "{{ config.api_token|default(gitlab.private_token, 'true') }}"
      register: pipeline_out
      retries: "{{ config.timeout }}"
      delay: "{{ gitlab.pipeline.delay }}"
      until: (((pipeline_out.json
             |default({'status':'unknown'})).status
             |default('unknown'))
             not in ['created', 'waiting_for_resource', 'preparing',
             'pending', 'running', 'unknown']) or (
             pipeline_out.status == 401
             )


    - name: Exit -1
      fail:
      when: pipeline_out.json.status not in ['success']

    ##
    # When finished, recover an artifact if requested
    ##
    - name: pull artifacts_src
      when:
        config.pull_artifacts is defined and config.pull_artifacts != None
      block:
        - name: "Get job id for the artifact to get"
          uri:
            url: >-
              {{ config.api
              }}/pipelines/{{ trigger_out.json.id }}/jobs?scope[]=success
            method: GET
            headers:
              PRIVATE-TOKEN:
                "{{ config.api_token|default(gitlab.private_token, 'true') }}"
          register: pipeline_success_jobs

        - name: download job artifact
          uri:
            url: "{{ config.api }}/jobs/{{ job_id[0] }}/artifacts"
            headers:
              PRIVATE-TOKEN:
                "{{ config.api_token|default(gitlab.private_token, 'true') }}"
            dest: "{{ playbook_dir }}/artifacts.zip"
          vars:
            job_id: >-
              {{ pipeline_success_jobs.json |json_query(
              '[?name==`'+ config.pull_artifacts +'`].id') }}

        - name: remove actual artifacts
          file:
            path: "{{ item }}"
            state: absent
          when: item[-1] == '/'
          with_items:
            "{{ vars[lookup( 'env', 'CI_JOB_NAME')].artifacts.paths }}"

        - name: create artifacts folders
          file:
            path: "{{ item }}"
            state: directory
            recurse: true
            mode: 0775
          when: item[-1] == '/'
          with_items:
            "{{ vars[lookup( 'env', 'CI_JOB_NAME')].artifacts.paths }}"

        - name: unarchive artifacts
          unarchive:
            src: "{{ playbook_dir }}/artifacts.zip"
            dest: "{{ playbook_dir }}"
            remote_src: "yes"

        - name: trigger OK healthchecks
          uri:
            url: "{{ base_url }}"
          when: healthchecks_id is defined
          ignore_errors: true

        - name: update grafana stop point
          include_tasks: grafana_stop.yml
          vars:
            result: "{{ pipeline_out.json.status }}"
            text: "<a href={{ pipeline_url }}>{{ step }}</a> succeeded"
          when: grafana is defined and grafana_events is defined

  ##
  # If something failed, print the jobs that failed
  ##
  rescue:
    - name: print last pipeline result for forensic
      debug:
        var: pipeline_out
        verbosity: 3

    - name: update grafana stop point
      include_tasks: grafana_stop.yml
      vars:
        result: "{{ pipeline_out.json.status }}"
        text: "<a href={{ pipeline_url }}>{{ step }}</a> failed"
      when: grafana is defined and grafana_events is defined

    - name: trigger Failed healthcheck
      uri:
        url: "{{ base_url }}/fail"
      when: healthchecks_id is defined

    - name: "Show last pipeline_out value"
      debug:
        msg: "{{ pipeline_out.json | default('No pipeline out') }}"
        verbosity: 3

    - name: "RESCUE - Get jobs list that failed"
      uri:
        url: "{{ config.api }}/pipelines/{{ trigger_out.json.id }}/jobs/"
        method: GET
        status_code: 200
        return_content: 'yes'
        headers:
          PRIVATE-TOKEN:
            "{{ config.api_token|default(gitlab.private_token, 'true') }}"
      register: jobs_list

    - name: RESCUE - filter failed jobs
      set_fact:
        failed_jobs:
          "{{ failed_jobs | default({}) | combine({ item.id:
             {'stage': item.stage,
              'name': item.name,
              'status': item.status,
              'duration': item.duration,
              'url': url
             }})}}"
      vars:
        url: "{{ config.url }}/-/jobs/{{ item.id }}"
      when: item.status not in ['success', 'skipped']
      with_items: "{{ jobs_list.json }}"

    - name: RESCUE - run failed !
      when: true
      fail:
        msg: "{{ msg.split('\n') }}"
      vars:
        msg: |
          ******************************************************************
          * Oh ! NO !!! Pipeling of the project failed !!!
          * -----------------------
          * Step: {{ step }}
          * Project: {{ config.project }}
          * Status: {{ pipeline_out.json.status }}
          * Pipeline: '{{ pipeline_url }}'
          * API pipeline url: '{{ api_pipeline_url }}'
          * Failed jobs:
          {% for job_id, job_status in failed_jobs.items() -%}
          *   - id: {{ job_id }}
          *     name: {{ job_status.stage }}/{{ job_status.name }}
          *     status: {{ job_status.status }}
          *     duration: {{ job_status.duration }}
          *     link: {{ job_status.url }}
          {% endfor %}
          ******************************************************************