如何使用 YAML 为 NestJS 应用程序设置构建和发布管道到 Azure

How to set up build and release pipeline for NestJS application to Azure using YAML

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

我正在部署到 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) 指的是什么以及这些环境变量的设置方式和位置?

将您的应用程序部署到 Azure 中托管。您需要使用 Azure App Service Deploy task or Azure Web App task.

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

$(Build.ArtifactStagingDirectory) 指的是 运行 是您的管道的代理计算机的文件夹。 (当你的 运行 你的管道时,它会选择一个在 pool 中定义的代理到 运行 你的管道任务)

到代理计算机中文件夹的映射如下图所示。查看 predefined variables 了解更多信息。

$(Agent.BuildDirectory) is mapped to c:\agent_work

$(Build.ArtifactStagingDirectory) is mapped to c:\agent_work\a

$(Build.BinariesDirectory) is mapped to c:\agent_work\b

$(Build.SourcesDirectory) is mapped to c:\agent_work\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 了解更多信息。