Azure / webapps-deploy

Enable GitHub developers to deploy to Azure WebApps using GitHub Actions
MIT License
270 stars 195 forks source link

Deployment is *very* slow (nodeJS with publish profile) #60

Closed 0gust1 closed 1 year ago

0gust1 commented 4 years ago

I'm using a workflow file adapted from "build and deploy a Node.js Web app to Azure using publish profile".

(side remark) : I had issues with symbolic links too (see https://github.com/Azure/webapps-deploy/issues/54#issuecomment-694259266)

Upon analysis, it seems that the whole copy of the node_modules folders to the webroot of the webapp takes ages (10-12 minutes for the Azure Webapp deploy step !) It's very inefficient to process and send many little files over network.

=> Shouldn't the final npm install (and npm run build – I'm using typescript) commands be executed on the target machine instead of a github action container ?

IMHO, the workflow should be like this for the "nodeJS with publish profile" scenario :

Related questions :

alexmiddeleer commented 2 years ago

As far as I can tell this is still an issue for greenfield projects. I am just trying out Azure for the first time today, but I am mostly choosing defaults for everything.

  1. I created a GitHub repo with no code in it.
  2. I created a node web app in Azure, connected to my repo. Azure created a deployment action for me(everything default except I changed the default branch to a branch called ci, since I didn't want the app to redeploy for every change to main)
  3. I installed a node framework that comes with a bunch of node_modules out of the box. Hello world works on my local machine. I merge the code to the ci branch and it automatically creates a deploy job.
  4. Deploys now take 15 minutes+, and I can see from the logs that thousands of files from node_modules are being processed.
github-actions[bot] commented 2 years ago

This issue is idle because it has been open for 14 days with no activity.

ThePieMonster commented 2 years ago

Interested in what @alexmiddeleer said as well. I have a GitHub Actions file setup and I can see below that the deployment is taking over 20 minutes. There should be some way to trim this time down, no? I tried sending a zip file instead of individual files but that takes over 30 minutes if I use a zip file.

Any suggestions would be appreciated.

image

github-actions[bot] commented 2 years ago

This issue is idle because it has been open for 14 days with no activity.

hungtranpg commented 2 years ago

I removed npm install step and it run so quickly then. from 60m (fail) to 1m (success).

image image

github-actions[bot] commented 2 years ago

This issue is idle because it has been open for 14 days with no activity.

AndrewVos commented 2 years ago

This is an incredibly poor experience for my first attempt at deploying a project to Azure.

My builds are taking longer than 10 minutes!

Netlify manages this in under 1 minute, on the FREE tier.

github-actions[bot] commented 2 years ago

This issue is idle because it has been open for 14 days with no activity.

ywalia01 commented 2 years ago

Same experience. A single page app with 2 routes and minimal packages is taking >3 hrs to build and deploy on Azure where Netlify takes <1 min. Smh

github-actions[bot] commented 2 years ago

This issue is idle because it has been open for 14 days with no activity.

MoatezNG commented 2 years ago

Hi guys i was able to fix the issue with zipping artifact and unzipping you only need to add the following to your GitHub workflow file :

after npm install , build , test

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

Inside upload artifact change path from . to "release.zip"

after download artifact add

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

And thats it this should improve deployment time by a lot =)

stefanoz-cloud commented 1 year ago

@MoatezNG could you provide your entire file .yml in copy? Thanks

github-actions[bot] commented 1 year ago

This issue is idle because it has been open for 14 days with no activity.

abacaj commented 1 year ago

Takes 30+ min for my deploy step on Azure, just < 5min on DigitalOcean app service. Sucks that microsoft hasn't provided any guidance here.

MoatezNG commented 1 year ago

@stefanoz-cloud sure

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

on:
  push:
    branches:
      - main
  workflow_dispatch:

env:
  AZURE_WEBAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
  NODE_VERSION: '16.x' # set this to the node version to use

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: npm install, build
        run: |
          npm install
          npm run build --if-present
      - name: Zip artifact for deployment
        run: zip release.zip ./* -r
      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v3
        with:
          name: node-app
          path: release.zip

  deploy:
    permissions:
      contents: none
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'Development'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v3
        with:
          name: node-app
      - name: unzip artifact for deployment
        run: unzip release.zip
      - name: 'Deploy to Azure WebApp'
        id: 'id'
        uses: azure/webapps-deploy@v2
        with:
          app-name: 'NAME'
          slot-name: 'SLOT'
          publish-profile: 'SECRET'
          package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
sven5 commented 1 year ago

The deployment is slow when there are many files to deploy. When uploading a Zip file it's been extracted on the target App service (Linux). This takes a while. I think this is just a serial operation. We're deploying about ~10.000 static web files (html, js, css) to our App service and it takes ~20min. It's an output of a Gatsby build.

The only way to speed up deployment time is to decrease the artifact size.

abacaj commented 1 year ago

The deployment is slow when there are many files to deploy. When uploading a Zip file it's been extracted on the target App service (Linux). This takes a while. I think this is just a serial operation. We're deploying about ~10.000 static web files (html, js, css) to our App service and it takes ~20min. It's an output of a Gatsby build.

The only way to speed up deployment time is to decrease the artifact size.

Not sure how other platforms handle it, but I’ve only seen this delay deploying to Azure.

Vercel, digitalocean and a few others do not have this issue, same codebase.

sven5 commented 1 year ago

I could imagine that the deployment process, especially the Zip unpacking on the App Service, would be faster when running in parallel.

Another option would be using Docker images. You create your custom image and upload it to a container registry. After that the image is applied on the App Service. Perhaps this is faster than deploying a Zip file.

psierak commented 1 year ago

@MoatezNG your answer worked for me. Brought my build and deploy from 2.5 hrs to 20 min. Thank you

tefkah commented 1 year ago

Something to add: While the deployment process takes about 30 mins, after which it crashes with the mysterious "Refer to logs" error, I do already see the changes reflected after about 5ish minutes. No idea why it doesn't just complete, kind of a waste of energy, but at the very least iteration time isn't twice per hour.

kumaraksh1 commented 1 year ago

Similar issue (https://github.com/Azure/webapps-deploy/issues/229) is going on, we will be fixing this soon. Closing this issue for now and the progress can be tracked from the mentioned issue.

tefkah commented 1 year ago

great to hear, thank you!

hharb3 commented 1 year ago

In upload artifacts section include only build/ directory

sabeeshvk commented 1 year ago

Making the zip file from GitHub build process and doing unzip on the Azure side seems to work much better. I used the steps as mentioned by MoatezNG above.

mavyfaby commented 1 year ago

I significantly reduce my deployment time from 11 minutes to 1 minute by zipping after npm build. Thanks to @MoatezNG.

Before: image

After: image

lhammarstrom commented 1 year ago

I was using Azure Devops to build and package my NextJS app with fully acceptable build times (~1 minute with caching). Before zipping I removed all files except the ones needed (including not deleting node_modules). I agree that npm install should be run on the target machine but I never managed to get it working using .deployment and deploy.sh files, so I opted for the easy route and adding node_modules to the bundle. This had no effect on deploy times which were still horrendous. It could take 20 minutes or more using the Zip deploy.

However when I changed the WEBSITE_RUN_FROM_PACKAGE application setting to 1 my deploys took 20 seconds. So for everyone banging their heads: go to configuration and set:

WEBSITE_RUN_FROM_PACKAGE=1

0gust1 commented 1 year ago

A small update (as I originally created the issue...)

Zhaph commented 1 year ago

Website_Run_From_Package is fine if your app can run from inside the zip file. If you're using Incremental Static Regeneration to build new pages on the fly from data stores, etc. then this won't work for you, and you'll need to unpack the Zip file at one end or the other.

lhammarstrom commented 1 year ago

Website_Run_From_Package is fine if your app can run from inside the zip file. If you're using Incremental Static Regeneration to build new pages on the fly from data stores, etc. then this won't work for you, and you'll need to unpack the Zip file at one end or the other.

Well it continues to be an issue to deploy anything other than .NET on Azure

Zhaph commented 1 year ago

Indeed. Just switched to the AzureWebApp task, increased the memory with NODE_OPTIONS: --max_old_space_size=4096 and now it's failing due to path lengths when trying to unpack and sort the files out:

Cleaning up temp folders from previous zip deployments and extracting pushed zip file C:\local\Temp\zipdeploy\xeqz5oqd.zip (205.23 MB) to C:\local\Temp\zipdeploy\extracted
The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

Not sure who's really at fault here, Windows with its legacy path requirements, or node and npm with it's 90k+ JS files required to run a website...

IliaZolas commented 1 year ago

Hey guys, I wanted to validate @MoatezNG answer as successful.

After you connect your Azure resource to your Github account, you will have a secret and yaml file appear in your respective repo.

You need to access that .yml file and modify it according to @MoatezNG instructions though it might differ for some people. Also remember that indentation is important and will result in errors if done incorrectly.

Here is my new yml file which resulted in deployment in under 4 minutes (remember most of this is automatically generated so you just need to add specific parts):


name: Build and deploy Node.js app to Azure Web App - <your-app-name-is automatically generated here>

on:
  push:
    branches:
      - main
  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
          CI=false npm run build --if-present // I did this to skip warnings as errors, remove if you want
          CI=false npm run test --if-present // I did this to skip warnings as errors, remove if you want

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

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

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

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v2
        with:
          name: release.zip
      - 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: '<your-app-name-is automatically generated here>'
          slot-name: 'Production'
          publish-profile: ${{ secrets.AZUREAPPSERVICE... is automatically generated here> }}
          package: .

P.S. if you're publishing a react app you need to include your build file in your repo (npm run build will generate this file on your machine) ✌️

starthecode commented 1 year ago

I was using Azure Devops to build and package my NextJS app with fully acceptable build times (~1 minute with caching). Before zipping I removed all files except the ones needed (including not deleting node_modules). I agree that npm install should be run on the target machine but I never managed to get it working using .deployment and deploy.sh files, so I opted for the easy route and adding node_modules to the bundle. This had no effect on deploy times which were still horrendous. It could take 20 minutes or more using the Zip deploy.

However when I changed the WEBSITE_RUN_FROM_PACKAGE application setting to 1 my deploys took 20 seconds. So for everyone banging their heads: go to configuration and set:

WEBSITE_RUN_FROM_PACKAGE=1

this solve the delay to deployment but adding this, breaking application and getting this error - code: 'MODULE_NOT_FOUND',

Screenshot04454_1

sgollapudi77 commented 1 year ago

Hi Everyone, we've made changes to the workflow file created by default from the portal. Also, for others who are already using the workflow file you can add 2 steps as mentioned in this comment. Please reply on the other issue #229 if you're still facing the issue.

braedonwatkins commented 11 months ago

commenting to keep reference. having a similar situation of ~15min build time even with a zipped directory.

i tried excluding the node_modules folder from the zip and then doing npm install in the startup command. i verified it was targeting the correct directory and that it was correctly installing modules but the apiapp just says there was an error.

may try using a bash script in the /home directory, as some others in the thread suggest, but i cant see why that would work as opposed to a startup command. maybe the order of operations is such that the CI/CD does its build and runs the api.js file then runs the startup command? but still when the api fails wouldnt it retry and then work? why does it continue to fail?

MrSharpp commented 10 months ago

I was using Azure Devops to build and package my NextJS app with fully acceptable build times (~1 minute with caching). Before zipping I removed all files except the ones needed (including not deleting node_modules). I agree that npm install should be run on the target machine but I never managed to get it working using .deployment and deploy.sh files, so I opted for the easy route and adding node_modules to the bundle. This had no effect on deploy times which were still horrendous. It could take 20 minutes or more using the Zip deploy. However when I changed the WEBSITE_RUN_FROM_PACKAGE application setting to 1 my deploys took 20 seconds. So for everyone banging their heads: go to configuration and set: WEBSITE_RUN_FROM_PACKAGE=1

this solve the delay to deployment but adding this, breaking application and getting this error - code: 'MODULE_NOT_FOUND',

Screenshot04454_1

fixed this issue by changing the zip command to

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

after it, the app runs perfect and deploying is fast too

jasperfirecai2 commented 9 months ago

going by the deployment logs in azure, a lot of time taken is azure not being very smart about handling zip deploys, and the workflow staying connected during that. WHen you deploy a zip file it puts it in a /tmp directory, then extracts it to /tmp/zipdeploy/extracted, THEN copies the files over to your destination. it would be a whole lot faster if it just extracted into the target directly

ogysha commented 9 months ago

Here is the workflow which runs fast.

name: FE on Azure App Service CI/CD

on:
  workflow_dispatch:
  push:
    branches:
      - main
      - develop
  pull_request:
    types: [opened, synchronize, reopened, closed]
    branches:
      - main
      - develop

env:
  AZURE_WEBAPP_NAME: fe-app-dev
  AZURE_WEBAPP_PACKAGE_PATH: build
  NODE_VERSION: 20.x

permissions:
  contents: read

jobs:
  build-and-deploy:
    name: Build and deploy
    runs-on: ubuntu-latest
    env:
      NODE_ENV: production
      VITE_MY_VAR: ${{ secrets.MY_VAR }}
    environment:
      name: dev
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    steps:
      - uses: actions/checkout@v4

      - name: Set up Node.js version
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: yarn install and build
        run: |
          yarn install --frozen-lockfile --production=false
          yarn build

      - name: Deploy to Azure Web App
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v3
        with:
          app-name: ${{ env.AZURE_WEBAPP_NAME }}
          publish-profile: ${{ secrets.AZURE_WEB_APP_PUBLISH_PROFILE }}
          package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}