【问题标题】:How to include PS code with shared YAML templates in Azure DevOps Server 2020?如何在 Azure DevOps Server 2020 中包含带有共享 YAML 模板的 PS 代码?
【发布时间】:2021-05-15 11:23:06
【问题描述】:

我们在 YAML 构建中广泛使用共享 YAML 模板。像这样的:

trigger: none

resources:
  repositories:
    - repository: templates
      type: git
      name: DFDevOps\cicd-templates
      ref: refs/tags/stable-1
      # http://tdc1tfsapp01:8080/tfs/DefaultCollection/DFDevOps/_git/cicd-templates

name: $(BuildVersionPrefix).$(DayOfYear)$(Date:HH)

jobs:
  - job: Build
    pool:
      demands: DotNetFramework
    workspace:
      clean: outputs
    timeoutInMinutes: 180
    steps:
      - checkout: self # self represents the repo where the initial Pipelines YAML file was found
        clean: true
        lfs: true

      - template: ci/ci-build.yml@templates
        parameters:
          releaseBranch: $(ReleaseBranch)

但是,共享 YAML 模板通常需要执行 Powershell 代码。到目前为止,我们要么将其嵌入,要么将其放入模板加载的 PS 模块中。

我讨厌这两种方法。我讨厌嵌入,因为:

  1. 没有智能感知
  2. 没有单元测试
  3. 没有正确报告管道故障的错误

我讨厌放入一个模块,因为它与原点分离并且需要大量开销。

我的问题 - 是否可以将 PS 代码放在与 YAML 模板相同的存储库中的专用 ps1 文件中,并在运行时提供它们?

【问题讨论】:

  • 这个问题怎么样?下面的答案是否解决了您的问题,如果没有,请告诉我有关此问题的最新信息吗?
  • 我还没查。但我会回来的。

标签: azure-devops yaml azure-devops-server azure-devops-server-2020


【解决方案1】:

因此,checkout: self 将引用该特定管道 YAML 所在的存储库 - 而不是模板存储库(除非它们是相同的)。

我将添加第二个签出步骤 - checkout: templates - 在管道中,然后在您的 powershell 脚本任务的输入中(在模板中),指定您刚刚签出的脚本的路径:

        scriptType: filePath
        scriptPath: $(Build.SourcesDirectory)/templates/pathToscript.ps1

模板只是在编译时就地展开到管道中,因此它应该可以访问该文件夹。您现在可以通过下载扩展的管道在 YAML 管道编辑器中验证这一点:Download full YAML

【讨论】:

  • 谢谢。我会测试它。你能想出一种不触及最顶层 YAML 的方法吗?因为我们将不得不修改很多 YAML。也许模板存储库已经在某处可用?毕竟,它的 YAML 已经在使用中了……
  • 我想该模板可以包含自己的结帐步骤,可能值得一试。
  • 非常有趣的想法。我得检查一下。
猜你喜欢
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
  • 2022-10-24
  • 2021-06-28
  • 2021-01-31
  • 2021-10-14
  • 1970-01-01
  • 2021-05-14
相关资源
最近更新 更多