From 9be0f4f3463de4d501b4d6743e9ced7b8f1b6098 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C4=99czek?= Date: Sat, 11 Nov 2023 21:14:21 +0100 Subject: [PATCH 1/2] Deploy infrastructure --- .../deploy-to-azure-with-cosmos-db.yml | 4 +- infrastructure/azure/main.bicep | 9 ++++ infrastructure/azure/resource-group.bicep | 48 +++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 infrastructure/azure/resource-group.bicep diff --git a/.github/workflows/deploy-to-azure-with-cosmos-db.yml b/.github/workflows/deploy-to-azure-with-cosmos-db.yml index a56d789..2af506e 100644 --- a/.github/workflows/deploy-to-azure-with-cosmos-db.yml +++ b/.github/workflows/deploy-to-azure-with-cosmos-db.yml @@ -10,7 +10,7 @@ permissions: contents: read jobs: - deploy-infrastructe: + deploy-infrastructure: runs-on: ubuntu-latest environment: azure steps: @@ -38,7 +38,7 @@ jobs: az cache purge az account clear build-and-deploy-webapp: - needs: [deploy-infrastructe] + needs: [deploy-infrastructure] runs-on: ubuntu-latest environment: azure steps: diff --git a/infrastructure/azure/main.bicep b/infrastructure/azure/main.bicep index 2967090..13f85d5 100644 --- a/infrastructure/azure/main.bicep +++ b/infrastructure/azure/main.bicep @@ -7,3 +7,12 @@ resource resourceGroupResource 'Microsoft.Resources/resourceGroups@2022-09-01' = name: 'rg-${projectName}' location: projectLocation } + +module resourceGroupModule 'resource-group.bicep' = { + name: '${projectName}-rg' + scope: resourceGroup(resourceGroupResource.name) + params: { + projectName: projectName + projectLocation: resourceGroupResource.location + } +} diff --git a/infrastructure/azure/resource-group.bicep b/infrastructure/azure/resource-group.bicep new file mode 100644 index 0000000..711c78b --- /dev/null +++ b/infrastructure/azure/resource-group.bicep @@ -0,0 +1,48 @@ +targetScope = 'resourceGroup' + +param projectName string +param projectLocation string = resourceGroup().location + +var projectAppServiceName = 'app-${length(projectName) > 56 ? substring(projectName, 0, 56) : projectName}' +var projectAppServicePlanName = 'asp-${length(projectName) > 36 ? substring(projectName, 0, 36) : projectName}' +var projectDocumentDBAccountName = 'cosmos-${uniqueString(resourceGroup().id)}' + +resource projectDocumentDBAccount 'Microsoft.DocumentDB/databaseAccounts@2023-04-15' = { + name: projectDocumentDBAccountName + location: projectLocation + properties: { + enableFreeTier: true + databaseAccountOfferType: 'Standard' + consistencyPolicy: { + defaultConsistencyLevel: 'Session' + } + locations: [ + { + locationName: projectLocation + } + ] + } +} + +resource projectAppServicePlan 'Microsoft.Web/serverfarms@2022-09-01' = { + name: projectAppServicePlanName + location: projectLocation + sku: { + name: 'F1' + } + kind: 'linux' + properties: { + reserved: true + } +} + +resource projectAppService 'Microsoft.Web/sites@2022-09-01' = { + name: projectAppServiceName + location: projectLocation + properties: { + serverFarmId: projectAppServicePlan.id + siteConfig: { + linuxFxVersion: 'DOTNETCORE|6.0' + } + } +} From 50b693ba51258a847b8e728ad5f90e5d6abd2090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C4=99czek?= Date: Sat, 11 Nov 2023 22:49:48 +0100 Subject: [PATCH 2/2] Deploy application --- .../deploy-to-azure-with-cosmos-db.yml | 32 ++++++++++++++- README.md | 2 +- infrastructure/azure/main.bicep | 10 +++++ infrastructure/azure/resource-group.bicep | 41 +++++++++++++++++++ 4 files changed, 82 insertions(+), 3 deletions(-) diff --git a/.github/workflows/deploy-to-azure-with-cosmos-db.yml b/.github/workflows/deploy-to-azure-with-cosmos-db.yml index 2af506e..95a5cbb 100644 --- a/.github/workflows/deploy-to-azure-with-cosmos-db.yml +++ b/.github/workflows/deploy-to-azure-with-cosmos-db.yml @@ -3,7 +3,9 @@ on: workflow_dispatch env: PROJECT_NAME: demo-aspnetcore-pushnotifications + PROJECT_PATH: './Demo.AspNetCore.PushNotifications' PROJECT_LOCATION: westeurope + PROJECT_APP_SERVICE_NAME: app-demo-aspnetcore-pushnotifications permissions: id-token: write @@ -30,7 +32,7 @@ jobs: subscriptionId: ${{ secrets.AZURE_SUBSCRIPTION }} deploymentName: ${{ env.PROJECT_NAME }} template: '"./infrastructure/azure/main.bicep"' - parameters: 'projectName=${{ env.PROJECT_NAME }} projectLocation=${{ env.PROJECT_LOCATION }}' + parameters: 'projectName=${{ env.PROJECT_NAME }} projectLocation=${{ env.PROJECT_LOCATION }} pushServiceClientSubject=${{ secrets.PUSHSERVICECLIENT_SUBJECT }} pushServiceClientPublicKey=${{ secrets.PUSHSERVICECLIENT_PUBLICKEY }} pushServiceClientPrivateKey=${{ secrets.PUSHSERVICECLIENT_PRIVATEKEY }}' failOnStdErr: false - name: Azure Logout run: | @@ -43,4 +45,30 @@ jobs: environment: azure steps: - name: Checkout - uses: actions/checkout@v3 \ No newline at end of file + uses: actions/checkout@v3 + - name: Setup .NET 6.0 SDK + uses: actions/setup-dotnet@v3.2.0 + with: + dotnet-version: '6.0.x' + - name: Restore + run: dotnet restore "${PROJECT_PATH}" + - name: Build + run: dotnet build "${PROJECT_PATH}" --configuration Release --no-restore + - name: Publish + run: dotnet publish "${PROJECT_PATH}" --configuration Release --no-build --output ${DOTNET_ROOT}/${PROJECT_APP_SERVICE_NAME} + - name: Azure Login + uses: azure/login@v1 + with: + client-id: ${{ secrets.AZURE_CLIENT_ID }} + tenant-id: ${{ secrets.AZURE_TENANT_ID }} + subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} + - name: Deploy + uses: azure/webapps-deploy@v3 + with: + app-name: ${{ env.PROJECT_APP_SERVICE_NAME }} + package: ${{ env.DOTNET_ROOT }}/${{ env.PROJECT_APP_SERVICE_NAME }} + - name: Azure Logout + run: | + az logout + az cache purge + az account clear \ No newline at end of file diff --git a/README.md b/README.md index d0a7e08..d7719f0 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ In order to run the project, some configuration is required. Inside *appsettings } ``` -Those keys can be acquired with help of online generators (https://vapidkeys.com/). +Those keys can be acquired with help of online generators (https://vapidkeys.com/, https://www.attheminute.com/vapid-key-generator). ## Donating diff --git a/infrastructure/azure/main.bicep b/infrastructure/azure/main.bicep index 13f85d5..7357993 100644 --- a/infrastructure/azure/main.bicep +++ b/infrastructure/azure/main.bicep @@ -3,6 +3,13 @@ targetScope = 'subscription' param projectName string param projectLocation string +@secure() +param pushServiceClientSubject string +@secure() +param pushServiceClientPublicKey string +@secure() +param pushServiceClientPrivateKey string + resource resourceGroupResource 'Microsoft.Resources/resourceGroups@2022-09-01' = { name: 'rg-${projectName}' location: projectLocation @@ -14,5 +21,8 @@ module resourceGroupModule 'resource-group.bicep' = { params: { projectName: projectName projectLocation: resourceGroupResource.location + pushServiceClientSubject: pushServiceClientSubject + pushServiceClientPublicKey: pushServiceClientPublicKey + pushServiceClientPrivateKey: pushServiceClientPrivateKey } } diff --git a/infrastructure/azure/resource-group.bicep b/infrastructure/azure/resource-group.bicep index 711c78b..c9fb846 100644 --- a/infrastructure/azure/resource-group.bicep +++ b/infrastructure/azure/resource-group.bicep @@ -3,6 +3,13 @@ targetScope = 'resourceGroup' param projectName string param projectLocation string = resourceGroup().location +@secure() +param pushServiceClientSubject string +@secure() +param pushServiceClientPublicKey string +@secure() +param pushServiceClientPrivateKey string + var projectAppServiceName = 'app-${length(projectName) > 56 ? substring(projectName, 0, 56) : projectName}' var projectAppServicePlanName = 'asp-${length(projectName) > 36 ? substring(projectName, 0, 36) : projectName}' var projectDocumentDBAccountName = 'cosmos-${uniqueString(resourceGroup().id)}' @@ -43,6 +50,40 @@ resource projectAppService 'Microsoft.Web/sites@2022-09-01' = { serverFarmId: projectAppServicePlan.id siteConfig: { linuxFxVersion: 'DOTNETCORE|6.0' + appSettings: [ + { + name: 'PushSubscriptionStoreType' + value: 'CosmosDB' + } + { + name: 'CosmosDB__AccountEndpoint' + value: projectDocumentDBAccount.properties.documentEndpoint + } + { + name: 'CosmosDB__AuthKey' + value: projectDocumentDBAccount.listKeys().primaryMasterKey + } + { + name: 'CosmosDB__DatabaseName' + value: 'PushNotifications' + } + { + name: 'CosmosDB__ContainerName' + value: 'Subscriptions' + } + { + name: 'PushServiceClient__Subject' + value: pushServiceClientSubject + } + { + name: 'PushServiceClient__PublicKey' + value: pushServiceClientPublicKey + } + { + name: 'PushServiceClient__PrivateKey' + value: pushServiceClientPrivateKey + } + ] } } }