From 965d8b431c932f7ea41961cdb0a95ae3d18a170b Mon Sep 17 00:00:00 2001 From: thienqb123456 Date: Mon, 3 Mar 2025 14:05:33 +0700 Subject: [PATCH] =?UTF-8?q?=C4=91=E1=BB=95i=20t=C3=AAn=20site=20iis=20n?= =?UTF-8?q?=C3=AAn=20ph=E1=BA=A3i=20s=E1=BB=ADa=20cho=20n=C3=B3=20=C4=83n?= =?UTF-8?q?=20theo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../host_vars/beta-portal-hnt-backend.yml | 7 ++ .../host_vars/beta-portal-hnt-frontend.yml | 2 +- .../host_vars/beta-portal-ktdt-backend.yml | 6 ++ playbooks/deploy_be_portal.yml | 10 +++ roles/deploy-be-portal/tasks/cleanup.yml | 5 ++ roles/deploy-be-portal/tasks/deploy.yml | 45 ++++++++++++ roles/deploy-be-portal/tasks/main.yml | 13 ++++ roles/deploy-be-portal/tasks/setup.yml | 69 +++++++++++++++++++ roles/deploy-be-portal/tasks/switch.yml | 10 +++ roles/deploy/tasks/setup.yml | 2 - 10 files changed, 166 insertions(+), 3 deletions(-) create mode 100644 inventory/host_vars/beta-portal-hnt-backend.yml create mode 100644 inventory/host_vars/beta-portal-ktdt-backend.yml create mode 100644 playbooks/deploy_be_portal.yml create mode 100644 roles/deploy-be-portal/tasks/cleanup.yml create mode 100644 roles/deploy-be-portal/tasks/deploy.yml create mode 100644 roles/deploy-be-portal/tasks/main.yml create mode 100644 roles/deploy-be-portal/tasks/setup.yml create mode 100644 roles/deploy-be-portal/tasks/switch.yml diff --git a/inventory/host_vars/beta-portal-hnt-backend.yml b/inventory/host_vars/beta-portal-hnt-backend.yml new file mode 100644 index 0000000..22030ef --- /dev/null +++ b/inventory/host_vars/beta-portal-hnt-backend.yml @@ -0,0 +1,7 @@ +temp_dir: "C:\\deploy\\vpress\\temp" +iis_site_name: "portal-api.hanoitimes.vn" +blue_path: "C:\\deploy\\portal\\be\\hnt\\blue" +green_path: "C:\\deploy\\portal\\be\\hnt\\green" + +aspnetcore_environtment: "Hnt_Beta" +iis_app_pool: "portal-api.hanoitimes.vn" \ No newline at end of file diff --git a/inventory/host_vars/beta-portal-hnt-frontend.yml b/inventory/host_vars/beta-portal-hnt-frontend.yml index c82e871..c8d73c3 100644 --- a/inventory/host_vars/beta-portal-hnt-frontend.yml +++ b/inventory/host_vars/beta-portal-hnt-frontend.yml @@ -1,4 +1,4 @@ temp_dir: "C:\\deploy\\vpress\\temp" -iis_site_name: "beta.hanoitimes.vn" +iis_site_name: "hanoitimes.vn" blue_path: "C:\\deploy\\portal\\fe\\hnt\\blue" green_path: "C:\\deploy\\portal\\fe\\hnt\\green" \ No newline at end of file diff --git a/inventory/host_vars/beta-portal-ktdt-backend.yml b/inventory/host_vars/beta-portal-ktdt-backend.yml new file mode 100644 index 0000000..5c96051 --- /dev/null +++ b/inventory/host_vars/beta-portal-ktdt-backend.yml @@ -0,0 +1,6 @@ +temp_dir: "C:\\deploy\\vpress\\temp" +iis_site_name: "portal-api.kinhtedothi.vn" +blue_path: "C:\\deploy\\portal\\be\\ktdt\\blue" +green_path: "C:\\deploy\\portal\\be\\ktdt\\green" + +aspnetcore_environtment: "Ktdt_Beta" \ No newline at end of file diff --git a/playbooks/deploy_be_portal.yml b/playbooks/deploy_be_portal.yml new file mode 100644 index 0000000..80d7d62 --- /dev/null +++ b/playbooks/deploy_be_portal.yml @@ -0,0 +1,10 @@ +- name: Deploy Backend for Project Acp, Portal + hosts: "{{ deploy_env }}-{{project_name}}-backend" + vars: + portal_name: "{{ portal_name }}" + pre_tasks: + - name: Include portal-specific variables + include_vars: "../inventory/host_vars/{{ deploy_env }}-{{ project_name }}-{{ portal_name }}-backend.yml" + roles: + - deploy-be-portal + \ No newline at end of file diff --git a/roles/deploy-be-portal/tasks/cleanup.yml b/roles/deploy-be-portal/tasks/cleanup.yml new file mode 100644 index 0000000..50350b2 --- /dev/null +++ b/roles/deploy-be-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-be-portal/tasks/deploy.yml b/roles/deploy-be-portal/tasks/deploy.yml new file mode 100644 index 0000000..c74b0cf --- /dev/null +++ b/roles/deploy-be-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-be-portal/tasks/main.yml b/roles/deploy-be-portal/tasks/main.yml new file mode 100644 index 0000000..1570bf6 --- /dev/null +++ b/roles/deploy-be-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-be-portal/tasks/setup.yml b/roles/deploy-be-portal/tasks/setup.yml new file mode 100644 index 0000000..d6fd9de --- /dev/null +++ b/roles/deploy-be-portal/tasks/setup.yml @@ -0,0 +1,69 @@ +#Debug +- name: Debug iis_site_name + debug: + var: iis_site_name + +- name: Debug iis_app_pool + debug: + var: iis_app_pool + +- name: Debug aspnetcore_environtment + debug: + var: aspnetcore_environtment + +- 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 + +- name: Set environment variable for IIS App Pool + hosts: windows_servers + tasks: + - name: Thiết lập ASPNETCORE_ENVIRONMENT cho App Pool + win_shell: | + $appPoolName = "{{ iis_app_pool }}" + $envVarName = "ASPNETCORE_ENVIRONMENT" + $envVarValue = "{{ aspnetcore_environtment }}" + $appPool = Get-Item "IIS:\AppPools\$appPoolName" + $appPool.EnvironmentVariables[$envVarName] = $envVarValue + $appPool | Set-Item + args: + executable: powershell.exe + +#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}}" + $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-be-portal/tasks/switch.yml b/roles/deploy-be-portal/tasks/switch.yml new file mode 100644 index 0000000..b66d2b3 --- /dev/null +++ b/roles/deploy-be-portal/tasks/switch.yml @@ -0,0 +1,10 @@ +- name: Update site IIS web application + win_iis_website: + name: "{{ iis_site_name }}" + physical_path: "{{ target_path }}" + +# - name: Restart IIS site to apply changes +# win_iis_website: +# name: "{{ iis_site_name }}" +# state: restarted + diff --git a/roles/deploy/tasks/setup.yml b/roles/deploy/tasks/setup.yml index 01e7d93..7895f6c 100644 --- a/roles/deploy/tasks/setup.yml +++ b/roles/deploy/tasks/setup.yml @@ -16,8 +16,6 @@ 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: |