From 3d516b67e39d85a3da78cd3db83e79b1ed6b0319 Mon Sep 17 00:00:00 2001 From: Lucile Jeanneret Date: Tue, 17 Dec 2024 14:41:56 +0100 Subject: [PATCH] ci(webap): deploy to web app --- .../main_virtual-teacher-webapp-wus.yml | 65 ------------------- .github/workflows/terraform.yml | 32 ++++++++- deploy/main.tf | 44 ++++++------- 3 files changed, 49 insertions(+), 92 deletions(-) delete mode 100644 .github/workflows/main_virtual-teacher-webapp-wus.yml diff --git a/.github/workflows/main_virtual-teacher-webapp-wus.yml b/.github/workflows/main_virtual-teacher-webapp-wus.yml deleted file mode 100644 index a8d8af3..0000000 --- a/.github/workflows/main_virtual-teacher-webapp-wus.yml +++ /dev/null @@ -1,65 +0,0 @@ -# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy -# More GitHub Actions for Azure: https://github.com/Azure/actions - -name: Build and deploy ASP.Net Core app to Azure Web App - virtual-teacher-webapp-wus - -on: - push: - branches: - - main - workflow_dispatch: - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - - name: Set up .NET Core - uses: actions/setup-dotnet@v4 - with: - dotnet-version: '8.x' - - - name: Build with dotnet - run: dotnet build --configuration Release - - - name: dotnet publish - run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp - - - name: Upload artifact for deployment job - uses: actions/upload-artifact@v4 - with: - name: .net-app - path: ${{env.DOTNET_ROOT}}/myapp - - deploy: - runs-on: ubuntu-latest - needs: build - environment: - name: 'Production' - url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} - permissions: - id-token: write #This is required for requesting the JWT - - steps: - - name: Download artifact from build job - uses: actions/download-artifact@v4 - with: - name: .net-app - - - name: Login to Azure - uses: azure/login@v2 - with: - client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_9A4F0DC400684FCEA1880ADB1EC5FA84 }} - tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_7EC35593D96A4AD7A0270988865F9BC7 }} - subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_60A6E063A30A43089B5A063A1AA66ACE }} - - - name: Deploy to Azure Web App - id: deploy-to-webapp - uses: azure/webapps-deploy@v3 - with: - app-name: 'virtual-teacher-webapp-wus' - slot-name: 'Production' - package: . - \ No newline at end of file diff --git a/.github/workflows/terraform.yml b/.github/workflows/terraform.yml index 2338268..e7448b3 100644 --- a/.github/workflows/terraform.yml +++ b/.github/workflows/terraform.yml @@ -21,6 +21,32 @@ jobs: uses: Azure/get-keyvault-secrets@v1 with: keyvault: "kvvirtualteacher" - secrets: 'subscription-id' # comma separated list of secret keys that need to be fetched from the Key Vault - - run : echo ${{ steps.keyvault.outputs.subscription-id }} - \ No newline at end of file + secrets: 'subscription-id, appService-clientId, appService-clientSecret' # comma separated list of secret keys that need to be fetched from the Key Vault + + - name: Set up Terraform + uses: hashicorp/setup-terraform@v1 + with: + terraform_version: 1.0.11 + + - name: Initialize Terraform + run: terraform init + working-directory: ./deploy + + - name: Apply Terraform + run: terraform apply -auto-approve + working-directory: ./deploy + + - name: Login to Azure + uses: azure/login@v2 + with: + client-id: ${{ steps.keyvault.outputs.appService-clientId }} + tenant-id: ${{ steps.keyvault.outputs.appService-tenantId }} + subscription-id: ${{ steps.keyvault.outputs.subscription-id }} + + - name: Deploy to Azure Web App + id: deploy-to-webapp + uses: azure/webapps-deploy@v3 + with: + app-name: 'virtual-teacher-webapp-wus' + slot-name: 'Production' + package: . \ No newline at end of file diff --git a/deploy/main.tf b/deploy/main.tf index 4ca3b2e..ef19e96 100644 --- a/deploy/main.tf +++ b/deploy/main.tf @@ -8,32 +8,28 @@ provider "azurerm" { data "azurerm_resource_group" "rg" { name = "rg-virtual-teacher-demo-wus" } -# Create the Linux App Service Plan -resource "azurerm_service_plan" "appserviceplan" { - name = "webapp-asp-${random_integer.ri.result}" - location = azurerm_resource_group.rg.location - resource_group_name = azurerm_resource_group.rg.name - os_type = "Linux" - sku_name = "B1" + +# Reference to an existing App Service Plan +data "azurerm_app_service_plan" "existing_plan" { + name = "ASP-rgvirtualteacherdemowus-8ff3" + resource_group_name = data.azurerm_resource_group.rg.name } -# Create the web app, pass in the App Service Plan ID -resource "azurerm_linux_web_app" "webapp" { - name = "webapp-${random_integer.ri.result}" - location = azurerm_resource_group.rg.location - resource_group_name = azurerm_resource_group.rg.name - service_plan_id = azurerm_service_plan.appserviceplan.id - https_only = true - site_config { - minimum_tls_version = "1.2" - } +# Reference to an existing App Service (Web App) +data "azurerm_app_service" "existing_webapp" { + name = "webapp-virtual-teacher" + resource_group_name = data.azurerm_resource_group.rg.name } -# Deploy code from a public GitHub repo -resource "azurerm_app_service_source_control" "sourcecontrol" { - app_id = azurerm_linux_web_app.webapp.id - repo_url = "https://github.com/Azure-Samples/nodejs-docs-hello-world" - branch = "master" - use_manual_integration = true - use_mercurial = false +# Deploy code to the existing Web App +resource "azurerm_app_service_slot" "deployment_slot" { + name = "staging" + location = data.azurerm_resource_group.rg.location + resource_group_name = data.azurerm_resource_group.rg.name + app_service_plan_id = data.azurerm_app_service_plan.existing_plan.id + app_service_name = data.azurerm_app_service.existing_webapp.name + + site_config { + scm_type = "LocalGit" + } } \ No newline at end of file