如何将文件从工件目录复制到 Azure 应用程序服务?

How to copy files from artifact directory to azure app service?

一点背景知识:

我已经为 .NET 创建了 azure CI/CD 管道,在 CI 步骤中有一个 gulp 到 css/js 的步骤。不知何故,我注意到在我生成的工件中 gulped css/js 不会出现。因此,根据 post 中的最后一个答案(Gulp-compiled CSS folder missing from the Azure DevOps pipeline Build Artifact ) 我设法找到生成的 css/js 的路径并将其复制到 $(Build.ArtifactStagingDirectory) (通过在 gulp 之后和发布工件任务之前添加复制文件任务)。如下面的屏幕截图所示,您可以看到标记为蓝色框的文件夹现在已复制到 $(Build.ArtifactStagingDirectory).

现在开始提问:

我认为在发布管道中,用于将其部署到 Azure 应用程序的工件是 "SampleWebApplication.zip"。现在,我不确定如何将我的 css/js 文件夹复制到 azure 应用程序,因为当我看到 azure 应用程序的站点文件夹时,资产文件夹中仍然没有这些 css 和 js 文件夹。

EDIT-1

在下面附上管道任务的屏幕截图:

还附加了 "Build Solution" 和 "Publish Artifact" 任务的 YAML

构建解决方案 YAML:-

#Your build pipeline references an undefined variable named ‘Parameters.solution’. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab. See https://go.microsoft.com/fwlink/?linkid=865972
#Your build pipeline references the ‘BuildPlatform’ variable, which you’ve selected to be settable at queue time. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab, and then select the option to make it settable at queue time. See https://go.microsoft.com/fwlink/?linkid=865971
#Your build pipeline references the ‘BuildConfiguration’ variable, which you’ve selected to be settable at queue time. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab, and then select the option to make it settable at queue time. See https://go.microsoft.com/fwlink/?linkid=865971

steps:
- task: VSBuild@1
  displayName: 'Build solution'
  inputs:
    solution: '$(Parameters.solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\"'
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'

发布工件 YAML

#Your build pipeline references an undefined variable named ‘Parameters.ArtifactName’. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab. See https://go.microsoft.com/fwlink/?linkid=865972

steps:
- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact'
  inputs:
    PathtoPublish: '$(build.artifactstagingdirectory)'
    ArtifactName: '$(Parameters.ArtifactName)'

您应该认识到发布的工件 drop 和生成的文件 SampleWebApplication.zip.

之间的区别

不确定您使用哪个任务部署到 Azure App Service,但据我所知,相关的部署任务总是需要 xx.zip 才能部署到 Web 应用程序。也就是说,deploy task只拿SampleWebApplication.zip,不关心published artifact

所以您应该做的是确保 SampleWebApplication.zip 包含 css/js 文件夹。部署任务只部署 xx.zip,它不会关心工件是否包含 css/js 文件夹。您应该关注如何在生成 xx.zip 时包含 css/js 文件夹,而不是使用复制任务将这些文件夹复制到 drop 工件中...

如果您正在配置 asp.net 核心项目,请查看 this document 以获得更多有用信息。我认为您应该在 dotnet publish 步骤之前或期间包括 css/js 文件夹,但不能在之后。

我设法找到了两个解决方法。该问题似乎特定于 "Build Solution" 任务。当构建到 $(Build.ArtifactStagingDirectory) 时,它不包括 gulp 生成的 CSS 和 JS 文件。但是作为 per this post on stack overflow 我将构建输出目录从 /p:PackageLocation="$(Build.ArtifactStagingDirectory) 更改为 /p:PackageLocation="$(Build.BinariesDirectory) 然后测试了

第一个解决方法

将“Publish artifact”任务的“Path to Publish”更改为 $(Build.BinariesDirectory) from $(Build.ArtifactStagingDirectory)还包括 gulped CSS 和 JS 文件。

第二种解决方法

  1. 我将“构建解决方案”任务的“发布路径”的参数从/p:PackageLocation="$(Build.ArtifactStagingDirectory)/p:PackageLocation="$(Build.BinariesDirectory),如第一个工作中提到的

  2. 创建任务“Archive $(Build.BinariesDirectory)”文件任务将文件从 $(build.binariesdirectory) 压缩到 $(Build.ArtifactStagingDirectory) 如下图所示:

  1. 创建“提取文件”任务以解压缩 $(Build.ArtifactStagingDirectory)/GeneratedArtifacts.zip 中存在的 .zip 文件,它将提取所有压缩的工件,如 .cmd 和 .zip 文件“发布任务”是必需的,出现在 $(Build.ArtifactStagingDirectory)GeneratedArtifacts.zip 中。就是这样!!

供您参考,第二种解决方法 GeneratedArtifacts.zip 包含最终文件,如下面的屏幕截图所示:

最后,对于第一个工作 - publish artifacts 任务将发布 $(Build.BinariesDirectory) 中存在的所有文件对于第二种解决方法 - publish artifacts 任务将发布 $(Build.ArtifactStagingDirectory)

中存在的所有文件

更准确地说,下面是第一个解决方法的 Publish Artifact 任务的屏幕截图:

下面是第二种解决方法的 Publish Artifact 任务的屏幕截图:

第一个变通办法似乎很干净并且由较少的构建管道步骤组成,所以我遵循了它,但也想写第二个变通办法,因为如果第一个变通办法对某人不起作用可能是第二个工作。

我可以看到您正在为 CI 使用 Azure DevOps。另一种解决方案是从 Web 根目录压缩文件,包括目录结构,并将它们作为 'secure file' 添加到 DevOps(配置 Azure KeyVault 后,在 library/secure 文件下找到它)。

然后添加管道任务以将安全文件(使用“下载安全文件”任务)下载到您的构建服务器。确保为此任务添加“引用名称”,以便您可以在后面的步骤中引用下载的文件路径。

然后添加独立的“Azure App Service Deploy”步骤以仅部署此 zip 文件。 Select“Zip 部署”的部署方法,并在“包或文件夹”部分引用您下载的安全文件,例如 $(secureFileReferenceName.secureFilePath)

这有效地将 zip 文件从安全存储下载到构建代理并将其解压缩到应用服务中的 wwwroot。

我希望有更简单的方法!