diff --git a/deployment/ansible/playbooks/deploy-image.yml b/deployment/ansible/playbooks/deploy-image.yml index f5c1eaad..6218fdd4 100644 --- a/deployment/ansible/playbooks/deploy-image.yml +++ b/deployment/ansible/playbooks/deploy-image.yml @@ -5,13 +5,8 @@ become: no vars: - # Determine stack path based on environment - application_stack_dest: >- - {%- if deployment_environment == 'staging' -%} - {{ staging_stack_path | default(stacks_base_path + '/staging') }} - {%- else -%} - {{ app_stack_path | default(stacks_base_path + '/production') }} - {%- endif -%} + # Application code directory (where docker-compose files are located) + application_code_dest: "/home/deploy/michaelschiemer/current" application_compose_suffix: >- {%- if deployment_environment == 'staging' -%} staging.yml @@ -21,13 +16,19 @@ # Image to deploy (can be overridden via -e image_tag=...) image_tag: "{{ image_tag | default('latest') }}" docker_registry: "{{ docker_registry | default('registry.michaelschiemer.de') }}" - app_name: "{{ app_name | default('framework') }}" - # Full image URL - deploy_image: "{{ docker_registry }}/{{ app_name }}:{{ image_tag }}" + app_name_default: "framework" # Deployment environment (staging or production) deployment_environment: "{{ deployment_environment | default('production') }}" tasks: + - name: Set app_name from provided value or default + ansible.builtin.set_fact: + app_name: "{{ app_name if (app_name is defined and app_name != '') else app_name_default }}" + + - name: Set deploy_image from registry, app_name and tag + ansible.builtin.set_fact: + deploy_image: "{{ docker_registry }}/{{ app_name }}:{{ image_tag }}" + - name: Determine Docker registry password from vault or extra vars ansible.builtin.set_fact: registry_password: >- @@ -42,10 +43,11 @@ - name: Check if registry is accessible ansible.builtin.uri: - url: "http://{{ docker_registry }}/v2/" + url: "https://{{ docker_registry }}/v2/" method: GET status_code: [200, 401] timeout: 5 + validate_certs: no register: registry_check ignore_errors: yes delegate_to: "{{ inventory_hostname }}" @@ -79,7 +81,7 @@ - name: Update docker-compose file with new image tag ansible.builtin.replace: - path: "{{ application_stack_dest }}/docker-compose.{{ application_compose_suffix }}" + path: "{{ application_code_dest }}/docker-compose.{{ application_compose_suffix }}" regexp: '^(\s+image:\s+)({{ docker_registry }}/{{ app_name }}:)(.*)$' replace: '\1\2{{ image_tag }}' register: compose_update_result @@ -88,7 +90,7 @@ - name: Update docker-compose file with new image (alternative pattern) ansible.builtin.replace: - path: "{{ application_stack_dest }}/docker-compose.{{ application_compose_suffix }}" + path: "{{ application_code_dest }}/docker-compose.{{ application_compose_suffix }}" regexp: 'image:\s+{{ docker_registry }}/{{ app_name }}:.*' replace: 'image: {{ deploy_image }}' register: compose_update_alt @@ -107,7 +109,7 @@ - name: Deploy application stack with new image shell: | - cd {{ application_stack_dest }} + cd {{ application_code_dest }} docker compose -f docker-compose.base.yml -f docker-compose.{{ application_compose_suffix }} up -d --pull missing --force-recreate --remove-orphans register: compose_deploy_result changed_when: true @@ -118,7 +120,7 @@ - name: Check container status shell: | - cd {{ application_stack_dest }} + cd {{ application_code_dest }} docker compose -f docker-compose.base.yml -f docker-compose.{{ application_compose_suffix }} ps register: container_status changed_when: false @@ -132,7 +134,7 @@ Image: {{ deploy_image }} Tag: {{ image_tag }} Environment: {{ deployment_environment }} - Stack: {{ application_stack_dest }} + Stack: {{ application_code_dest }} Status: SUCCESS ========================================