From 8be95ce2739b904bfcd3b4d740bd713f16805598 Mon Sep 17 00:00:00 2001 From: Gil LaHaye Date: Thu, 19 Oct 2023 14:52:16 -0700 Subject: [PATCH 1/5] Make deploy-webapi do all it needs to by default --- .github/workflows/copilot-build-backend.yml | 2 +- .github/workflows/copilot-deploy-backend.yml | 2 +- scripts/deploy/deploy-webapi.ps1 | 17 +++++++++++------ scripts/deploy/deploy-webapi.sh | 20 ++++++++++---------- scripts/deploy/package-webapi.ps1 | 8 ++++++++ scripts/deploy/package-webapi.sh | 8 ++++++++ 6 files changed, 39 insertions(+), 18 deletions(-) diff --git a/.github/workflows/copilot-build-backend.yml b/.github/workflows/copilot-build-backend.yml index feebfb782..f6f4b9be6 100644 --- a/.github/workflows/copilot-build-backend.yml +++ b/.github/workflows/copilot-build-backend.yml @@ -57,7 +57,7 @@ jobs: - name: Package Copilot Chat WebAPI run: | - scripts\deploy\package-webapi.ps1 -Configuration Release -DotnetFramework net6.0 -TargetRuntime win-x64 -OutputDirectory ${{ github.workspace }}\scripts\deploy -Version ${{ steps.versiontag.outputs.versiontag }} -InformationalVersion "Built from commit ${{ steps.gitversion.outputs.ShortSha }} on $(Get-Date -Format "yyyy-MM-dd")" -SkipFrontendFiles=${{ github.event_name == 'pull_request' }} + scripts\deploy\package-webapi.ps1 -Configuration Release -DotnetFramework net6.0 -TargetRuntime win-x64 -OutputDirectory ${{ github.workspace }}\scripts\deploy -Version ${{ steps.versiontag.outputs.versiontag }} -InformationalVersion "Built from commit ${{ steps.gitversion.outputs.ShortSha }} on $(Get-Date -Format 'yyyy-MM-dd')" -SkipFrontendFiles=${{ github.event_name == 'pull_request' }} - name: Upload package to artifacts uses: actions/upload-artifact@v3 diff --git a/.github/workflows/copilot-deploy-backend.yml b/.github/workflows/copilot-deploy-backend.yml index 0f450d270..50a0cfe71 100644 --- a/.github/workflows/copilot-deploy-backend.yml +++ b/.github/workflows/copilot-deploy-backend.yml @@ -81,4 +81,4 @@ jobs: - name: "Deploy" run: | - scripts/deploy/deploy-webapi.sh -p "${{ github.workspace }}/${{inputs.ARTIFACT_NAME}}/webapi.zip" -d ${{inputs.DEPLOYMENT_NAME}} -s ${{secrets.AZURE_SUBSCRIPTION_ID}} -rg ${{vars.CC_DEPLOYMENT_GROUP_NAME}} --register-cors + scripts/deploy/deploy-webapi.sh -p "${{ github.workspace }}/${{inputs.ARTIFACT_NAME}}/webapi.zip" -d ${{inputs.DEPLOYMENT_NAME}} -s ${{secrets.AZURE_SUBSCRIPTION_ID}} -rg ${{vars.CC_DEPLOYMENT_GROUP_NAME}} --skip-app-registration diff --git a/scripts/deploy/deploy-webapi.ps1 b/scripts/deploy/deploy-webapi.ps1 index e5b77f619..8141acc6c 100644 --- a/scripts/deploy/deploy-webapi.ps1 +++ b/scripts/deploy/deploy-webapi.ps1 @@ -27,12 +27,12 @@ param( $PackageFilePath = "$PSScriptRoot/out/webapi.zip", [switch] - # Switch to add our URIs in app registration's redirect URIs if missing - $EnsureUriInAppRegistration, + # Don't attempt to add our URIs in app registration's redirect URIs + $SkipAppRegistration, [switch] - # Switch to add our URIs in CORS origins for our plugins - $RegisterPluginCors + # Don't attempt to add our URIs in CORS origins for our plugins + $SkipCorsRegistration ) # Ensure $PackageFilePath exists @@ -107,7 +107,7 @@ if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } -if ($EnsureUriInAppRegistration) { +if (-Not $SkipAppRegistration) { $webapiSettings = $(az webapp config appsettings list --name $webapiName --resource-group $ResourceGroupName | ConvertFrom-JSON) $frontendClientId = ($webapiSettings | Where-Object -Property name -EQ -Value Frontend:AadClientId).value $objectId = (az ad app show --id $frontendClientId | ConvertFrom-Json).id @@ -137,12 +137,13 @@ if ($EnsureUriInAppRegistration) { --headers 'Content-Type=application/json' ` --body $body if ($LASTEXITCODE -ne 0) { + Write-Host "Failed to update AAD app registration - Use -SkipAppRegistration switch to skip this step" exit $LASTEXITCODE } } } -if ($RegisterPluginCors) { +if (-Not $SkipCorsRegistration) { foreach ($pluginName in $pluginNames) { $allowedOrigins = $((az webapp cors show --name $pluginName --resource-group $ResourceGroupName --subscription $Subscription | ConvertFrom-Json).allowedOrigins) foreach ($address in $origins) { @@ -150,6 +151,10 @@ if ($RegisterPluginCors) { Write-Host "Ensuring '$origin' is included in CORS origins for plugin '$pluginName'..." if (-not $allowedOrigins -contains $origin) { az webapp cors add --name $pluginName --resource-group $ResourceGroupName --subscription $Subscription --allowed-origins $origin + if ($LASTEXITCODE -ne 0) { + Write-Host "Failed to update plugin CORS URIs - Use -SkipCorsRegistration switch to skip this step" + exit $LASTEXITCODE + } } } } diff --git a/scripts/deploy/deploy-webapi.sh b/scripts/deploy/deploy-webapi.sh index 67c563253..c81d5de30 100755 --- a/scripts/deploy/deploy-webapi.sh +++ b/scripts/deploy/deploy-webapi.sh @@ -11,8 +11,8 @@ usage() { echo " -rg, --resource-group RESOURCE_GROUP Resource group name from a 'deploy-azure.sh' deployment (mandatory)" echo " -p, --package PACKAGE_FILE_PATH Path to the package file from a 'package-webapi.sh' run (default: \"./out/webapi.zip\")" echo " -o, --slot DEPLOYMENT_SLOT Name of the target web app deployment slot" - echo " -r, --register-app Switch to add our URI in app registration's redirect URIs if missing" - echo " -c, --register-cors Register service with the plugins as allowed CORS origin" + echo " -sr, --skip-app-registration Skip adding our URI in app registration's redirect URIs" + echo " -sc, --skip-cors-registraion Skip registration of service with the plugins as allowed CORS origin" } # Parse arguments @@ -39,8 +39,8 @@ while [[ $# -gt 0 ]]; do shift shift ;; - -r|--register-app) - REGISTER_APP=true + -r|--skip-app-registration) + REGISTER_APP=false shift ;; -o|--slot) @@ -48,8 +48,8 @@ while [[ $# -gt 0 ]]; do shift shift ;; - -c|--register-cors) - REGISTER_CORS=true + -c|--skip-cors-registraion) + REGISTER_CORS=false shift ;; *) @@ -143,7 +143,7 @@ if [ $? -ne 0 ]; then exit 1 fi -if [[ -n $REGISTER_APP ]]; then +if [[ -z $REGISTER_APP ]]; then WEBAPI_SETTINGS=$(az webapp config appsettings list --name $WEB_API_NAME --resource-group $RESOURCE_GROUP --output json) FRONTEND_CLIENT_ID=$(echo $WEBAPI_SETTINGS | jq -r '.[] | select(.name == "Frontend:AadClientId") | .value') OBJECT_ID=$(az ad app show --id $FRONTEND_CLIENT_ID | jq -r '.id') @@ -177,13 +177,13 @@ if [[ -n $REGISTER_APP ]]; then --body $BODY if [ $? -ne 0 ]; then - echo "Failed to update app registration $OBJECT_ID with redirect URIs" + echo "Failed to update app registration $OBJECT_ID with redirect URIs - Use -sc switch to skip this step" exit 1 fi fi fi -if [[ -n $REGISTER_CORS ]]; then +if [[ -z $REGISTER_CORS ]]; then for PLUGIN_NAME in $PLUGIN_NAMES; do ALLOWED_ORIGINS=$(az webapp cors show --name $PLUGIN_NAME --resource-group $RESOURCE_GROUP --subscription $SUBSCRIPTION | jq -r '.allowedOrigins[]') for ADDRESS in $(echo "$ORIGINS"); do @@ -192,7 +192,7 @@ if [[ -n $REGISTER_CORS ]]; then if [[ ! "$ALLOWED_ORIGINS" =~ "$ORIGIN" ]]; then az webapp cors add --name $PLUGIN_NAME --resource-group $RESOURCE_GROUP --subscription $SUBSCRIPTION --allowed-origins "$ORIGIN" if [ $? -ne 0 ]; then - echo "Failed to update CORS origins with $ORIGIN" + echo "Failed to update CORS origins with $ORIGIN - Use -sc switch to skip this step" exit 1 fi fi diff --git a/scripts/deploy/package-webapi.ps1 b/scripts/deploy/package-webapi.ps1 index acb0d0f87..3212f3ec2 100755 --- a/scripts/deploy/package-webapi.ps1 +++ b/scripts/deploy/package-webapi.ps1 @@ -60,6 +60,14 @@ if (-Not $SkipFrontendFiles) { Write-Host "Building static frontend files..." Push-Location -Path "$PSScriptRoot/../../webapp" + + if ($Version -ne "0.0.0") { + Add-Content -Path ./.env -Value "REACT_APP_SK_VERSION=$Version" + } + + if ($InformationalVersion -ne "") { + Add-Content -Path ./.env -Value "REACT_APP_SK_BUILD_INFO=$InformationalVersion" + } Write-Host "Installing yarn dependencies..." yarn install diff --git a/scripts/deploy/package-webapi.sh b/scripts/deploy/package-webapi.sh index ef5356ad6..5e98a265b 100644 --- a/scripts/deploy/package-webapi.sh +++ b/scripts/deploy/package-webapi.sh @@ -112,6 +112,14 @@ if [[ -z "$SKIP_FRONTEND" ]]; then pushd "$SCRIPT_ROOT/../../webapp" + if [ "$Version" != "0.0.0" ]; then + echo "REACT_APP_SK_VERSION=$Version" >> .env + fi + + if [ -n "$InformationalVersion" ]; then + echo "REACT_APP_SK_BUILD_INFO=$InformationalVersion" >> .env + fi + echo "Installing yarn dependencies..." yarn install if [ $? -ne 0 ]; then From f84cc78476ed6130027735184f0bc27cc33fa09d Mon Sep 17 00:00:00 2001 From: Gil LaHaye Date: Thu, 19 Oct 2023 15:03:27 -0700 Subject: [PATCH 2/5] Fix typo --- scripts/deploy/deploy-webapi.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/deploy/deploy-webapi.sh b/scripts/deploy/deploy-webapi.sh index c81d5de30..764fe75c1 100755 --- a/scripts/deploy/deploy-webapi.sh +++ b/scripts/deploy/deploy-webapi.sh @@ -12,7 +12,7 @@ usage() { echo " -p, --package PACKAGE_FILE_PATH Path to the package file from a 'package-webapi.sh' run (default: \"./out/webapi.zip\")" echo " -o, --slot DEPLOYMENT_SLOT Name of the target web app deployment slot" echo " -sr, --skip-app-registration Skip adding our URI in app registration's redirect URIs" - echo " -sc, --skip-cors-registraion Skip registration of service with the plugins as allowed CORS origin" + echo " -sc, --skip-cors-registration Skip registration of service with the plugins as allowed CORS origin" } # Parse arguments @@ -48,7 +48,7 @@ while [[ $# -gt 0 ]]; do shift shift ;; - -c|--skip-cors-registraion) + -c|--skip-cors-registration) REGISTER_CORS=false shift ;; From b4a16141ac485510f40663f8e47e3f0b8ba00ca7 Mon Sep 17 00:00:00 2001 From: Gil LaHaye Date: Thu, 19 Oct 2023 18:04:08 -0700 Subject: [PATCH 3/5] Make use of .env.productin --- .gitignore | 1 + scripts/deploy/package-webapi.ps1 | 17 ++++++++++------- scripts/deploy/package-webapi.sh | 11 ++++++----- webapp/src/libs/services/BaseService.ts | 4 +++- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 8253a1c9e..d658427a8 100644 --- a/.gitignore +++ b/.gitignore @@ -410,6 +410,7 @@ FodyWeavers.xsd *.pem .env +.env.production certs/ launchSettings.json config.development.yaml diff --git a/scripts/deploy/package-webapi.ps1 b/scripts/deploy/package-webapi.ps1 index 3212f3ec2..8ead6add7 100755 --- a/scripts/deploy/package-webapi.ps1 +++ b/scripts/deploy/package-webapi.ps1 @@ -46,10 +46,12 @@ $publishedZipFilePath = "$publishedZipDirectory/webapi.zip" if (!(Test-Path $publishedZipDirectory)) { New-Item -ItemType Directory -Force -Path $publishedZipDirectory | Out-Null } -if (!(Test-Path $publishOutputDirectory)) { - New-Item -ItemType Directory -Force -Path $publishOutputDirectory | Out-Null +if (Test-Path $publishOutputDirectory) { + rm $publishOutputDirectory/* -r -force } +New-Item -ItemType Directory -Force -Path $publishOutputDirectory | Out-Null + Write-Host "Build configuration: $BuildConfiguration" dotnet publish "$PSScriptRoot/../../webapi/CopilotChatWebApi.csproj" --configuration $BuildConfiguration --framework $DotNetFramework --runtime $TargetRuntime --self-contained --output "$publishOutputDirectory" /p:AssemblyVersion=$Version /p:FileVersion=$Version /p:InformationalVersion=$InformationalVersion if ($LASTEXITCODE -ne 0) { @@ -61,13 +63,14 @@ if (-Not $SkipFrontendFiles) { Push-Location -Path "$PSScriptRoot/../../webapp" - if ($Version -ne "0.0.0") { - Add-Content -Path ./.env -Value "REACT_APP_SK_VERSION=$Version" + $filePath = "./.env.production" + if (Test-path $filePath -PathType leaf) { + Remove-Item $filePath } - if ($InformationalVersion -ne "") { - Add-Content -Path ./.env -Value "REACT_APP_SK_BUILD_INFO=$InformationalVersion" - } + Add-Content -Path $filePath -Value "REACT_APP_BACKEND_URI=" + Add-Content -Path $filePath -Value "REACT_APP_SK_VERSION=$Version" + Add-Content -Path $filePath -Value "REACT_APP_SK_BUILD_INFO=$InformationalVersion" Write-Host "Installing yarn dependencies..." yarn install diff --git a/scripts/deploy/package-webapi.sh b/scripts/deploy/package-webapi.sh index 5e98a265b..73759aadd 100644 --- a/scripts/deploy/package-webapi.sh +++ b/scripts/deploy/package-webapi.sh @@ -112,13 +112,14 @@ if [[ -z "$SKIP_FRONTEND" ]]; then pushd "$SCRIPT_ROOT/../../webapp" - if [ "$Version" != "0.0.0" ]; then - echo "REACT_APP_SK_VERSION=$Version" >> .env + filePath="./.env.production" + if [ -f "$filePath" ]; then + rm "$filePath" fi - if [ -n "$InformationalVersion" ]; then - echo "REACT_APP_SK_BUILD_INFO=$InformationalVersion" >> .env - fi + echo "REACT_APP_BACKEND_URI=" >> "$filePath" + echo "REACT_APP_SK_VERSION=$Version" >> "$filePath" + echo "REACT_APP_SK_BUILD_INFO=$InformationalVersion" >> "$filePath" echo "Installing yarn dependencies..." yarn install diff --git a/webapp/src/libs/services/BaseService.ts b/webapp/src/libs/services/BaseService.ts index 1200b6c5f..4af69a8f2 100644 --- a/webapp/src/libs/services/BaseService.ts +++ b/webapp/src/libs/services/BaseService.ts @@ -12,7 +12,9 @@ interface ServiceRequest { const noResponseBodyStatusCodes = [202, 204]; -export const BackendServiceUrl = process.env.REACT_APP_BACKEND_URI ?? window.origin; +export const BackendServiceUrl = (process.env.REACT_APP_BACKEND_URI == null || + process.env.REACT_APP_BACKEND_URI.trim() === "") ? window.origin + : process.env.REACT_APP_BACKEND_URI; export const NetworkErrorMessage = '\n\nPlease check that your backend is running and that it is accessible by the app'; export class BaseService { From 703ac2b0cc6818c85617f8f5bde1e5483dee3a1b Mon Sep 17 00:00:00 2001 From: Gil LaHaye Date: Thu, 19 Oct 2023 18:10:47 -0700 Subject: [PATCH 4/5] Prettify modified file --- webapp/src/libs/services/BaseService.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/webapp/src/libs/services/BaseService.ts b/webapp/src/libs/services/BaseService.ts index 4af69a8f2..51c02204d 100644 --- a/webapp/src/libs/services/BaseService.ts +++ b/webapp/src/libs/services/BaseService.ts @@ -12,9 +12,10 @@ interface ServiceRequest { const noResponseBodyStatusCodes = [202, 204]; -export const BackendServiceUrl = (process.env.REACT_APP_BACKEND_URI == null || - process.env.REACT_APP_BACKEND_URI.trim() === "") ? window.origin - : process.env.REACT_APP_BACKEND_URI; +export const BackendServiceUrl = + process.env.REACT_APP_BACKEND_URI == null || process.env.REACT_APP_BACKEND_URI.trim() === '' + ? window.origin + : process.env.REACT_APP_BACKEND_URI; export const NetworkErrorMessage = '\n\nPlease check that your backend is running and that it is accessible by the app'; export class BaseService { From aecd7aae64774ece6d6fb025236ba03ac1a1fbe2 Mon Sep 17 00:00:00 2001 From: Gil LaHaye Date: Thu, 19 Oct 2023 20:31:34 -0700 Subject: [PATCH 5/5] Clarify comment --- scripts/deploy/deploy-webapi.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/deploy/deploy-webapi.ps1 b/scripts/deploy/deploy-webapi.ps1 index 8141acc6c..cc5fa5064 100644 --- a/scripts/deploy/deploy-webapi.ps1 +++ b/scripts/deploy/deploy-webapi.ps1 @@ -27,7 +27,7 @@ param( $PackageFilePath = "$PSScriptRoot/out/webapi.zip", [switch] - # Don't attempt to add our URIs in app registration's redirect URIs + # Don't attempt to add our URIs in frontend app registration's redirect URIs $SkipAppRegistration, [switch]