【问题标题】:How to set up build and release pipeline for NestJS application to Azure using YAML如何使用 YAML 设置 NestJS 应用程序到 Azure 的构建和发布管道
【发布时间】:2020-09-16 01:16:25
【问题描述】:

我无法理解和获取将 NestJS 应用程序部署到 Azure DevOps (ADO) 的构建/发布管道设置。

我正在部署到托管在 Azure 中的 Linux Web 应用程序。

据我了解,如果我使用 npm run start 之类的东西在本地运行应用程序,它会在我的项目根目录下创建一个 dist 文件夹。

因此,在为构建和部署编写 YAML 时。我的思考过程是:

  1. 运行 NPM 更新。
  2. 运行npm run build 以构建应用程序并生成dist 文件夹。
  3. 将应用程序的内容(或只是 dist 文件夹?)复制到目标文件夹 (/home/site/wwwroot)
  4. 运行npm run start:prod 启动服务器。

到目前为止,这是我的 YAML:

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

steps:

- task: UseNode@1
  inputs:
    version: '14.x'
    checkLatest: true

- task: Npm@0
  displayName: Run NPM Update for NestJS
  inputs:
    cwd: '$(Build.SourcesDirectory)/ProjectName'
    command: update

- task: Npm@0
  displayName: Build NestJS
  inputs:
    cwd: '$(Build.SourcesDirectory)/ProjectName'
    command: run
    arguments: "build"

- task: CopyFiles@2
  inputs:
    Contents: 'dist/**'
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

问题是在构建过程完成后,我在/home/site/wwwroot/ProjectName 中看不到dist 文件夹。有人可以帮我解决我所缺少的吗?

另外,关于 Azure DevOps 的一个小白问题,$(Build.SourcesDirectory)$(Build.ArtifactStagingDirectory) 指的是什么以及这些环境变量是如何以及在哪里设置的?

【问题讨论】:

    标签: node.js azure-devops azure-pipelines nestjs azure-pipelines-release-pipeline


    【解决方案1】:

    将您的应用部署到托管在 Azure 中。您需要使用Azure App Service Deploy taskAzure Web App task

    Azure devops 是构建应用程序并将其部署到服务器的工具(例如 Azure 上的 Linux Web 应用程序),它不适用于托管您的应用程序。

    $(Build.ArtifactStagingDirectory) 指的是运行管道的代理机器的文件夹。 (当您运行管道时,它会选择pool 中定义的代理来运行您的管道任务)

    代理机器中文件夹的映射如下图所示。更多信息请查看predefined variables

    $(Agent.BuildDirectory) 映射到 c:\agent_work\1

    $(Build.ArtifactStagingDirectory) 映射到 c:\agent_work\1\a

    $(Build.BinariesDirectory) 映射到 c:\agent_work\1\b

    $(Build.SourcesDirectory) 映射到 c:\agent_work\1\s

    那么回到如何将 NestJS 应用程序部署到 Azure 的问题?

    首先,您需要在 Azure devops 中创建一个服务连接以连接到您的 azure 订阅。详细步骤请查看here

    然后将Azure App Service Deploy task/Azure Web App task 添加到管道的末尾。见下例:

    - task: AzureRmWebAppDeployment@4
      inputs:
        ConnectionType: 'AzureRM'
        azureSubscription: 'SubscriptionServiceConnectionName'
        appType: 'webAppLinux'
        WebAppName: 'MyWebAppName'
        Package: '$(Build.ArtifactStagingDirectory)/dist/'
        StartupCommand: 'npm run start:prod'
    

    您可以查看here了解更多信息。

    【讨论】:

      猜你喜欢
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多