diff --git a/inventory/host_vars/prod-portal-frontend.yml b/inventory/host_vars/prod-portal-frontend.yml index de15b84..5add2e9 100644 --- a/inventory/host_vars/prod-portal-frontend.yml +++ b/inventory/host_vars/prod-portal-frontend.yml @@ -1,4 +1,5 @@ temp_dir: "C:\\deploy\\vpress\\temp" -iis_site_name: "demo.kinhtedothi.vn" +iis_site_name_1: "demo.kinhtedothi.vn" +iis_site_name_2: "hnt.kinhtedothi.vn" blue_path: "C:\\deploy\\vpress\\portal\\fe\\blue" green_path: "C:\\deploy\\vpress\\portal\\fe\\green" \ No newline at end of file diff --git a/inventory/host_vars/uat-portal-frontend.yml b/inventory/host_vars/uat-portal-frontend.yml index 3e9f9ac..ec65efe 100644 --- a/inventory/host_vars/uat-portal-frontend.yml +++ b/inventory/host_vars/uat-portal-frontend.yml @@ -1,4 +1,5 @@ temp_dir: "C:\\deploy\\vpress\\temp" -iis_site_name: "uat-portal.vpress.vn" +iis_site_name_1: "uat-portal.vpress.vn" +iis_site_name_2: "uat-portal2" blue_path: "C:\\deploy\\vpress\\portal\\fe\\blue" green_path: "C:\\deploy\\vpress\\portal\\fe\\green" \ No newline at end of file diff --git a/playbooks/deploy_fe_portal.yml b/playbooks/deploy_fe_portal.yml new file mode 100644 index 0000000..d00d78a --- /dev/null +++ b/playbooks/deploy_fe_portal.yml @@ -0,0 +1,4 @@ +- name: Deploy Frontend for Project Acp, Portal + hosts: "{{ deploy_env }}-{{project_name}}-frontend" + roles: + - deploy-fe-portal diff --git a/roles/deploy-fe-portal/tasks/cleanup.yml b/roles/deploy-fe-portal/tasks/cleanup.yml new file mode 100644 index 0000000..50350b2 --- /dev/null +++ b/roles/deploy-fe-portal/tasks/cleanup.yml @@ -0,0 +1,5 @@ +# Xóa file tạm +- name: Clean up temporary files + win_file: + path: "{{ temp_dir }}\\{{ artifact_name }}" + state: absent \ No newline at end of file diff --git a/roles/deploy-fe-portal/tasks/deploy.yml b/roles/deploy-fe-portal/tasks/deploy.yml new file mode 100644 index 0000000..c74b0cf --- /dev/null +++ b/roles/deploy-fe-portal/tasks/deploy.yml @@ -0,0 +1,45 @@ +# Tải artifact từ Nexus +- name: Download artifact from Nexus + win_get_url: + url: "{{ nexus_url }}/{{ artifact_name }}" + dest: "{{ temp_dir }}\\{{ artifact_name }}" + validate_certs: no + url_username: "{{ nexus_username }}" + url_password: "{{ nexus_password }}" + register: download_status + vars: + nexus_url: "{{ nexus_url }}" + artifact_name: "{{ artifact_name }}" + nexus_username: "{{ nexus_username }}" + nexus_password: "{{ nexus_password }}" + +# Debug download_status +- name: Debug download_status + debug: + var: download_status + +- name: Check if the download was successful + debug: + msg: "Download successful: {{ download_status.msg }}" + +# Kiểm tra ở trên server đã tồn tại file vùa tải xuống không +- name: Check if the artifact file exists + stat: + path: "{{ temp_dir }}/{{ artifact_name }}" + register: file_status + changed_when: false + failed_when: false + +# Debug file_status +- name: Debug file_status + debug: + var: file_status + + +# Giải nén artifact vào thư mục target +- name: Deploy to target environment + win_unzip: + src: "{{ temp_dir }}\\{{ artifact_name }}" + dest: "{{ target_path }}" + overwrite: yes + diff --git a/roles/deploy-fe-portal/tasks/main.yml b/roles/deploy-fe-portal/tasks/main.yml new file mode 100644 index 0000000..1570bf6 --- /dev/null +++ b/roles/deploy-fe-portal/tasks/main.yml @@ -0,0 +1,13 @@ +- name: Setup environment + import_tasks: setup.yml + +- name: Deploy application + import_tasks: deploy.yml + +- name: Clean up temporary files + import_tasks: cleanup.yml + +- name: Switch + import_tasks: switch.yml + + diff --git a/roles/deploy-fe-portal/tasks/setup.yml b/roles/deploy-fe-portal/tasks/setup.yml new file mode 100644 index 0000000..dde852b --- /dev/null +++ b/roles/deploy-fe-portal/tasks/setup.yml @@ -0,0 +1,51 @@ +#Debug +- name: Debug iis_site_name_1 + debug: + var: iis_site_name_1 + +- name: Debug iis_site_name_2 + debug: + var: iis_site_name_2 + +- name: Debug blue_path + debug: + var: blue_path + +# Kiểm tra các thông số Nexus +- name: Debug Nexus URL + debug: + var: nexus_url + +- name: Debug artifact name + debug: + var: artifact_name + +#Lấy đường dẫn vật lý của iis site +- name: Get the physical path of the current IIS site + win_shell: | + Import-Module WebAdministration + $site = Get-Website -Name "{{ iis_site_name_1 }}" + $site.PhysicalPath + register: active_path +- debug: + var: active_path + +- name: Handle error if active_path is null + fail: + msg: "The IIS site active path could not be determined." + when: active_path.stdout is not defined or active_path.stdout == '' + + +# Chuẩn hóa giá trị của active_path +- name: Normalize active_path + set_fact: + active_path: "{{ active_path.stdout | trim }}" +- debug: + var: active_path + +# Chọn môi trường Blue hoặc Green để deploy +- name: Set target deployment environment + set_fact: + target_path: "{{ green_path if active_path == blue_path else blue_path }}" #lấy ra đường dẫn thư mục cần deploy +- debug: + var: target_path \ No newline at end of file diff --git a/roles/deploy-fe-portal/tasks/switch.yml b/roles/deploy-fe-portal/tasks/switch.yml new file mode 100644 index 0000000..d55e887 --- /dev/null +++ b/roles/deploy-fe-portal/tasks/switch.yml @@ -0,0 +1,15 @@ +- name: Update site 1 IIS web application + win_iis_website: + name: "{{ iis_site_name_1 }}" + physical_path: "{{ target_path }}" + +- name: Update site 2 IIS web application + win_iis_website: + name: "{{ iis_site_name_2 }}" + physical_path: "{{ target_path }}" + +# - name: Restart IIS site to apply changes +# win_iis_website: +# name: "{{ iis_site_name }}" +# state: restarted +