projectkudu / kudu

Kudu is the engine behind git/hg deployments, WebJobs, and various other features in Azure Web Sites. It can also run outside of Azure.
Apache License 2.0
3.12k stars 652 forks source link

selectNodeVersion not working on Github Actions deploy #3453

Closed raph90 closed 6 months ago

raph90 commented 1 year ago

Deployment via Github Actions for an Azure web app fails on linux.

Here are my files:

deploy.sh:

#!/bin/bash

# ----------------------
# KUDU Deployment Script
# Version: 1.0.17
# ----------------------

# Helpers
# -------

exitWithMessageOnError () {
  if [ ! $? -eq 0 ]; then
    echo "An error has occurred during web site deployment."
    echo $1
    exit 1
  fi
}

# Prerequisites
# -------------

# Verify node.js installed
hash node 2>/dev/null
exitWithMessageOnError "Missing node.js executable, please install node.js, if already installed make sure it can be reached from current environment."

# Setup
# -----

SCRIPT_DIR="${BASH_SOURCE[0]%\\*}"
SCRIPT_DIR="${SCRIPT_DIR%/*}"
ARTIFACTS=$SCRIPT_DIR/../artifacts
KUDU_SYNC_CMD=${KUDU_SYNC_CMD//\"}

if [[ ! -n "$DEPLOYMENT_SOURCE" ]]; then
  DEPLOYMENT_SOURCE=$SCRIPT_DIR
fi

if [[ ! -n "$NEXT_MANIFEST_PATH" ]]; then
  NEXT_MANIFEST_PATH=$ARTIFACTS/manifest

  if [[ ! -n "$PREVIOUS_MANIFEST_PATH" ]]; then
    PREVIOUS_MANIFEST_PATH=$NEXT_MANIFEST_PATH
  fi
fi

if [[ ! -n "$DEPLOYMENT_TARGET" ]]; then
  DEPLOYMENT_TARGET=$ARTIFACTS/wwwroot
else
  KUDU_SERVICE=true
fi

if [[ ! -n "$KUDU_SYNC_CMD" ]]; then
  # Install kudu sync
  echo Installing Kudu Sync
  npm install kudusync -g --silent
  exitWithMessageOnError "npm failed"

  if [[ ! -n "$KUDU_SERVICE" ]]; then
    # In case we are running locally this is the correct location of kuduSync
    KUDU_SYNC_CMD=kuduSync
  else
    # In case we are running on kudu service this is the correct location of kuduSync
    KUDU_SYNC_CMD=$APPDATA/npm/node_modules/kuduSync/bin/kuduSync
  fi
fi

# Node Helpers
# ------------

selectNodeVersion () {
  if [[ -n "$KUDU_SELECT_NODE_VERSION_CMD" ]]; then
    SELECT_NODE_VERSION="$KUDU_SELECT_NODE_VERSION_CMD \"$DEPLOYMENT_SOURCE\" \"$DEPLOYMENT_TARGET\" \"$DEPLOYMENT_TEMP\""
    eval $SELECT_NODE_VERSION
    exitWithMessageOnError "select node version failed"

    if [[ -e "$DEPLOYMENT_TEMP/__nodeVersion.tmp" ]]; then
      NODE_EXE=`cat "$DEPLOYMENT_TEMP/__nodeVersion.tmp"`
      exitWithMessageOnError "getting node version failed"
    fi

    if [[ -e "$DEPLOYMENT_TEMP/__npmVersion.tmp" ]]; then
      NPM_JS_PATH=`cat "$DEPLOYMENT_TEMP/__npmVersion.tmp"`
      exitWithMessageOnError "getting npm version failed"
    fi

    if [[ ! -n "$NODE_EXE" ]]; then
      NODE_EXE=node
    fi

    NPM_CMD="\"$NODE_EXE\" \"$NPM_JS_PATH\""
  else
    NPM_CMD=npm
    NODE_EXE=node
  fi
}

##################################################################################################################################
# Deployment
# ----------

echo Handling node.js deployment.

# 1. KuduSync
if [[ "$IN_PLACE_DEPLOYMENT" -ne "1" ]]; then
  "$KUDU_SYNC_CMD" -v 50 -f "$DEPLOYMENT_SOURCE" -t "$DEPLOYMENT_TARGET" -n "$NEXT_MANIFEST_PATH" -p "$PREVIOUS_MANIFEST_PATH" -i ".git;.hg;.deployment;deploy.sh"
  exitWithMessageOnError "Kudu Sync failed"
fi

# 2. Select node version
selectNodeVersion

# 3. Install npm packages
if [ -e "$DEPLOYMENT_TARGET/package.json" ]; then
  cd "$DEPLOYMENT_TARGET"

  echo "Running yarn install"
  eval yarn install
  echo "Build!"
  eval yarn rw build api
  exitWithMessageOnError "yarn failed"
  cd - > /dev/null
fi

##################################################################################################################################
echo "Finished successfully."

.deployment:

[config]
command = bash deploy.sh
WEBSITE_NODE_DEFAULT_VERSION=18.12.0

My Github action:

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

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v3.1.2
        with:
          name: node-app
          path: .

  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@v3.0.2
        with:
          name: node-app

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

And then the output I get:

/opt/Kudu/Scripts/selectNodeVersion.js:166
An error has occurred during web site deployment.
    throw new Error('Unable to locate node.js installation directory at ' + nodejsDir);
select node version failed
    ^

Error: Unable to locate node.js installation directory at /opt/nodejs
    at Object.<anonymous> (/opt/Kudu/Scripts/selectNodeVersion.js:166:11)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:[75](https://github.com/GetAGame-app/getagame2023/actions/runs/4478534776/jobs/7871427631#step:4:76):12)
    at internal/main/run_main_module.js:17:47
/opt/Kudu/Scripts/selectNodeVersion.js:166\n    throw new Error('Unable to locate node.js installation directory at ' + nodejsDir);\n    ^\n\nError: Unable to locate node.js installation directory at /opt/nodejs\n    at Object.<anonymous> (/opt/Kudu/Scripts/selectNodeVersion.js:166:11)\n    at Module._compile (internal/modules/cjs/loader.js:1085:14)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)\n    at Module.load (internal/modules/cjs/loader.js:950:32)\n    at Function.Module._load (internal/modules/cjs/loader.js:[79](https://github.com/GetAGame-app/getagame2023/actions/runs/4478534776/jobs/7871427631#step:4:80)0:12)\n    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)\n    at internal/main/run_main_module.js:17:47\n/opt/Kudu/Scripts/starter.sh bash deploy.sh
Deployment Failed. deployer = GITHUB_ZIP_DEPLOY deploymentPath = ZipDeploy. Extract zip.
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.

The issue is that nodejs does not exist inside of /opt on the machine. I don't know how to get it there, or why it's not there, but I've experimented with new installs for node 18-lts and can confirm that with a new Azure Web App install this does not work.

Any help would be appreciated!


Thanks! We'll be in touch soon.

derhoppe commented 1 year ago

Same issue here, any help appreciated.

talk2MeGooseman commented 1 year ago

I am also having the same issue

kodkroken commented 1 year ago

Same issue here, we're trying to use node 18 for our custom deploy scripts which runs yarn install. However the packages we use require node version >= 16 since node 14 is now deprecated.

sgollapudi77 commented 1 year ago

Hi @raph90 can you please check if you have this Env variable "SCM_SCRIPT_GENERATOR_ARGS" in your app settings and remove it before retrying?

pniederlag commented 1 year ago

Unfortunatly we do see this problem now as well. We didn't touch a thing. Latest success was in 2023-01, but when we tried our next deploy in 2023-07 we do run into troubles...

remote: /opt/Kudu/KuduConsole/Scripts/selectNodeVersion.js:166        
remote:     throw new Error('Unable to locate node.js installation directory at ' + nodejsDir);        
remote:     ^        
remote: 
remote: Error: Unable to locate node.js installation directory at /opt/nodejs        
remote:     at Object.<anonymous> (/opt/Kudu/KuduConsole/Scripts/selectNodeVersion.js:166:11)        
remote:     at Module._compile (internal/modules/cjs/loader.js:1085:14)        
remote:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)        
remote:     at Module.load (internal/modules/cjs/loader.js:950:32)        
remote:     at Function.Module._load (internal/modules/cjs/loader.js:790:12)        
remote:     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)        
remote:     at internal/main/run_main_module.js:17:47        
remote: Deployment Failed. deployer =  deploymentPath =  

we build and run on linux and try to find an (easy) way to ship our code from a jenkins-pipeline to azure function-app-runtime. git push was working from 2022-06(last time we had to overhaul our pipeline) until at least 2023-01

sgollapudi77 commented 1 year ago

Hi @pniederlag can you please check if you have this Env variable "SCM_SCRIPT_GENERATOR_ARGS" in your app settings and remove it before retrying?

pniederlag commented 1 year ago

I found this to be part of .deployment in the repository and now have dropped it. Seems to help for me. Thx.

diff --git a/.deployment b/.deployment
index 33a38ed..8d17029 100644
--- a/.deployment
+++ b/.deployment
@@ -1,2 +1 @@
 [config]
-SCM_SCRIPT_GENERATOR_ARGS=--node
sgollapudi77 commented 1 year ago

Glad that it helped @pniederlag. Let us know if we can close this.

pniederlag commented 1 year ago

@sgollapudi77 my problem seems solved @raph90 was original reporter, I have no clue wether he found a way to get his problem solved.

aawalton commented 1 year ago

I'm hitting the same issue with a fresh App Service instance and the default custom deployment script for node.

Reproduction repo here.

derhoppe commented 1 year ago

Hi, we did solve this somehow. I really can't remember the full steps but we did create a .deployment file with this content:

[config] SCM_SCRIPT_GENERATOR_ARGS=--basic SCM_DO_BUILD_DURING_DEPLOYMENT=false ENABLE_ORYX_BUILD=false WEBSITE_NPM_DEFAULT_VERSION=6.14.15 command = bash deploy.sh

and we did change the deploy.sh by removing the selectNodeVersion line and adding these two lines:

# selectNodeVersion NPM_CMD=npm NODE_EXE=node

Now it works for us.

Maybe that helps someone here...

jvano commented 6 months ago

Hi

If the problem persists and is related to running it on Azure App Service, please open a support incident in Azure: https://learn.microsoft.com/en-us/azure/azure-portal/supportability/how-to-create-azure-support-request

This way we can better track and assist you on this case

Thanks,

Joaquin Vano Azure App Service