我无法创建要部署在 Azure 上的 Node.JS Web 应用

我无法创建要部署在 Azure 上的 Node.JS Web 应用

过去一周,我一直在尝试让 Azure 使用 Node.JS 部署一个可访问的网站。让我重新回顾一下我所做的步骤。

1) 我转到 Visual Studio,然后选择基本 Azure Node.js Express 4 应用程序。

2)我得到一个基本项目,我运行该项目并在浏览器上打开本地主机以确认我可以查看提供的基本页面。

3)我将所有代码提交到Azure Git存储库。

4)我在 Azure 上创建了一个管道,通过使用我的 Azure git repo 上的代码,允许它自动创建自己的 yaml 文件,如下所示:

# Node.js Express Web App to Linux on Azure
# Build a Node.js Express app and deploy it to Azure as a Linux web app.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
- master

variables:

  # Azure Resource Manager connection created during pipeline creation
  azureSubscription: '<Censored>'

  # Web app name
  webAppName: 'TheEclipsedLock-Basic-Portfolio-Website-012345'

  # Environment name
  environmentName: 'TheEclipsedLock-Basic-Portfolio-Website-012345'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build stage
  jobs:  
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)

    steps:
    - task: NodeTool@0
      inputs:
        versionSpec: '10.x'
      displayName: 'Install Node.js'

    - script: |
        npm install
        npm run build --if-present
        npm run test --if-present
      displayName: 'npm install, build and test'

    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
        includeRootFolder: false
        archiveType: zip
        archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
        replaceExistingArchive: true

    - upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      artifact: drop

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build
  condition: succeeded()
  jobs:
  - deployment: Deploy
    displayName: Deploy
    environment: $(environmentName)
    pool: 
      vmImage: $(vmImageName)
    strategy:
      runOnce:
        deploy:
          steps:            
          - task: AzureWebApp@1
            displayName: 'Azure Web App Deploy: TheEclipsedLock-Basic-Portfolio-Website-012345'
            inputs:
              azureSubscription: $(azureSubscription)
              appType: webAppLinux
              appName: $(webAppName)
              runtimeStack: 'NODE|10.10'
              package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip
              startUpCommand: 'npm run start'

好的,我要做的就是等待管道完成,对吧?很简单,对吧?

然后我得到这个:

##[section]Starting: npm install, build and test
==============================================================================
Task         : Command line
Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version      : 2.151.2
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
========================== Starting Command Output ===========================
[command]/bin/bash --noprofile --norc /home/vsts/work/_temp/46ee9b20-5f7d-4b04-a1e3-00bad5c1bb2c.sh
npm WARN saveError ENOENT: no such file or directory, open '/home/vsts/work/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/home/vsts/work/package.json'
npm WARN work No description
npm WARN work No repository field.
npm WARN work No README data
npm WARN work No license field.

audited 22 packages in 0.616s
found 4 vulnerabilities (2 moderate, 2 high)
  run `npm audit fix` to fix them, or `npm audit` for details
npm ERR! path /home/vsts/work/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/home/vsts/work/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/vsts/.npm/_logs/2019-09-06T06_42_26_917Z-debug.log
npm ERR! path /home/vsts/work/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/home/vsts/work/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/vsts/.npm/_logs/2019-09-06T06_42_27_284Z-debug.log
##[error]Bash exited with code '254'.
##[section]Finishing: npm install, build and test

过去几天我一直在谷歌上搜索,我看到的各种答案都包括删除 package.json 文件以仅使用 npm init 命令,但是如果我使用 npm init,Azure 的终端就会永远卡住,因为它需要输出,而由于 Azure 的性质,我无法简单地通过它的窗口将我自己的输出提供给它。

在我开发了一个小型基本网站的单独项目中,我创建了以下 yaml 文件:

# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
- master
- development
- feature/*

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: NodeTool@0
  inputs:
    versionSpec: '10.x'
  displayName: 'Install Node.js'

- script: |
    cd AzureWebsite
    npm install
    npm install express
    npm install chai
    npm install mocha
    npm install request
    npm install supertest
    npm install mocha-junit-reporter --save-dev
    npm publish
    cd test
    cd root
    npm install -g mocha
    mocha -R spec server_test.js

- task: ArchiveFiles@2
  displayName: 'Compressing to zip...'
  inputs:
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
    includeRootFolder: false
#    archiveType: 'zip'
#    archiveFile: 'web.zip'
#    replaceExistingArchive: true 

- script: |
    ls


- task: CopyFiles@2
  displayName: 'Copying Files...'
  inputs:
    SourceFolder: '$(Build.SourcesDirectory)'
    Contents: |
        **/*
        package.json
    TargetFolder: '$(build.artifactstagingdirectory)'

- task: PublishBuildArtifacts@1
  displayName: 'Publishing Artifacts...'

中间脚本的目的是对某些网页是否可访问进行单元测试。ArchiveFiles@2 有助于部署,因为发布管道抱怨它没有任何 zip 文件可供使用,所以我只使用工作目录的 zip。我希望它进入应该发布的工件暂存目录。

然后我将压缩的工件文件插入发布管道……你看,它完成了!现在我可以坐下来观看我的网站了——

:( Application Error
If you are the application administrator, you can access the diagnostic resources.
2019-09-06T00:04:02.862036828Z     export NODE_PATH=/node_modules:$NODE_PATH
2019-09-06T00:04:02.862044828Z     export PATH=/node_modules/.bin:$PATH
2019-09-06T00:04:02.862052528Z     if [ -d node_modules ]; then
2019-09-06T00:04:02.870382644Z         mv -f node_modules _del_node_modules || true
2019-09-06T00:04:02.870433146Z         nohup rm -fr _del_node_modules &> /dev/null &
2019-09-06T00:04:02.870561051Z     fi
2019-09-06T00:04:02.870593952Z fi
2019-09-06T00:04:02.879273882Z echo "Done."
2019-09-06T00:04:02.879941807Z if [ -n $injectedAppInsights ]; then
2019-09-06T00:04:02.879960708Z     if [ -f ./oryx-appinsightsloader.js ]; then
2019-09-06T00:04:02.879970308Z         export NODE_OPTIONS='--require ./oryx-appinsightsloader.js '$NODE_OPTIONS
2019-09-06T00:04:02.879978808Z     fi
2019-09-06T00:04:02.881075650Z fi
2019-09-06T00:04:02.881648972Z PATH="$PATH:/home/site/wwwroot" node server.js
2019-09-06T00:04:02.897250264Z Checking if node_modules was compressed...
2019-09-06T00:04:02.897973091Z Done.
2019-09-06T00:04:03.116761493Z internal/modules/cjs/loader.js:583
2019-09-06T00:04:03.116819895Z     throw err;
2019-09-06T00:04:03.116830595Z     ^
2019-09-06T00:04:03.116838996Z 
2019-09-06T00:04:03.116847196Z Error: Cannot find module '/home/site/wwwroot/server.js'
2019-09-06T00:04:03.116855396Z     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
2019-09-06T00:04:03.116863797Z     at Function.Module._load (internal/modules/cjs/loader.js:507:25)
2019-09-06T00:04:03.116871997Z     at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
2019-09-06T00:04:03.116880197Z     at startup (internal/bootstrap/node.js:282:19)
2019-09-06T00:04:03.116888198Z     at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

2019-09-06T01:08:39.125915218Z   _____                               
2019-09-06T01:08:39.139973550Z   /  _  \ __________ _________   ____  
2019-09-06T01:08:39.142557948Z  /  /_\  \___   /  |  \_  __ \_/ __ \ 
2019-09-06T01:08:39.142622550Z /    |    \/    /|  |  /|  | \/\  ___/ 
2019-09-06T01:08:39.143569886Z \____|__  /_____ \____/ |__|    \___  >
2019-09-06T01:08:39.143632489Z         \/      \/                  \/ 
2019-09-06T01:08:39.143745093Z A P P   S E R V I C E   O N   L I N U X
2019-09-06T01:08:39.143778294Z 
2019-09-06T01:08:39.143865997Z Documentation: http://aka.ms/webapp-linux
2019-09-06T01:08:39.143898399Z NodeJS quickstart: https://aka.ms/node-qs
2019-09-06T01:08:39.143987702Z NodeJS Version : v10.14.2
2019-09-06T01:08:39.144020103Z 
2019-09-06T01:08:39.740354185Z /opt/startup/init_container.sh: line 32: [: ==: unary operator expected
2019-09-06T01:08:40.004482186Z Oryx Version : 0.2.20190518.2, Commit: 5e1ddd1855bcb53ce686e2124ed6e9603cb0587a
2019-09-06T01:08:40.005254116Z 
2019-09-06T01:08:41.063282880Z Writing output script to '/opt/startup/startup.sh'
2019-09-06T01:08:41.086379955Z Running #!/bin/sh
2019-09-06T01:08:41.086424356Z 
2019-09-06T01:08:41.086434457Z # Enter the source directory to make sure the script runs where the user expects
2019-09-06T01:08:41.086443257Z cd /home/site/wwwroot
2019-09-06T01:08:41.086451357Z 
2019-09-06T01:08:41.086459258Z if [ -f ./oryx-manifest.toml ]; then
2019-09-06T01:08:41.086467358Z     echo "Found 'oryx-manifest.toml'"
2019-09-06T01:08:41.086475358Z     . ./oryx-manifest.toml
2019-09-06T01:08:41.086483058Z fi
2019-09-06T01:08:41.086490559Z 
2019-09-06T01:08:41.086498059Z if [ -z "$PORT" ]; then
2019-09-06T01:08:41.086505959Z         export PORT=8080
2019-09-06T01:08:41.086513960Z fi
2019-09-06T01:08:41.086521460Z 
2019-09-06T01:08:41.086528960Z echo "Checking if node_modules was compressed..."
2019-09-06T01:08:41.086536960Z case $compressedNodeModulesFile in 
2019-09-06T01:08:41.086544761Z     *".zip")
2019-09-06T01:08:41.086552461Z         echo "Found zip-based node_modules."
2019-09-06T01:08:41.086560161Z         extractionCommand="unzip -q $compressedNodeModulesFile -d /node_modules"
2019-09-06T01:08:41.086568162Z         ;;
2019-09-06T01:08:41.086575762Z     *".tar.gz")
2019-09-06T01:08:41.086583562Z         echo "Found tar.gz based node_modules."
2019-09-06T01:08:41.086591463Z         extractionCommand="tar -xzf $compressedNodeModulesFile -C /node_modules"
2019-09-06T01:08:41.086599363Z          ;;
2019-09-06T01:08:41.086617964Z esac
2019-09-06T01:08:41.086626464Z if [ ! -z "$extractionCommand" ]; then
2019-09-06T01:08:41.086634464Z     echo "Removing existing modules directory..."
2019-09-06T01:08:41.086642464Z     rm -fr /node_modules
2019-09-06T01:08:41.086650065Z     mkdir -p /node_modules
2019-09-06T01:08:41.086657665Z     echo "Extracting modules..."
2019-09-06T01:08:41.086665365Z     $extractionCommand
2019-09-06T01:08:41.086673066Z     export NODE_PATH=/node_modules:$NODE_PATH
2019-09-06T01:08:41.086680766Z     export PATH=/node_modules/.bin:$PATH
2019-09-06T01:08:41.086688366Z     if [ -d node_modules ]; then
2019-09-06T01:08:41.090326604Z         mv -f node_modules _del_node_modules || true
2019-09-06T01:08:41.090348105Z         nohup rm -fr _del_node_modules &> /dev/null &
2019-09-06T01:08:41.097109361Z     fi
2019-09-06T01:08:41.097405672Z fi
2019-09-06T01:08:41.097423073Z echo "Done."
2019-09-06T01:08:41.097694483Z if [ -n $injectedAppInsights ]; then
2019-09-06T01:08:41.097710284Z     if [ -f ./oryx-appinsightsloader.js ]; then
2019-09-06T01:08:41.097983994Z         export NODE_OPTIONS='--require ./oryx-appinsightsloader.js '$NODE_OPTIONS
2019-09-06T01:08:41.098000295Z     fi
2019-09-06T01:08:41.099117237Z fi
2019-09-06T01:08:41.099135338Z PATH="$PATH:/home/site/wwwroot" node server.js
2019-09-06T01:08:41.116457593Z Checking if node_modules was compressed...
2019-09-06T01:08:41.117112918Z Done.
2019-09-06T01:08:41.497851236Z internal/modules/cjs/loader.js:583
2019-09-06T01:08:41.497907838Z     throw err;
2019-09-06T01:08:41.497918538Z     ^
2019-09-06T01:08:41.497927039Z 
2019-09-06T01:08:41.497935339Z Error: Cannot find module '/home/site/wwwroot/server.js'
2019-09-06T01:08:41.497943739Z     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
2019-09-06T01:08:41.497952239Z     at Function.Module._load (internal/modules/cjs/loader.js:507:25)
2019-09-06T01:08:41.497960640Z     at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
2019-09-06T01:08:41.497968940Z     at startup (internal/bootstrap/node.js:282:19)
2019-09-06T01:08:41.497977140Z     at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
2019-09-06 00:02:46.272 INFO  - Starting container for site
2019-09-06 00:02:46.272 INFO  - docker run -d -p 43591:8080 --name theeclipsedlock-portfolio-website-12345_0 -e WEBSITE_NODE_DEFAULT_VERSION=6.9.1 -e APPSETTING_WEBSITE_NODE_DEFAULT_VERSION=6.9.1 -e WEBSITE_SITE_NAME=TheEclipsedLock-Portfolio-Website-12345 -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=theeclipsedlock-portfolio-website-12345.azurewebsites.net -e WEBSITE_INSTANCE_ID=366ef1906eda232dd4fa18f32e0793800c765d26b4da9643746011451b789a58 appsvc/node:lts_1905131832 node server.js 

2019-09-06 00:02:46.272 INFO  - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2019-09-06 00:02:49.238 INFO  - Initiating warmup request to container theeclipsedlock-portfolio-website-12345_0 for site theeclipsedlock-portfolio-website-12345
2019-09-06 00:02:51.436 ERROR - Container theeclipsedlock-portfolio-website-12345_0 for site theeclipsedlock-portfolio-website-12345 has exited, failing site start
2019-09-06 00:02:51.441 ERROR - Container theeclipsedlock-portfolio-website-12345_0 didn't respond to HTTP pings on port: 8080, failing site start. See container logs for debugging.
2019-09-06 00:04:00.371 INFO  - Starting container for site
2019-09-06 00:04:00.372 INFO  - docker run -d -p 13820:8080 --name theeclipsedlock-portfolio-website-12345_0 -e WEBSITE_NODE_DEFAULT_VERSION=6.9.1 -e APPSETTING_WEBSITE_NODE_DEFAULT_VERSION=6.9.1 -e WEBSITE_SITE_NAME=TheEclipsedLock-Portfolio-Website-12345 -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=theeclipsedlock-portfolio-website-12345.azurewebsites.net -e WEBSITE_INSTANCE_ID=366ef1906eda232dd4fa18f32e0793800c765d26b4da9643746011451b789a58 appsvc/node:lts_1905131832 node server.js 

说实话,我对此束手无策。我不知道我能做什么,而且我几乎找不到谷歌上的资源,因为它们都暗示我没有使用 Azure。我知道一些同行正在做和我一样的事情,我听从了他们的建议并观看了他们自己的教程,但没有人能解决我遇到的问题。我祈祷有人能帮我指明正确的方向,因为我不知道该怎么做。

在提出任何评论之前:是的,我是 Node.JS 的新手,我只想部署一些东西,这样我才能知道它能正常工作。老实说,我正盲目地寻找一个可以在线工作的配置,即使通过本地主机也可以正常工作。不,我不能切换到 Node.JS 以外的其他东西,因为我在过去几天里已经投入了大量时间来尝试部署一些最基本的功能。

感谢您抽出时间来阅读。

相关内容