Azure / static-web-apps-cli

Azure Static Web Apps CLI ✨
https://aka.ms/swa/cli-local-development
MIT License
585 stars 112 forks source link

CLI is not correctly deploying dotnet API application. #566

Open scottrudy opened 1 year ago

scottrudy commented 1 year ago

Before filing this issue, please ensure you're using the latest CLI by running swa --version and comparing to the latest version on npm.

Are you accessing the CLI from the default port :4280 ?

Make sure you are accessing the URL printed in the console when running swa start!

ℹ️ NOTE: Make sure to enable debug logs when running any swa commands using --verbose=silly

Describe the bug My application is using parcel to generate static web files backed by a dotnet 6 HTTP function, which works fine using swa start locally. When using swa deploy the API is not found. It does not appear in the API section of the resource in Azure's portal and calls from the UI result in a 404. The project is using an api directory off the root of the project. The zip files are generated and deployed for the static app and the API.

Note that on the portal Overview blade it does show Function errors, but does not indicate any Function hits. The Application Insights blade also shows the warning message, "App Insights is only applicable to Static Web Apps with at least one function. Add a function to your app to enable App Insights."

To Reproduce Steps to reproduce the behavior:

  1. Go to the root of the project
  2. Type in command 'swa deploy -O ./dist/ -i ./api/'
  3. Click on the URL generated by the command
  4. Open up the browser console
  5. See 404 error

Expected behavior I'm expecting to see the call to the API succeed. In addition, I expect to see the API show up in the API section of the resource on the Azure Portal.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context

PS C:\source\z-hack-dreamworld\geotag> swa deploy -O ./dist/ -i ./api/ --verbose=silly

Welcome to Azure Static Web Apps CLI (1.0.2)

Getting config file options from swa-cli.config.json...
Changed directory to C:\source\z-hack-dreamworld\geotag
Using configuration "geotag" from file:
  C:\source\z-hack-dreamworld\geotag\swa-cli.config.json

Deploying front-end files from folder:
  C:\source\z-hack-dreamworld\geotag\dist

Deploying API from folder:
  C:\source\z-hack-dreamworld\geotag\api

No deployment token found. Trying interactive login...
Checking Azure session...
Executing authenticateWithAzureIdentity
- details:
  - tenantId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  - clientId: <undefined>
  - clientSecret: <undefined>
 - useKeychain: true
Keychain is enabled
Executing swaCliPersistencePlugin
Executing before cache access plugin
Machine ID: <hidden>
Invoking crypto service
Getting credentials
Getting credentials from native keychain
Getting keychain reference
isKeychainEnabled: true
KeychainCache: undefined
Attempting to load native keychain
Got native keychain reference
Got native keychain reference
Got credentials from native keychain: <hidden>
Credentials found in native keychain
Credentials is chunked. Reading all chunks...
Got all chunks successfully
Credentials: <hidden>
Decrypting credentials
Decrypted credentials: <hidden>
Credentials content: <hidden>
Credentials: <hidden>
Before cache access plugin. Done.
Executing after cache access plugin
Machine ID: <hidden>
Invoking crypto service
Did TokenCacheContext cache changed: false
After cache access plugin. Done.
✔ Successfully logged into Azure!
Selected tenant: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Selected subscription: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Project credentials:
 - subscriptionId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
 - tenantId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
 - clientId: <undefined>
 - clientSecret: <undefined>
Login successful

Checking project "MyProject" settings...
Looking for project "MyProject"...
Static site found!
- staticSite:
  - id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyResourceGroup/providers/Microsoft.Web/staticSites/MyProject
  - name: MyProject
  - location: East US 2
  - type: Microsoft.Web/staticSites
 - tags:
   - Used: Yes
 - sku:
   - name: Free
   - tier: Free
  - defaultHostname: fakename.1.azurestaticapps.net
  - repositoryUrl:
  - branch:
 - customDomains:
 - privateEndpointConnections:
  - stagingEnvironmentPolicy: Enabled
  - allowConfigFileUpdates: true
  - contentDistributionEndpoint: https://content-eus2.infrastructure.1.azurestaticapps.net
  - keyVaultReferenceIdentity: SystemAssigned
 - userProvidedFunctionApps:
  - provider: None
  - enterpriseGradeCdnStatus: Disabled
Project settings:
 - resourceGroupName: MyResourceGroup
 - staticSiteName: MyProject
 - subscriptionId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
✔ Successfully setup project!
Saving project settings to swa-cli.config.json...
Updating configuration file at C:\source\z-hack-dreamworld\geotag\swa-cli.config.json
Loading existing swa-cli.config.json file at C:\source\z-hack-dreamworld\geotag\swa-cli.config.json

Deployment token provided via remote configuration
 - deploymentToken: <hidden>

Deploying to environment: preview

Trying to read workflow config with values:
 - appLocation: C:\source\z-hack-dreamworld\geotag
 - outputLocation: C:\source\z-hack-dreamworld\geotag\dist
 - apiLocation: C:\source\z-hack-dreamworld\geotag\api
No workflow config folder found at C:\source\z-hack-dreamworld\geotag\.github\workflows
Validating user workflow config (BEFORE):
 - appLocation: C:\source\z-hack-dreamworld\geotag
 - outputLocation: C:\source\z-hack-dreamworld\geotag\dist
 - apiLocation: C:\source\z-hack-dreamworld\geotag\api
Validating user workflow config (AFTER):
 - appLocation: C:\source\z-hack-dreamworld\geotag
 - apiLocation: C:\source\z-hack-dreamworld\geotag\api
 - outputLocation: C:\source\z-hack-dreamworld\geotag\dist
Writing configuration "geotag" to swa-cli.config.json
 - appLocation: .
 - apiLocation: api
 - outputLocation: dist
 - appName: MyProject
 - resourceGroupName: MyResourceGroup
Loading staticwebapp.config.json schema...
Schema loaded successfully from https://json.schemastore.org/staticwebapp.config.json
Compiling schema...
Reading content from staticwebapp.config.json...
Parsing staticwebapp.config.json...
Validating staticwebapp.config.json...
File validated successfully. Continuing with configuration!
Content parsed successfully

Found configuration file:
  C:\source\z-hack-dreamworld\geotag\staticwebapp.config.json

Deploying project to Azure Static Web Apps...
Fetching release metadata for version: stable. Please wait...
 - version: stable
 - buildId: 1.0.020391
 - publishDate: 2022-08-01T17:11:02.6122551Z
- files:
 - linux-x64:
   - url: https://swalocaldeploy.azureedge.net/downloads/1.0.020391/linux/StaticSitesClient
   - sha: 2f2accf69fbc9eb87c9ef35900f61771cfa7d449d177babe6534cc0f952ad993
 - win-x64:
   - url: https://swalocaldeploy.azureedge.net/downloads/1.0.020391/windows/StaticSitesClient.exe
   - sha: 100cc717758ea6ef4af52957128fb0e6b0edd67b0d98b504eabbe7b708f0376e
 - osx-x64:
   - url: https://swalocaldeploy.azureedge.net/downloads/1.0.020391/macOS/StaticSitesClient
   - sha: 34e91e8631e042839f50db6a78d87de62f57401d2a3f4ab616b6bb51264b9411
Local client binary is up to date. Skipping download.
Deploying using C:\Users\scott\.swa\deploy\1.0.020391\StaticSitesClient.exe@1.0.020391
Deploying using the following options:
- env:
  - SWA_CLI_DEBUG: silly
  - SWA_RUNTIME_WORKFLOW_LOCATION: <undefined>
  - SWA_RUNTIME_CONFIG_LOCATION: C:\source\z-hack-dreamworld\geotag
  - SWA_RUNTIME_CONFIG: C:\source\z-hack-dreamworld\geotag\staticwebapp.config.json
  - SWA_CLI_VERSION: 1.0.2
  - SWA_CLI_DEPLOY_DRY_RUN: false
  - SWA_CLI_DEPLOY_BINARY: C:\Users\scott\.swa\deploy\1.0.020391\StaticSitesClient.exe@1.0.020391
  - DEPLOYMENT_ACTION: upload
  - DEPLOYMENT_PROVIDER: swa-cli-1.0.2
  - REPOSITORY_BASE: C:\source\z-hack-dreamworld\geotag
  - SKIP_APP_BUILD: true
  - SKIP_API_BUILD: true
  - DEPLOYMENT_TOKEN: <hidden>
  - APP_LOCATION: C:\source\z-hack-dreamworld\geotag\dist
  - API_LOCATION: C:\source\z-hack-dreamworld\geotag\api
  - CONFIG_FILE_LOCATION: C:\source\z-hack-dreamworld\geotag
  - VERBOSE: true
  - DEPLOYMENT_ENVIRONMENT: preview
ℹ DeploymentId: c4d368c4-1230-4808-bc3c-26b48f03100b
ℹ
ℹ Verbose logging enabled
ℹ Build timeout not specified, defaulting to 15 minutes
ℹ App Directory Location: 'C:\source\z-hack-dreamworld\geotag\dist' was found.
ℹ config_file_location: config file 'C:\source\z-hack-dreamworld\geotag\staticwebapp.config.json' was found.
ℹ Looking for event info
ℹ Event info parsed from action options.
ℹ Could not get repository branch. Proceeding
ℹ Could not get repository url. Proceeding
ℹ Skipping step to build C:\source\z-hack-dreamworld\geotag\dist with Oryx
ℹ Api Directory Location: 'C:\source\z-hack-dreamworld\geotag\api' was found.
ℹ Didn't find Oryx manifest file under location: C:\source\z-hack-dreamworld\geotag\ss-oryx\app-manifest\oryx-manifest.toml
ℹ Determined default file to be: index.html
ℹ Using 'staticwebapp.config.json' file for configuration information, 'routes.json' will be ignored.
ℹ Skipping step to build C:\source\z-hack-dreamworld\geotag\api with Oryx
ℹ Function Runtime Information. OS: windows, Functions Runtime: ~4, dotnet version: 6.0
ℹ Zipping Api Artifacts
ℹ Api Zip will be created from directory: C:\source\z-hack-dreamworld\geotag\api
ℹ Api Content Hash: 190108b5b766139125fb7fbca3ec5134
ℹ Done Zipping Api Artifacts
ℹ Zipping App Artifacts
ℹ App Zip will be created from directory: C:\source\z-hack-dreamworld\geotag\dist
ℹ Done Zipping App Artifacts
ℹ Uploading build artifacts.
ℹ Finished Upload. Polling on deployment.
ℹ Status: InProgress. Time: 0.054465(s)
ℹ Status: InProgress. Time: 15.7585498(s)
ℹ Status: Succeeded. Time: 31.018438(s)
ℹ Deployment Complete :)
ℹ Thanks for using Azure Static Web Apps!
✔ Project deployed to https://fakename-preview.eastus2.1.azurestaticapps.net 🚀
scottrudy commented 1 year ago

As of now I just deployed the API to an Azure Function with no changes. I then modified the web page service call to use the Azure Function host name. Works flawlessly, but would be better to have them working in one resource.

manekinekko commented 1 year ago

Hi @scottrudy have you tried the steps from our documentation here? I guess the dotnet API needs to be built before being deployed.

scottrudy commented 1 year ago

@manekinekko In looking at this command, the publish step is for the Blazor app, which references the publish folder on the command line.

swa deploy ./Client/bin/Release/net6.0/publish/wwwroot --api-location ./Api

The API portion, which is the second half of the command, just uses the directory for a reference.

justinyoo commented 1 year ago

@manekinekko I have the exact same issue, using SWA CLI v1.0.3. Here are my log file:

❯ swa deploy -d $swa_key --env default --verbose=silly

Welcome to Azure Static Web Apps CLI (1.0.3)

Getting config file options from swa-cli.config.json...
Changed directory to /workspaces/blazor-wasm-time-series
Using configuration "blazor-wasm-time-series" from file:
  /workspaces/blazor-wasm-time-series/swa-cli.config.json

Deploying front-end files from folder:
  /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web/wwwroot

Deploying API from folder:
  /workspaces/blazor-wasm-time-series/src/WaterConsumption.Proxy

Deployment token provided via flag
 - --deployment-token: <hidden>

Deploying to environment: default

Trying to read workflow config with values:
 - appLocation: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web
 - outputLocation: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web/wwwroot
 - apiLocation: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Proxy
No workflow config folder found at /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web/.github/workflows
Validating user workflow config (BEFORE):
 - appLocation: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web
 - outputLocation: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web/wwwroot
 - apiLocation: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Proxy
Validating user workflow config (AFTER):
 - appLocation: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web
 - apiLocation: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Proxy
 - outputLocation: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web/wwwroot
Loading staticwebapp.config.json schema...
Schema loaded successfully from https://json.schemastore.org/staticwebapp.config.json
Compiling schema...
Reading content from staticwebapp.config.json...
Parsing staticwebapp.config.json...
Validating staticwebapp.config.json...
File validated successfully. Continuing with configuration!
Content parsed successfully

Found configuration file:
  /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web/staticwebapp.config.json

Deploying project to Azure Static Web Apps...
Fetching release metadata for version: stable. Please wait...
 - version: stable
 - buildId: 1.0.020981
 - publishDate: 2022-09-29T17:37:34.4094014Z
- files:
 - linux-x64:
   - url: https://swalocaldeploy.azureedge.net/downloads/1.0.020981/linux/StaticSitesClient
   - sha: e55db21d0720a12098804559152303e66846bdd94c13a30b30be29a0fc9ae366
 - win-x64:
   - url: https://swalocaldeploy.azureedge.net/downloads/1.0.020981/windows/StaticSitesClient.exe
   - sha: 4e4eb72aef26641e236c24929c822500e956bda099ca78054112cadf20261107
 - osx-x64:
   - url: https://swalocaldeploy.azureedge.net/downloads/1.0.020981/macOS/StaticSitesClient
   - sha: e0f2ae7ec297510a1b3b9332c0dc0dd962a51adb8ea13cd4f3746d79fc24a7e9
Local client binary is up to date. Skipping download.
Deploying using /home/vscode/.swa/deploy/1.0.020981/StaticSitesClient@1.0.020981
Deploying using the following options:
- env:
  - SWA_CLI_DEBUG: silly
  - SWA_RUNTIME_WORKFLOW_LOCATION: <undefined>
  - SWA_RUNTIME_CONFIG_LOCATION: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web
  - SWA_RUNTIME_CONFIG: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web/staticwebapp.config.json
  - SWA_CLI_VERSION: 1.0.3
  - SWA_CLI_DEPLOY_DRY_RUN: false
  - SWA_CLI_DEPLOY_BINARY: /home/vscode/.swa/deploy/1.0.020981/StaticSitesClient@1.0.020981
  - DEPLOYMENT_ACTION: upload
  - DEPLOYMENT_PROVIDER: SwaCli
  - REPOSITORY_BASE: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web
  - SKIP_APP_BUILD: true
  - SKIP_API_BUILD: true
  - DEPLOYMENT_TOKEN: <hidden>
  - APP_LOCATION: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web/wwwroot
  - API_LOCATION: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Proxy
  - CONFIG_FILE_LOCATION: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web
  - VERBOSE: true
  - DEPLOYMENT_ENVIRONMENT: default
ℹ DeploymentId: 810de18d-b6e7-4ca5-986a-65a49a634128
ℹ 
ℹ Verbose logging enabled
ℹ Build timeout not specified, defaulting to 15 minutes
ℹ App Directory Location: '/workspaces/blazor-wasm-time-series/src/WaterConsumption.Web/wwwroot' was found.
ℹ config_file_location: config file '/workspaces/blazor-wasm-time-series/src/WaterConsumption.Web/staticwebapp.config.json' was found.
ℹ Looking for event info
ℹ Event info parsed from action options.
ℹ Could not get repository branch. Proceeding
ℹ Could not get repository url. Proceeding
ℹ Skipping step to build /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web/wwwroot with Oryx
ℹ Api Directory Location: '/workspaces/blazor-wasm-time-series/src/WaterConsumption.Proxy' was found.
ℹ Didn't find Oryx manifest file under location: /workspaces/blazor-wasm-time-series/ss-oryx/app-manifest/oryx-manifest.toml
ℹ Determined default file to be: index.html
ℹ Using 'staticwebapp.config.json' file for configuration information, 'routes.json' will be ignored.
ℹ Copying 'staticwebapp.config.json' to build output
ℹ Skipping step to build /workspaces/blazor-wasm-time-series/src/WaterConsumption.Proxy with Oryx
ℹ Function Runtime Information. OS: windows, Functions Runtime: ~4, dotnet version: 6.0
ℹ Zipping Api Artifacts
ℹ Api Zip will be created from directory: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Proxy
ℹ Api Content Hash: ca391616580c35e88608ddcf11c9af77
ℹ Done Zipping Api Artifacts
ℹ Zipping App Artifacts
ℹ App Zip will be created from directory: /workspaces/blazor-wasm-time-series/src/WaterConsumption.Web/wwwroot
ℹ Done Zipping App Artifacts
ℹ Uploading build artifacts.
ℹ Finished Upload. Polling on deployment.
ℹ Status: InProgress. Time: 0.1892429(s)
ℹ Status: InProgress. Time: 15.457412(s)
ℹ Status: Succeeded. Time: 30.6338286(s)
ℹ Deployment Complete :)
ℹ Thanks for using Azure Static Web Apps!
✔ Project deployed to https://brave-mud-0b19c941e.2.azurestaticapps.net 🚀

And here's my repo: https://github.com/justinyoo/blazor-wasm-time-series

I can deploy the same code without changes through VS Code's right-click publish. What I identified was that, if I use the SWA CLI, the managed API doesn't show up:

image

On the other hand, if I use VS Code's right-click publish, it shows the managed backend API:

image

The main difference between the right-click publish and swa deploy is that the right-click publish is using GitHub Actions for deploy.

psimsa commented 1 year ago

It appears the swa deploy command is sending the entire .net project rather than the actual compiled binaries produced by dotnet publish which works on other runtimes but not for .Net. For me, the following worked (.net 6 isolated api project + blazor wasm UI with all default settings):

swa build
swa deploy -i api\bin\Release\net6.0\publish
psimsa commented 1 year ago

Btw this actually appears to be tightly related to #510

andrewreeman commented 1 year ago

I just ran into this issue now and can confirm @ofcoursedude's solution worked great for me.

It appears the swa deploy command is sending the entire .net project rather than the actual compiled binaries produced by dotnet publish which works on other runtimes but not for .Net. For me, the following worked (.net 6 isolated api project + blazor wasm UI with all default settings):

swa build
swa deploy -i api\bin\Release\net6.0\publish
pwang2 commented 1 year ago

any one know a WHY here., it is super frustrating to me as the managed API link just briefly shown and gone again... something looks not right I feel.

martinpyman commented 7 months ago

Even at version 1.1.2 this is still an issue, I've use BYOF up until today and trying to get it deploy was very frustrating until coming across this thread!