如何将特定文件夹/文件复制到工件?

时间:2021-04-01 20:17:16

标签: azure-devops azure-pipelines azure-pipelines-tasks

我有以下构建管道:

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 构建项目时,这是我们部署的(预期的)工件:

expected

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

result

我如何完成预期的工件?

我实际上很惊讶 AngularOutput 被复制到工件的根目录中,而不是 Bundles...我在复制任务中指定复制 Bundles 文件夹,其中将包含 AngularOutput。 ..

1 个答案:

答案 0 :(得分:1)

由于我不知道 .csproj 的配置,仅提供基于屏幕截图的解决方法。

<块引用>

在这个神器中不需要这个 Angular 文件夹

<块引用>

工件中不需要这些 Web.Debug 和 Web.Release

通过下面的脚本添加任务电源外壳并删除文件夹和文件

#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''

<块引用>

此处不需要那些 dll、.pdb、.xml 和 .configure 文件。

<块引用>

这里需要有一个'Bundles'目录,它是由angular build任务生成的

勾选这个Copy files任务,这些文件在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 而不是 root 中。

<块引用>

此 AngularOutput 文件夹需要位于上面“Project123”文件夹内的“Bundles”目录下

我们可以将文件夹复制到 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

和测试结果:

enter image description here