From 0e5db6a7f72295fa99bc1b52f2d1a96f469d9072 Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Fri, 14 Oct 2022 11:46:00 +0100 Subject: [PATCH] feat: linked backends --- .devcontainer/Dockerfile | 10 ++-- .devcontainer/base.Dockerfile | 17 ++++++ .devcontainer/devcontainer.json | 59 ++++++++++--------- .github/workflows/build-and-deploy.yml | 18 ++++-- infra/function.bicep | 80 ++++++++++++++++++++++++++ infra/main.bicep | 33 +++++++++-- 6 files changed, 176 insertions(+), 41 deletions(-) create mode 100644 .devcontainer/base.Dockerfile create mode 100644 infra/function.bicep diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 4f4a9d0c61d..d59927b1f30 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,7 +1,5 @@ -# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.209.6/containers/javascript-node/.devcontainer/base.Dockerfile - -# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 16, 14, 12, 16-bullseye, 14-bullseye, 12-bullseye, 16-buster, 14-buster, 12-buster -ARG VARIANT="16-bullseye" +# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 18, 16, 14, 18-bullseye, 16-bullseye, 14-bullseye, 18-buster, 16-buster, 14-buster +ARG VARIANT=16-bullseye FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:0-${VARIANT} # [Optional] Uncomment this section to install additional OS packages. @@ -12,5 +10,5 @@ FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:0-${VARIANT} # ARG EXTRA_NODE_VERSION=10 # RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}" -# [Optional] Uncomment if you want to install more global node modules -# RUN su node -c "npm install -g " +# [Optional] Uncomment if you want to install more global node packages +# RUN su node -c "npm install -g " diff --git a/.devcontainer/base.Dockerfile b/.devcontainer/base.Dockerfile new file mode 100644 index 00000000000..35b6654f80a --- /dev/null +++ b/.devcontainer/base.Dockerfile @@ -0,0 +1,17 @@ +# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 18, 16, 14, 18-bullseye, 16-bullseye, 14-bullseye, 18-buster, 16-buster, 14-buster +ARG VARIANT=16-bullseye +FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${VARIANT} + +# Install tslint, typescript. eslint is installed by javascript image +ARG NODE_MODULES="tslint-to-eslint-config typescript" +COPY library-scripts/meta.env /usr/local/etc/vscode-dev-containers +RUN su node -c "umask 0002 && npm install -g ${NODE_MODULES}" \ + && npm cache clean --force > /dev/null 2>&1 + +# [Optional] Uncomment this section to install additional OS packages. +# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ +# && apt-get -y install --no-install-recommends + +# [Optional] Uncomment if you want to install an additional version of node using nvm +# ARG EXTRA_NODE_VERSION=10 +# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}" diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 152eb7d05bf..bb448a889fb 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,34 +1,39 @@ // For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: -// https://github.com/microsoft/vscode-dev-containers/tree/v0.209.6/containers/javascript-node +// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/typescript-node { - "name": "Node.js", - "build": { - "dockerfile": "Dockerfile", - // Update 'VARIANT' to pick a Node version: 16, 14, 12. - // Append -bullseye or -buster to pin to an OS version. - // Use -bullseye variants on local arm64/Apple Silicon. - "args": { "VARIANT": "16" } - }, + "name": "Node.js & TypeScript", + "build": { + "dockerfile": "Dockerfile", + // Update 'VARIANT' to pick a Node version: 18, 16, 14. + // Append -bullseye or -buster to pin to an OS version. + // Use -bullseye variants on local on arm64/Apple Silicon. + "args": { + "VARIANT": "16-bullseye" + } + }, - // Set *default* container specific settings.json values on container create. - "settings": {}, + // Configure tool-specific properties. + "customizations": { + // Configure properties specific to VS Code. + "vscode": { + // Add the IDs of extensions you want installed when the container is created. + "extensions": [ + "dbaeumer.vscode-eslint", + "esbenp.prettier-vscode", + "ms-azuretools.vscode-bicep" + ] + } + }, - // Add the IDs of extensions you want installed when the container is created. - "extensions": [ - "dbaeumer.vscode-eslint", - "esbenp.prettier-vscode", - "ms-azuretools.vscode-bicep" - ], + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], - // Use 'forwardPorts' to make a list of ports inside the container available locally. - // "forwardPorts": [], + // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "yarn install", - // Use 'postCreateCommand' to run commands after the container is created. - "postCreateCommand": "az bicep install", - - // Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. - "remoteUser": "node", - "features": { - "azure-cli": "latest" - } + // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. + "remoteUser": "node", + "features": { + "azure-cli": "latest" + } } diff --git a/.github/workflows/build-and-deploy.yml b/.github/workflows/build-and-deploy.yml index a1d39662fd7..5dbbfe575d0 100644 --- a/.github/workflows/build-and-deploy.yml +++ b/.github/workflows/build-and-deploy.yml @@ -19,6 +19,10 @@ env: RESOURCE_GROUP: rg-blog-johnnyreilly-com LOCATION: westeurope STATICWEBAPPNAME: blog.johnnyreilly.com + FUNCTIONAPPNAME: func-blog-johnnyreilly-com + HOSTINGPLANNAME: ASP-rgblogjohnnyreillycom-a345 + STORAGEACCOUNTNAME: stblogjohnnyreilly + CUSTOMDOMAINNAME: blog.johnnyreilly.com TAGS: '{"owner":"johnnyreilly", "email":"johnny_reilly@hotmail.com"}' jobs: @@ -109,10 +113,13 @@ jobs: --parameters \ branch='main' \ location='${{ env.LOCATION }}' \ - name='${{ env.STATICWEBAPPNAME }}' \ + staticWebAppName='${{ env.STATICWEBAPPNAME }}' \ + functionAppName='${{ env.FUNCTIONAPPNAME }}' \ + hostingPlanName='${{ env.HOSTINGPLANNAME }}' \ + storageAccountName='${{ env.STORAGEACCOUNTNAME }}' \ tags='${{ env.TAGS }}' \ repositoryToken='${{ secrets.WORKFLOW_TOKEN }}' \ - customDomainName='${{ env.STATICWEBAPPNAME }}' + customDomainName='${{ env.CUSTOMDOMAINNAME }}' - name: Static Web App - deploy infra id: static_web_app_deploy @@ -127,10 +134,13 @@ jobs: --parameters \ branch='main' \ location='${{ env.LOCATION }}' \ - name='${{ env.STATICWEBAPPNAME }}' \ + staticWebAppName='${{ env.STATICWEBAPPNAME }}' \ + functionAppName='${{ env.FUNCTIONAPPNAME }}' \ + hostingPlanName='${{ env.HOSTINGPLANNAME }}' \ + storageAccountName='${{ env.STORAGEACCOUNTNAME }}' \ tags='${{ env.TAGS }}' \ repositoryToken='${{ secrets.WORKFLOW_TOKEN }}' \ - customDomainName='${{ env.STATICWEBAPPNAME }}' + customDomainName='${{ env.CUSTOMDOMAINNAME }}' - name: Static Web App - get API key for deployment id: static_web_app_apikey diff --git a/infra/function.bicep b/infra/function.bicep new file mode 100644 index 00000000000..3e4581f452e --- /dev/null +++ b/infra/function.bicep @@ -0,0 +1,80 @@ +param functionAppName string +param location string +param hostingPlanName string +param storageAccountName string +param tags object + +resource functionApp 'Microsoft.Web/sites@2022-03-01' = { + name: functionAppName + kind: 'functionapp,linux' + location: location + tags: tags + properties: { + siteConfig: { + appSettings: [ + { + name: 'FUNCTIONS_EXTENSION_VERSION' + value: '~4' + } + { + name: 'FUNCTIONS_WORKER_RUNTIME' + value: 'node' + } + { + name: 'AzureWebJobsStorage' + value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}' + } + { + name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING' + value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}' + } + { + name: 'WEBSITE_CONTENTSHARE' + value: '${toLower(functionAppName)}a6e3' + } + ] + cors: { + allowedOrigins: [ + 'https://portal.azure.com' + ] + } + use32BitWorkerProcess: false + ftpsState: 'FtpsOnly' + linuxFxVersion: 'Node|16' + } + serverFarmId: serverFarm.id + clientAffinityEnabled: false + httpsOnly: true + } +} + +resource serverFarm 'Microsoft.Web/serverfarms@2022-03-01' = { + name: hostingPlanName + location: location + kind: 'linux' + tags: {} + properties: { + reserved: true + } + sku: { + tier: 'Dynamic' + name: 'Y1' + } + dependsOn: [] +} + +resource storageAccount 'Microsoft.Storage/storageAccounts@2022-05-01' = { + name: storageAccountName + location: location + tags: {} + sku: { + name: 'Standard_LRS' + } + properties: { + supportsHttpsTrafficOnly: true + minimumTlsVersion: 'TLS1_2' + } + kind: 'StorageV2' +} + +output functionAppResourceId string = functionApp.id diff --git a/infra/main.bicep b/infra/main.bicep index 53d66e2c119..ba52958159e 100644 --- a/infra/main.bicep +++ b/infra/main.bicep @@ -1,18 +1,35 @@ param location string param branch string -param name string +param staticWebAppName string +param functionAppName string +param hostingPlanName string +param storageAccountName string param tags object @secure() param repositoryToken string param customDomainName string +module functionApp 'function.bicep' = { + name: 'functionApp' + params: { + location: location + tags: tags + functionAppName: functionAppName + hostingPlanName: hostingPlanName + storageAccountName: storageAccountName + } +} + resource staticWebApp 'Microsoft.Web/staticSites@2021-02-01' = { - name: name + name: staticWebAppName location: location tags: tags sku: { - name: 'Free' - tier: 'Free' + name: 'Standard' + tier: 'Standard' + // Free doesn't work with linked backends + // name: 'Free' + // tier: 'Free' } properties: { repositoryUrl: 'https://github.com/johnnyreilly/blog.johnnyreilly.com' @@ -33,6 +50,14 @@ resource customDomain 'Microsoft.Web/staticSites/customDomains@2021-02-01' = { properties: {} } +resource staticWebAppBackend 'Microsoft.Web/staticSites/linkedBackends@2022-03-01' = { + name: '${staticWebAppName}/backend' + properties: { + backendResourceId: functionApp.outputs.functionAppResourceId + region: location + } +} + output staticWebAppDefaultHostName string = staticWebApp.properties.defaultHostname // eg gentle-bush-0db02ce03.azurestaticapps.net output staticWebAppId string = staticWebApp.id output staticWebAppName string = staticWebApp.name