首页 > 解决方案 > 如何将预定义的管道构建参数传递给 Azure DevOps 管道中的模板?

问题描述

模板:

parameters:
- name: PathPrefix
  displayName: 'Path prefix'
  type: string
  default: ''

steps:
- task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: restore
    projects: ${{parameters.PathPrefix}}**/$(Build.DefinitionName).sln

管道:

resources:
  repositories:
  - repository: devops
    name: foo/devops
    type: git
    ref: master

trigger:
  branches:
    include:
    - refs/heads/*

jobs:
- job: Job_1
  displayName: Agent job 1
  pool:
    vmImage: windows-latest
  steps:
  - checkout: self
  - template: azure/pipelines/pipeline.yaml@devops
    parameters:
      PathPrefix: $(Build.DefinitionName)

在还原步骤中运行错误:

##[error]No files matched the search pattern.

即使设置verbosityRestore: detailed恢复步骤也不会给我更多信息。

如果我不设置PathPrefix,它似乎使用默认的空字符串并找到解决方案文件(在某些情况下)。但是,如果我确实设置了某些存储库中需要的前缀,它就找不到该文件。我尝试了各种在模板(${{}}$()$[]其他)中引用参数的方法以及在管道中指定参数的不同方法,包括硬编码路径(尽管我想改用变量),但没有任何效果。

我想也许变量会起作用,所以我也尝试在管道中指定变量并在模板中使用它们,但这会导致同样的错误。在模板中定义变量给了我一个模板的编译错误(意外的标记“变量”或类似的东西)。

标签: azure-devopsazure-pipelinesazure-pipelines-yaml

解决方案


看看你正在传递什么,并在心里扩展结果。

${{parameters.PathPrefix}}**/$(Build.DefinitionName).sln

如果Build.DefinitionNamefoo,并且你将它传递给 as PathPrefix,那么你得到的是:

foo**/foo.sln

看起来你想要一个额外的正斜杠,所以你得到foo/**/foo.sln.


推荐阅读