diff --git a/playbooks/deploy_be_resource.yml b/playbooks/deploy_be_resource.yml new file mode 100644 index 0000000..ee35f98 --- /dev/null +++ b/playbooks/deploy_be_resource.yml @@ -0,0 +1,10 @@ +- name: Deploy Backend for Project 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-v2/tasks/main.yml b/roles/deploy-be-portal-v2/tasks/main.yml new file mode 100644 index 0000000..9cc617e --- /dev/null +++ b/roles/deploy-be-portal-v2/tasks/main.yml @@ -0,0 +1,35 @@ +tasks: + - name: get_site_current_active_path.yml + import_role: + name: iis_utils + tasks_from: get_site_current_active_path.yml + + - name: set_target_path + import_role: + name: iis_utils + tasks_from: set_target_path.yml + + - name: artifact_download + import_role: + name: iis_utils + tasks_from: artifact_download.yml + + - name: artifact_deploy + import_role: + name: iis_utils + tasks_from: artifact_deploy.yml + + - name: update_iis_physical_path + import_role: + name: iis_utils + tasks_from: update_iis_physical_path.yml + + - name: set_site_aspnetcore_environment + import_role: + name: iis_utils + tasks_from: set_site_aspnetcore_environment.yml + + + + + diff --git a/roles/iis_utils/tasks/artifact_deploy.yml b/roles/iis_utils/tasks/artifact_deploy.yml new file mode 100644 index 0000000..f4ebd20 --- /dev/null +++ b/roles/iis_utils/tasks/artifact_deploy.yml @@ -0,0 +1,14 @@ +- name: Validate variables + assert: + that: + - artifact_name is defined and artifact_name | length > 0 + - target_path is defined and target_path | length > 0 + - temp_dir is defined and temp_dir | length > 0 + fail_msg: "Thiếu hoặc rỗng biến cần thiết để deploy artifact!" + +# 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 \ No newline at end of file diff --git a/roles/iis_utils/tasks/artifact_download.yml b/roles/iis_utils/tasks/artifact_download.yml new file mode 100644 index 0000000..6f17af0 --- /dev/null +++ b/roles/iis_utils/tasks/artifact_download.yml @@ -0,0 +1,48 @@ +- name: Validate variables + assert: + that: + - nexus_url is defined and nexus_url | length > 0 + - temp_dir is defined and temp_dir | length > 0 + - artifact_name is defined and artifact_name | length > 0 + - nexus_username is defined and nexus_username | length > 0 + - nexus_password is defined and nexus_password | length > 0 + fail_msg: "Thiếu hoặc rỗng biến cần thiết để download artifact!" + +# Tải artifact từ Nexus Repository +- 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 + +# Lỗi khi tải artifact từ nexus thất bại +- name: Fail if download artifact from Nexus Repository fail + fail: + msg: "Download artifact from nexus fail" + when: download_status.status_code != 200 + +# 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 \ No newline at end of file diff --git a/roles/iis_utils/tasks/cleanup_temp_artifact.yml b/roles/iis_utils/tasks/cleanup_temp_artifact.yml new file mode 100644 index 0000000..ea33c6a --- /dev/null +++ b/roles/iis_utils/tasks/cleanup_temp_artifact.yml @@ -0,0 +1,12 @@ +- name: Validate variables + assert: + that: + - temp_dir is defined and temp_dir | length > 0 + - artifact_name is defined and artifact_name | length > 0 + fail_msg: "Thiếu hoặc rỗng biến cần thiết để cleanup_temp_artifact!" + +# 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/iis_utils/tasks/get_site_current_active_path.yml b/roles/iis_utils/tasks/get_site_current_active_path.yml new file mode 100644 index 0000000..66ba430 --- /dev/null +++ b/roles/iis_utils/tasks/get_site_current_active_path.yml @@ -0,0 +1,26 @@ +- name: Validate variables + assert: + that: + - iis_site_name is defined and iis_site_name | length > 0 + fail_msg: "Thiếu hoặc rỗng biến cần thiết để get_site_current_active_path!" + +#Lấy đường dẫn vật lý của iis site/kiểm tra sự tồn tại của site +- name: Get the physical path of the current IIS site + win_shell: | + Import-Module WebAdministration + $site = Get-Website -Name "{{ iis_site_name}}" + if ($site) { + Write-Output $site.PhysicalPath + } else { + Write-Output "NOT_FOUND" + } + register: site_path + +- name: Set fact for site path + set_fact: + active_path: "{{ site_path.stdout_lines[0] }}" + +- name: Fail if site does not exist + fail: + msg: "IIS site {{ iis_site_name }} not found!" + when: active_path == "NOT_FOUND" \ No newline at end of file diff --git a/roles/iis_utils/tasks/restart_iis_site.yml b/roles/iis_utils/tasks/restart_iis_site.yml new file mode 100644 index 0000000..23411fc --- /dev/null +++ b/roles/iis_utils/tasks/restart_iis_site.yml @@ -0,0 +1,11 @@ +- name: Validate variables + assert: + that: + - iis_site_name is defined and iis_site_name | length > 0 + fail_msg: "Thiếu hoặc rỗng biến cần thiết để restart_iis_site.yml!" + +# Restart IIS site +- name: Restart IIS site + win_iis_website: + name: "{{ iis_site_name }}" + state: restarted \ No newline at end of file diff --git a/roles/iis_utils/tasks/set_site_aspnetcore_environment.yml b/roles/iis_utils/tasks/set_site_aspnetcore_environment.yml new file mode 100644 index 0000000..1254888 --- /dev/null +++ b/roles/iis_utils/tasks/set_site_aspnetcore_environment.yml @@ -0,0 +1,15 @@ +- name: Validate variables + assert: + that: + - iis_site_name is defined and iis_site_name | length > 0 + - aspnetcore_environment is defined and aspnetcore_environment | length > 0 + fail_msg: "Thiếu hoặc rỗng biến cần thiết để set_site_aspnetcore_environment!" + +# Set ASPNETCORE_ENVIRONMENT cho site IIS +- name: Set ASPNETCORE_ENVIRONMENT for IIS site + win_shell: | + Import-Module WebAdministration + Add-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST/{{ iis_site_name }}" ` + -filter "system.webServer/aspNetCore/environmentVariables" ` + -name "." ` + -value @{name="ASPNETCORE_ENVIRONMENT"; value="{{ aspnetcore_environment }}"} \ No newline at end of file diff --git a/roles/iis_utils/tasks/set_target_path.yml b/roles/iis_utils/tasks/set_target_path.yml new file mode 100644 index 0000000..53f8cbf --- /dev/null +++ b/roles/iis_utils/tasks/set_target_path.yml @@ -0,0 +1,16 @@ +- name: Validate variables + assert: + that: + - green_path is defined and green_path | length > 0 + - blue_path is defined and blue_path | length > 0 + - active_path is defined and active_path | length > 0 + fail_msg: "Thiếu hoặc rỗng biến cần thiết để set_target_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/iis_utils/tasks/update_iis_physical_path.yml b/roles/iis_utils/tasks/update_iis_physical_path.yml new file mode 100644 index 0000000..79bb8d5 --- /dev/null +++ b/roles/iis_utils/tasks/update_iis_physical_path.yml @@ -0,0 +1,12 @@ +- name: Validate variables + assert: + that: + - iis_site_name is defined and iis_site_name | length > 0 + - target_path is defined and target_path | length > 0 + fail_msg: "Thiếu hoặc rỗng biến cần thiết để set_target_path!" + +# Cập nhật PhysicalPath mới cho site IIS (green/blue) +- name: Update site IIS web application + win_iis_website: + name: "{{ iis_site_name }}" + physical_path: "{{ target_path }}" \ No newline at end of file