【问题标题】:How to make an Azure Function that was migrated from Javascript to Typescript build?如何构建从 Javascript 迁移到 Typescript 的 Azure Functions?
【发布时间】:2023-02-10 11:08:19
【问题描述】:

我有一个最初在 Javascript 中创建和部署的函数。
我将它本地迁移到 Typescript,并且可以正常运行。
但是,当它通过 Github 集成(构建提供者:应用服务构建服务)进行部署时,它不会构建项目。我已经检查过 Kudu,如果我去 wwwroot,dist 文件夹会丢失。

这是我的包 json:

{
  "name": "backup-function",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "build": "tsc --project tsconfig.build.json",
    "watch": "tsc -w",
    "prestart": "npm run build",
    "start": "func start --verbose",
    "test": "jest"
  },
  "engine": {
    "node": ">=16.0.0"
  },
  "dependencies": {
    "@azure/storage-blob": "^12.1.0",
    "durable-functions": "^2.1.0",
    ...
  },
  "devDependencies": {
    "@azure/functions": "^3.5.0",
    "@babel/preset-typescript": "^7.18.6",
    "@types/node": "16.x",
    "azure-functions-core-tools": "^4.0.4915",
    "jest": "^29.3.1",
    "ts-jest": "^29.0.3",
    "ts-node": "^10.9.1",
    "typescript": "^4.9.4"
    ...
  }
}

我的主机.json

{
  "version": "2.0",
  "extensions": {
    "durableTask": {
      "hubName": "backup"
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  },
  "functionTimeout": "00:10:00",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "maxTelemetryItemsPerSecond": 100
      }
    }
  }
}

【问题讨论】:

    标签: azure-functions azure-functions-runtime


    【解决方案1】:

    检查以下发现是否有助于解决问题:

    • 实际上,当您在 package.json 文件中使用此命令 npm run build 时,它应该创建 dist 文件夹并成功运行函数,如 GitHub Repo - Azure Functions TypeScript 主机问题中的类似问题 #4803 中所解决的那样。

    • 我怀疑你的建造package.json 中的命令可能会导致此处出现问题。 AFAIK,建造命令应该是tsc --project tsconfig.json

    • 尽管上述命令 npm run build 无法创建 dist 文件夹,请检查此 SO Answer #71846463 中关于同一问题的 @Olina04 给出的步骤和 GitHub Ticket #858

    【讨论】:

    • 实际上,我认为问题出在 c:homesitedeployments oolsdeploy.cmd 上,它正在运行 npm install --production 并且不包括构建步骤。
    【解决方案2】:

    我不得不用 Kudu 修改文件C:homesitedeployments oolsdeploy.cmd

    在第 121 行,我添加了这两行:

    call npm install --production :: below this line
    call npm install -D typescript
    call npm run build
    

    编辑:

    使用 Azure App Service 的部署速度非常慢,有时会超时,因此我还设法通过 github 操作对其进行了设置:

    name: Build and deploy Node.js project
    
    on:
      push:
        branches:
          - master
      workflow_dispatch:
    
    env:
      AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
      NODE_VERSION: '18.x' # set this to the node version to use (supports 8.x, 10.x, 12.x)
    
    jobs:
      build-and-deploy:
        runs-on: ubuntu-latest
        steps:
          - name: 'Checkout GitHub Action'
            uses: actions/checkout@v3
    
          - name: Setup Node ${{ env.NODE_VERSION }} Environment
            uses: actions/setup-node@v3
    
          - name: 'Resolve Project Dependencies Using Npm'
            shell: bash
            run: |
              pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'
              npm install
              npm run build --if-present
              npm run test --if-present
              npm prune --production
              popd
    
          - name: 'Deploy'
            uses: Azure/functions-action@v1
            id: some-id
            with:
              app-name: 'your-function-name'
              slot-name: 'Production'
              package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}
              publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE }}
    

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 2021-02-26
      • 2021-12-15
      • 2019-01-17
      • 2018-05-10
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多