如何将特定的 folders/files 复制到神器?

How to copy specific folders/files to artifact?

我有以下构建管道:

pool:
  name: Azure Pipelines
  demands:
  - npm
  - msbuild

steps:
- task: Npm@1
  displayName: 'npm install'
  inputs:
    workingDir: Project123/Angular
    verbose: false
    
steps:
- task: Npm@1
  displayName: 'npm custom: angular build'
  inputs:
    command: custom
    workingDir: Project123/Angular
    verbose: false
    customCommand: 'run-script build --prod --extractCss'

steps:
- task: NuGetCommand@2
  displayName: 'NuGet restore'
  
steps:
- task: MSBuild@1
  displayName: '.Net build'
  inputs:
    solution: 'Project123/*.csproj'
    msbuildArchitecture: x64
    configuration: Release
    msbuildArguments: '/p:OutputPath=$(Build.ArtifactStagingDirectory)'

- task: CopyFiles@2
  displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
  inputs:
    SourceFolder: Project123/Bundles
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: Release'
  inputs:
    ArtifactName: Release

当我们从 VS 手动构建项目时,这是我们部署的(预期的)工件:

但是,我的 YAML 生成了这个工件:

如何完成预期的工件?

我真的很惊讶 AngularOutput 被复制到工件的根目录,而不是 Bundles...我在复制任务中指定复制 Bundles 文件夹,其中包含角度输出...

由于我不知道.csproj配置,只是根据截图提供一个解决方法。

Don't need this Angular folder in this artifact

These Web.Debug and Web.Release are unneeded in the artifact

添加任务权限shell并通过以下脚本删除文件夹和文件

#Delete Angular folder and files
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      Remove-Item ''$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Angular''      
      Remove-Item ''$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Web.Debug.configure''    
      Remove-Item ''$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Web.Release.configure''

Those dlls, .pdb, .xml and .configure files are not needed here.

There needs to be a 'Bundles' directory here which is generated by the angular build task

勾选这个复制文件任务,这些文件在文件夹Project123/Bundles里,对吧?

- task: CopyFiles@2
  displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
  inputs:
    SourceFolder: Project123/Bundles
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

我们需要更改复制文件任务如下:

- task: CopyFiles@2
  displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
  inputs:
    SourceFolder: Project123/Bundles
    TargetFolder: '$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Bundles'

它会将这些文件保存在文件夹 Bundles 而不是根目录中。

This AngularOutput folder needs to be located under 'Bundles' directory inside 'Project123' folder above

我们可以将文件夹复制到 project123 文件夹,然后发布工件。

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(Build.ArtifactStagingDirectory)/AngularOutput'
    Contents: '**'
    TargetFolder: '$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/AngularOutput'

您可以 运行 在 YAML 下构建并检查结果。

pool:
  name: Azure Pipelines
  demands:
  - npm
  - msbuild

steps:
- task: Npm@1
  displayName: 'npm install'
  inputs:
    workingDir: Project123/Angular
    verbose: false
    
- task: Npm@1
  displayName: 'npm custom: angular build'
  inputs:
    command: custom
    workingDir: Project123/Angular
    verbose: false
    customCommand: 'run-script build --prod --extractCss'

- task: NuGetCommand@2
  displayName: 'NuGet restore'
  
- task: MSBuild@1
  displayName: '.Net build'
  inputs:
    solution: 'Project123/*.csproj'
    msbuildArchitecture: x64
    configuration: Release
    msbuildArguments: '/p:OutputPath=$(Build.ArtifactStagingDirectory)'

- task: CopyFiles@2
  displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
  inputs:
    SourceFolder: Project123/Bundles
    TargetFolder: '$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Bundles'

#Delete Angular folder and files
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      Remove-Item ''$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Angular''      
      Remove-Item ''$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Web.Debug.configure''    
      Remove-Item ''$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Web.Release.configure''

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(Build.ArtifactStagingDirectory)/AngularOutput'
    Contents: '**'
    TargetFolder: '$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/AngularOutput'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: Release'
  inputs:
    ArtifactName: Release

更新1

如果问题是删除文件夹和文件,请按如下更新 power shell 脚本并重试。

#Delete Angular folder and files
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      Remove-Item -Path $(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Angular -Recurse -Force     
      Remove-Item -Path $(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Web.Debug.configure -Recurse -Force  
      Remove-Item -Path $(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Web.Release.configure -Recurse -Force

而测试结果: