GitHub Actions deploy error when using 'working-directory' option for Node.js app in subfolder, and zip/unzip artifact steps
Asked Answered
P

1

3

I have a simple Node.js server that I'm deploying to Azure App Service using the following workflow in GitHub Actions. It includes artifact zip and unzip steps as per this answer, to improve deployment performance.

name: Build and deploy Node.js app to Azure Web App - myapp

on:
  push:
    branches:
      - master
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Set up Node.js version
        uses: actions/setup-node@v1
        with:
          node-version: '16.x'

      - name: npm install, build, and test
        run: |
          npm install
          npm run build --if-present
          npm run test --if-present

      - name: Zip artifact for deployment
        run: zip release.zip ./* -r

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v2
        with:
          name: node-app
          path: release.zip

  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'nodetest'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v2
        with:
          name: node-app

      - name: Unzip artifact for deployment
        run: unzip release.zip

      - name: 'Deploy to Azure Web App'
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v2
        with:
          app-name: 'myapp'
          slot-name: 'nodetest'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_9E52572382474FD2A31555D64BEBCB1D }}
          package: .

This works fine. However, I must move the code into subfolder 'Server' of my repo, which means I must change the workflow according to the answers to this questions. I must add the working-directory option to the defaults.

Also, as per here, the upload step doesn't use working-directory, so I have to explicitly specify the path for this step.

My workflow now looks like this:

name: Build and deploy Node.js app to Azure Web App - myapp

on:
  push:
    branches:
      - master
  workflow_dispatch:

jobs:
  build:
    defaults:
      run:
        working-directory: Server

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Set up Node.js version
        uses: actions/setup-node@v1
        with:
          node-version: '16.x'

      - name: npm install, build, and test
        run: |
          npm install
          npm run build --if-present
          npm run test --if-present

      - name: Zip artifact for deployment
        run: zip release.zip ./* -r

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v2
        with:
          name: node-app
          path: Server/release.zip

  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'nodetest'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v2
        with:
          name: node-app

      - name: Unzip artifact for deployment
        run: unzip release.zip

      - name: 'Deploy to Azure Web App'
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v2
        with:
          app-name: 'myapp'
          slot-name: 'nodetest'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_9E52572382474FD2A31555D64BEBCB1D }}
          package: .

Probably not relevant, but I should mention that I have to set PROJECT=Server in the App Service application settings as per here.

During the 'Deploy to Azure Web App' step, an error occurs. These are the logs:

Run azure/webapps-deploy@v2
  with:
    app-name: myapp
    slot-name: test
    publish-profile: ***
    package: .
Package deployment using ZIP Deploy initiated.
Updating submodules.
Preparing deployment for commit id '3fa8fc89-4'.
Generating deployment script.
Using the following command to generate deployment script: 'azure site deploymentscript -y --no-dot-deployment -r "/tmp/zipdeploy/extracted" -o "/home/site/deployments/tools" --basic --sitePath "/tmp/zipdeploy/extracted/Server"'.
The site directory path: ./Server
Generating deployment script for Web Site
Generated deployment script files
Running deployment command...
Command: "/home/site/deployments/tools/deploy.sh"
Handling Basic Web Site deployment.
Error: From directory doesn't exist
An error has occurred during web site deployment.
Kudu Sync failed
\n/opt/Kudu/Scripts/starter.sh "/home/site/deployments/tools/deploy.sh"
Deployment Failed.
Error: Failed to deploy web package to App Service.
Error: Deployment Failed with Error: Package deployment using ZIP Deploy failed. Refer logs for more details.
App Service Application URL: http://myapp-test.azurewebsites.net

The problem appears to be From directory doesn't exist (I found this in the Kudu code here). But I can't see why this is happening.

Any help greatly appreciated.

Paddle answered 29/4, 2022 at 0:33 Comment(0)
P
2

The files must be unzipped to the same folder as where they were zipped from in the repo. It could also be because of the PROJECT setting.

Either way, I solved this by adding -d Server to the unzip command.

It's worth noting that if the path is more than one level deep, the following step must be inserted before the unzip step:

- name: Create folder
  run: mkdir -p ParentFolder/Server
Paddle answered 29/4, 2022 at 0:33 Comment(1)
I didn't notice in your OG question that you had Server in front of the path for actions/upload-artifact@v2, so note to anyone else who didn't know actions/upload-artifact@v2 doesn't account for working-directory as its not a run commandSpruill

© 2022 - 2024 — McMap. All rights reserved.