From 1de50ccf42bdcfd49faf68cc8e39d4f73cf1c700 Mon Sep 17 00:00:00 2001 From: Gil LaHaye Date: Thu, 19 Oct 2023 23:35:25 -0400 Subject: [PATCH] Make deploy-webapi do all it needs to by default (#524) ### Motivation and Context Some switches need to be included at the command line to ensure proper modification of the AAD app registration and addition of our URIs to the plugin CORS. These steps should be done by default. ### Description Make all required steps run by default. Fixed population of frontend version info ### Contribution Checklist - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [Contribution Guidelines](https://github.com/microsoft/chat-copilot/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/chat-copilot/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible --- .github/workflows/copilot-build-backend.yml | 2 +- .github/workflows/copilot-deploy-backend.yml | 2 +- .gitignore | 1 + scripts/deploy/deploy-webapi.ps1 | 17 +++++++++++------ scripts/deploy/deploy-webapi.sh | 20 ++++++++++---------- scripts/deploy/package-webapi.ps1 | 15 +++++++++++++-- scripts/deploy/package-webapi.sh | 9 +++++++++ webapp/src/libs/services/BaseService.ts | 5 ++++- 8 files changed, 50 insertions(+), 21 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/.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/deploy-webapi.ps1 b/scripts/deploy/deploy-webapi.ps1 index e5b77f619..cc5fa5064 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 frontend 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..764fe75c1 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-registration 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-registration) + 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..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) { @@ -60,6 +62,15 @@ if (-Not $SkipFrontendFiles) { Write-Host "Building static frontend files..." Push-Location -Path "$PSScriptRoot/../../webapp" + + $filePath = "./.env.production" + if (Test-path $filePath -PathType leaf) { + Remove-Item $filePath + } + + 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 ef5356ad6..73759aadd 100644 --- a/scripts/deploy/package-webapi.sh +++ b/scripts/deploy/package-webapi.sh @@ -112,6 +112,15 @@ if [[ -z "$SKIP_FRONTEND" ]]; then pushd "$SCRIPT_ROOT/../../webapp" + filePath="./.env.production" + if [ -f "$filePath" ]; then + rm "$filePath" + 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 if [ $? -ne 0 ]; then diff --git a/webapp/src/libs/services/BaseService.ts b/webapp/src/libs/services/BaseService.ts index 1200b6c5f..51c02204d 100644 --- a/webapp/src/libs/services/BaseService.ts +++ b/webapp/src/libs/services/BaseService.ts @@ -12,7 +12,10 @@ 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 {