---
# Creates the docker image

- name: Check if the image already exists on the system
  community.docker.docker_image_info:
    name: "{{ docker_image_name }}:{{ docker_image_tag }}"
  register: result_check_image

- name: Check which repository to be cloned.
  ansible.builtin.debug:
    msg: >
      {% if result_check_image.images | length > 0 %}
        "Image for '{{ docker_image_name }}' already exixts on the target..."
      {% else %}
        "Image for '{{ docker_image_name }}' DONT exixts on the target, Building the image..."
      {% endif %}

- name: Build the docker image
  ansible.builtin.command:
    cmd: docker build . -t "{{ docker_image_name }}":"{{ docker_image_tag }}" -f "{{ docker_file_path }}"
  args:
    # chdir: "{{ c3infodesk_deployment_dest_folder }}"
    chdir: "{{ c3infodesk_deployment_repository_dest_folder }}"
  register: build_img_result
  when: result_check_image.images | length <= 0
  changed_when: build_img_result.rc != 0

- name: Check if image was created.
  ansible.builtin.debug:
    msg: >
      {% if build_img_result is defined %}
        "Building image for '{{ docker_image_name }}' was created..."
      {% else %}
        "Building image for '{{ docker_image_name }}' was skipped..."
      {% endif %}
