GitHub 为子文件夹中的 Node.js 应用程序和 zip/unzip 工件步骤使用 'working-directory' 选项时,操作部署错误

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

我有一个简单的 Node.js 服务器,我在 GitHub 操作中使用以下工作流程将其部署到 Azure 应用服务。它包括根据 this answer 的工件压缩和解压缩步骤,以提高部署性能。

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: .

这很好用。但是,我必须将代码移动到我的存储库的子文件夹 'Server' 中,这意味着我必须根据 的答案更改工作流程。我必须将 working-directory 选项添加到 defaults.

此外,根据 here,上传步骤不使用 working-directory,因此我必须明确指定此步骤的路径。

我的工作流程现在是这样的:

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: .

可能不相关,但我应该提一下,我必须按照 here.

在 App Service 应用程序设置中设置 PROJECT=Server

在'Deploy to Azure Web App'步骤中,发生错误。这些是日志:

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

问题似乎是 From directory doesn't exist(我在 Kudu 代码 here 中发现了这个问题)。但我不明白为什么会这样。

非常感谢任何帮助。

文件必须解压缩到与它们在存储库中的压缩位置相同的文件夹中。也可能是因为 PROJECT 设置。

无论如何,我通过在 unzip 命令中添加 -d Server 解决了这个问题。

值得注意的是,如果路径超过一层,在解压步骤之前必须插入以下步骤:

- name: Create folder
  run: mkdir -p ParentFolder/Server