将 Azure DevOps CI/CD 用于 Linux Web 应用程序时,如何将构建工件压缩并发布为 zip 文件而不是复制到目录

When using Azure DevOps CI/CD for a Linux web app how can I zip and publish the build artifact as a zip file only instead of copying to directory

Per these instructions 我正在为 Linux Web 应用程序创建集成 CI/CD 的 devops 项目。

这是我最初的管道 pipelines/build 工作

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

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

- script: |
    npm install -g @angular/cli
    npm install
    npm run build:ssr
  displayName: 'npm install and build'

- task: CopyFiles@2
  inputs:
    Contents: |
      dist/** 
      node_modules/**
      package.json
    TargetFolder: '$(Build.ArtifactStagingDirectory)'
    CleanTargetFolder: true
    OverWrite: true

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

当因为CopyFiles@2任务有65000个文件拷贝到目录下要在下面的PublishBuildArtifacts@1任务中发布。

因此,我想我可以在复制任务之后添加 Artifact 任务,这将生成一个 .zip 文件,并使部署更高效,速度更快。

然而,情况并非如此,它所做的只是将文件夹压缩,解压缩文件夹并继续进行相同的单个文件处理,如下所示。在我写这个 post 的时候,已经有大约 14 分钟了,这就是它已经走了多远。

Async Command Start: Upload Artifact
Uploading 65244 files
Total file: 65244 ---- Processed file: 69 (0%)
Total file: 65244 ---- Processed file: 200 (0%)
Total file: 65244 ---- Processed file: 333 (0%)
Total file: 65244 ---- Processed file: 460 (0%)
Total file: 65244 ---- Processed file: 603 (0%)
Total file: 65244 ---- Processed file: 746 (1%)
Total file: 65244 ---- Processed file: 884 (1%)
Total file: 65244 ---- Processed file: 1026 (1%)
Total file: 65244 ---- Processed file: 1142 (1%)
Total file: 65244 ---- Processed file: 1268 (1%)
Total file: 65244 ---- Processed file: 1365 (2%)
Total file: 65244 ---- Processed file: 1509 (2%)
Total file: 65244 ---- Processed file: 1672 (2%)
Total file: 65244 ---- Processed file: 1839 (2%)
Total file: 65244 ---- Processed file: 1993 (3%)
Total file: 65244 ---- Processed file: 2188 (3%)
Total file: 65244 ---- Processed file: 2362 (3%)
Total file: 65244 ---- Processed file: 2540 (3%)
Total file: 65244 ---- Processed file: 2701 (4%)
Total file: 65244 ---- Processed file: 2854 (4%)
Total file: 65244 ---- Processed file: 3051 (4%)
Total file: 65244 ---- Processed file: 3256 (4%)
Total file: 65244 ---- Processed file: 3459 (5%)
Total file: 65244 ---- Processed file: 3656 (5%)
Total file: 65244 ---- Processed file: 3836 (5%)
Total file: 65244 ---- Processed file: 4025 (6%)
Total file: 65244 ---- Processed file: 4196 (6%)
Total file: 65244 ---- Processed file: 4385 (6%)
Total file: 65244 ---- Processed file: 4545 (6%)
Total file: 65244 ---- Processed file: 4694 (7%)
Total file: 65244 ---- Processed file: 4864 (7%)
Total file: 65244 ---- Processed file: 5044 (7%)
Total file: 65244 ---- Processed file: 5253 (8%)
Total file: 65244 ---- Processed file: 5422 (8%)
Total file: 65244 ---- Processed file: 5620 (8%)

有什么方法可以满足我的需求吗?我不应该复制然后压缩而只是压缩吗?这是因为它是 Linux 途径与 Windows 途径。

到目前为止,这将无法使用,因为部署需要一个小时。

这是Deploy Azure App Service任务

的任务
steps:
- task: AzureRmWebAppDeployment@4
  displayName: 'Deploy Azure App Service'
  inputs:
    azureSubscription: '$(Parameters.ConnectedServiceName)'
    appType: '$(Parameters.WebAppKind)'
    WebAppName: '$(Parameters.WebAppName)'
    packageForLinux: '$(System.DefaultWorkingDirectory)/**/**/*.zip'
    RuntimeStack: 'NODE|12-lts'
    StartupCommand: '$(Parameters.StartupCommand)'

是的,您可以将特定文件夹存档到 zip、7z 或 tar 存档中。然后,您可以仅将存档发布为管道中的工件。下面是将 distnode_modulespackage.json 内容存档到 zip,然后将其作为工件发布的示例。

此处重要的一点是 replaceExistingArchive: false 存档任务,这会将每个 folder/file 添加到存档中。一个例外是,如果您存档到压缩的 TAR 文件,这些文件总是被完全替换。 (Source)

此外,Artifact 任务只是发布文件,以便在管道外访问它们,它不会将它们压缩到存档中。

steps:
- task: ArchiveFiles@2
  displayName: 'Archive Dist Folder'
  inputs:
    rootFolderOrFile: 'dist'
    includeRootFolder: false
    archiveFile: '$(Build.ArtifactStagingDirectory)/archive.zip'
    replaceExistingArchive: false
- task: ArchiveFiles@2
  displayName: 'Archive node_modules Folder'
  inputs:
    rootFolderOrFile: 'node_modules'
    includeRootFolder: false
    archiveFile: '$(Build.ArtifactStagingDirectory)/archive.zip'
    replaceExistingArchive: false
- task: ArchiveFiles@2
  displayName: 'Archive package.json File'
  inputs:
    rootFolderOrFile: 'package.json'
    includeRootFolder: false
    archiveFile: '$(Build.ArtifactStagingDirectory)/archive.zip'
    replaceExistingArchive: false
- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)/archive.zip'
    ArtifactName: 'drop'
    publishLocation: 'Container'

我想更新此答案以包括我在 2020 年 4 月 17 日起有效的最终解决方案。请记住,云中的事情总是在变化 space 但截至目前,这是一个完整的解决方案。

  1. 以下是我的管道构建步骤:
steps:
- task: NodeTool@0
  inputs:
    versionSpec: '12.x'
  displayName: 'Install Node.js'

- script: |
    npm install -g @angular/cli
    npm install
    npm run build:ssr
  displayName: 'npm install and build'

- task: ArchiveFiles@2
  displayName: 'Archive dist Folder'
  inputs:
    rootFolderOrFile: 'dist'
    includeRootFolder: true
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/archive.zip'
    replaceExistingArchive: false

- task: ArchiveFiles@2
  displayName: 'Archive node_modules Folder'
  inputs:
    rootFolderOrFile: 'node_modules'
    includeRootFolder: true
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/archive.zip'
    replaceExistingArchive: false

- task: ArchiveFiles@2
  displayName: 'Archive package.json File'
  inputs:
    rootFolderOrFile: 'package.json'
    includeRootFolder: false
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/archive.zip'
    replaceExistingArchive: false

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)/archive.zip'
    ArtifactName: 'drop'
    publishLocation: 'Container'
  1. 这是我在使用任务 Deploy Azure App Service 时在管道的发布部分中的部署步骤。
steps:
- task: AzureRmWebAppDeployment@4
  displayName: 'Deploy Azure App Service'
  inputs:
    azureSubscription: '$(Parameters.ConnectedServiceName)'
    appType: '$(Parameters.WebAppKind)'
    WebAppName: '$(Parameters.WebAppName)'
    packageForLinux: '$(System.DefaultWorkingDirectory)/**/**/*.zip'
    RuntimeStack: 'NODE|12-lts'
    StartupCommand: '$(Parameters.StartupCommand)'

出于某种原因,yaml 说我有很多未定义的系统变量,但事实并非如此,因为 A. 它有效,B. 我相信它们来自您的订阅和服务器系统。

最后确保为您的快速服务器或通用 angular 应用服务器实现创建正确的 PM2 start 命令。这是在 azure 门户中找到的 npm run serve:ssr

记住使用 npm 运行 是针对 Linux 服务器的,因为 Microsoft Windows 使用节点 运行