Andrei Kniazev
Andrei Kniazev

Reputation: 309

Task AzureStaticWebApp@0 'could not detect this directory' but its presented

I am working on building a pipeline using AzureDevOps, and I face a strange problem.

This is my pipeline:

- stage: 'Test'
  displayName: 'Deploy to the test environment'
  dependsOn: Build
  jobs:
    - job: 'Deploy'
      steps:
      - download: current
        artifact: lorehub-front
      - bash: cd $(Pipeline.Workspace); echo $(ls)
      - bash: cd $(Pipeline.Workspace)/lorehub-front; echo $(ls)
      - task: AzureStaticWebApp@0
        displayName: 'Publish to Azure Static WebApp'
        inputs:
          app_location: $(Pipeline.Workspace)/lorehub-front
          azure_static_web_apps_api_token: xxxx

The first bash shows that the folder 'lorehub-front' is presented

The second bash shows that the inside folder is correct files (index.html and etc)

Script contents: cd /home/vsts/work/1/lorehub-front; echo $(ls)

android-chrome-192x192.png android-chrome-512x512.png apple-touch-icon.png css env-config.js favicon-16x16.png favicon-32x32.png favicon.ico fonts index.html js site.webmanifest

But I am receiving this error:

App Directory Location: '/home/vsts/work/1/lorehub-front' is invalid. Could not detect this directory. Please verify your deployment configuration file reflects your repository structure.

Upvotes: 4

Views: 5021

Answers (3)

Jim Geurts
Jim Geurts

Reputation: 20429

After struggling for far too long to get AzureStaticWebApp deployments working, this is where I landed to get it working with a build artifact. I hope this more complete solution is helpful.

stages:
  - stage: Build
    displayName: 'Build project'
    jobs:
      - job: Build
        displayName: 'Build project'
        steps:
          # ... Run your build steps. 
          # The following code assumes that the build output will be placed in a dist folder.
          # For example, running npm run build:
          - task: Npm@1
            inputs:
              command: 'custom'
              customCommand: 'run build'
            displayName: 'Build project'

          - task: ArchiveFiles@2
            inputs:
              rootFolderOrFile: '$(Build.SourcesDirectory)/dist'
              includeRootFolder: false
              archiveFile: '$(Build.ArtifactStagingDirectory)/build-$(Build.BuildId).zip'
              verbose: true
            displayName: 'Archive Build Artifacts'

          - publish: '$(Build.ArtifactStagingDirectory)/build-$(Build.BuildId).zip'
            artifact: drop
            displayName: 'Publish Build Artifact'
  - stage: Deploy
    dependsOn: Build
    displayName: 'Deploy project'
    jobs:
      - deployment: Deploy
        displayName: 'Deploy project'
        environment: $(env)
        pool:
          vmImage: ubuntu-latest
        variables:
          - group: my-project-variable-group
        strategy:
          runOnce:
            deploy:
              steps:
                - download: current
                  artifact: drop

                - task: ExtractFiles@1
                  inputs:
                    archiveFilePatterns: '$(Pipeline.Workspace)/drop/build-$(Build.BuildId).zip'
                    destinationFolder: '$(Pipeline.Workspace)/dist'

                - task: AzureStaticWebApp@0
                  inputs:
                    workingDirectory: '$(Pipeline.Workspace)'
                    app_location: '/dist'
                    output_location: ''
                    skip_app_build: true
                    skip_api_build: true
                    azure_static_web_apps_api_token: $(AZURE_STATIC_WEB_APPS_API_TOKEN)

Couple notes:

  • This uses a variable group called my-project-variable-group. Within that variable group, there is a variable AZURE_STATIC_WEB_APPS_API_TOKEN with the deployment token for the destination static web app.
  • This approach creates and publishes an archive file of the built project. We found that approach to be significantly faster than publishing all files in the dist folder.

Upvotes: 0

Kushan
Kushan

Reputation: 1270

In case anyone else comes across this post from Google or whatever, I had the exact same problem as Andrei above but for the life of me I couldn't get the accepted solution here to work.

No matter what I put in app_location:, the task just flat out refused to see any files.

Upon further investigation, I found this github issue which claims the following (Emphasis mine):

The AzureStaticWebApp task’s app location is relative to the current directory

Looking at the documentation, we can see that this task uses the default directory of $(System.DefaultWorkingDirectory), which is different to $(Pipeline.Workspace) for whatever reason.

So, if you find yourself stuck in the same position and cannot get this task to recognise your artifacts, the solution is to add cwd: $(Pipeline.Workspace) to your task, e.g.

strategy:
runOnce:
  deploy:
    steps:
      - download: current
        artifact: WebApp
      - task: AzureStaticWebApp@0
        inputs:
          app_location: /
          skip_app_build: true
          azure_static_web_apps_api_token: $(deployment_token)
          cwd: $(Pipeline.Workspace)/WebApp

Upvotes: 10

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35484

App Directory Location: '/home/vsts/work/1/lorehub-front' is invalid.

The method to resolve this issue is that you need to change the path to /lorehub-front.

  - task: AzureStaticWebApp@0
    displayName: 'Publish to Azure Static WebApp'
    inputs:
      app_location: /lorehub-front
      azure_static_web_apps_api_token: xxxx

For more detailed info, you could refer to this doc: Tutorial: Publish Azure Static Web Apps with Azure DevOps

Enter / if your application source code is at the root of the repository, or /app if your application code is in a directory called app.

Upvotes: 6

Related Questions