将其他文件添加到 Azure Build Pipeline

Adding additional files to Azure Build Pipeline

我有一个 Azure DevOps 管道,它从存储库中提取并构建一个 Visual Studio Web 项目。然后将其发布到应用服务。

我有几个敏感的配置文件,这些文件未包含在存储库(或 VS 项目)中,并且在 Azure DevOps 系统中存储为 'Secure Files'。

我需要将这些文件包含在发布包的 'Config/Secure' 文件夹中(在 zip 文件中)。我可以下载它们,但无论我尝试什么,我都无法将这些文件包含在部署 zip 文件中。它们只出现在 'drop' 文件系统中,因此我似乎无法将它们部署到 Web 应用程序。

有人知道我该怎么做吗?在此先感谢和下面的管道 YAML:

trigger:
- main

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: DownloadSecureFile@1
  inputs:
    secureFile: 'AppSettings.secret.config'

- task: DownloadSecureFile@1
  inputs:
    secureFile: 'cache.secret.config'

- task: DownloadSecureFile@1
  inputs:
    secureFile: 'security.secret.config'

- task: DownloadSecureFile@1
  inputs:
    secureFile: 'Smtp.secret.config'

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(Agent.TempDirectory)'
    Contents: |
      AppSettings.secret.config
      cache.secret.config
      security.secret.config
      Smtp.secret.config
    TargetFolder: '$(Build.ArtifactStagingDirectory)/config/secret'
    OverWrite: true
    flattenFolders: true

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(Build.ArtifactStagingDirectory)\"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

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

如果您的项目配置为使用它们,请将它们移动到源代码管理文件夹而不是 ArtifactStagingDirectory

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(Agent.TempDirectory)'
    Contents: |
      AppSettings.secret.config
      cache.secret.config
      security.secret.config
      Smtp.secret.config
    TargetFolder: '$(Build.ArtifactStagingDirectory)/config/secret'
    OverWrite: true
    flattenFolders: true

所以这是错误的,因为你将它直接移动到你发布的文件夹中,而 VSBuild 根本没有触及这个文件夹。

如果您在存储库的根目录中配置文件夹(并且您在这里只使用一个存储库),这应该将您的文件移动到解决方案中。

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(Agent.TempDirectory)'
    Contents: |
      AppSettings.secret.config
      cache.secret.config
      security.secret.config
      Smtp.secret.config
    TargetFolder: '$(System.DefaultWorkingDirectory)/config/secret'
    OverWrite: true
    flattenFolders: true

但请注意,不推荐发布秘密文件(即使作为工件)。请考虑在部署前下载它们并放在正确的位置。

I need to include these files in the 'Config/Secure' folder for the package that gets published (within the zip file)

我们无法将新文件直接添加到 zip 文件中。作为解决方法,我们可以提取文件夹 $(System.DefaultWorkingDirectory),将安全文件复制到 $(Build.ArtifactStagingDirectory)/PrescQIPPWebApp/config/secret 并压缩文件夹 $(Build.ArtifactStagingDirectory)/PrescQIPPWebApp,然后发布 Artifact。

另外,由于zip文件解压后不会被删除,压缩后PrescQIPPWebApp文件夹也不会被删除,所以我们需要添加power shell任务来删除zip文件和 PrescQIPPWebApp 文件夹

我已经更新了您的 YAML 构建定义,您可以尝试一下,并在此分享结果。

trigger:
- main

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(Build.ArtifactStagingDirectory)\"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

#Extract PrescQIPPWebApp.zip file to $(Build.ArtifactStagingDirectory)/PrescQIPPWebApp folder
- task: ExtractFiles@1
  inputs:
    archiveFilePatterns: '$(Build.ArtifactStagingDirectory)/PrescQIPPWebApp.zip'
    destinationFolder: '$(Build.ArtifactStagingDirectory)/PrescQIPPWebApp'
    cleanDestinationFolder: false
    overwriteExistingFiles: false

#Delete PrescQIPPWebApp.zip file
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Remove-Item ''$(Build.ArtifactStagingDirectory)/PrescQIPPWebApp.zip'''

- task: DownloadSecureFile@1
  inputs:
    secureFile: 'AppSettings.secret.config'

- task: DownloadSecureFile@1
  inputs:
    secureFile: 'cache.secret.config'

- task: DownloadSecureFile@1
  inputs:
    secureFile: 'security.secret.config'

- task: DownloadSecureFile@1
  inputs:
    secureFile: 'Smtp.secret.config'

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(Agent.TempDirectory)'
    Contents: |
      AppSettings.secret.config
      cache.secret.config
      security.secret.config
      Smtp.secret.config
    TargetFolder: '$(Build.ArtifactStagingDirectory)/PrescQIPPWebApp/config/secret'
    OverWrite: true
    flattenFolders: true

#Archive file to PrescQIPPWebApp.zip
- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: '$(Build.ArtifactStagingDirectory)/PrescQIPPWebApp'
    includeRootFolder: true
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/PrescQIPPWebApp.zip'
    replaceExistingArchive: true

- task: VSTest@2
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

#Delete PrescQIPPWebApp folder
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Remove-Item -path ''$(Build.ArtifactStagingDirectory)/PrescQIPPWebApp'' -Recurse -Force -EA SilentlyContinue -Verbose'

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