如何使用模板引用Azure DevOps管道中另一个存储库中的脚本?

如何使用模板引用Azure DevOps管道中另一个存储库中的脚本?,azure,azure-devops,azure-devops-pipelines,Azure,Azure Devops,Azure Devops Pipelines,我正在尝试使用一个存储在Azure DevOps中另一个Git存储库中的模板,该模板引用了该存储库中也包含的脚本。虽然我可以从主管道成功使用模板,但它没有引用模板所需的脚本 有什么办法可以帮你吗 HelloWorld模板/脚本存储在名为“模板”的子文件夹中的Azure DevOps Repo“HelloWorld”中 HelloWorld.ps1脚本位于与以下模板相同的目录中 param($Name) Write-Host "Hello, $Name!" 模板#1参考本地

我正在尝试使用一个存储在Azure DevOps中另一个Git存储库中的模板,该模板引用了该存储库中也包含的脚本。虽然我可以从主管道成功使用模板,但它没有引用模板所需的脚本

有什么办法可以帮你吗

HelloWorld模板/脚本存储在名为“模板”的子文件夹中的Azure DevOps Repo“HelloWorld”中

HelloWorld.ps1脚本位于与以下模板相同的目录中

param($Name)

Write-Host "Hello, $Name!"
模板#1参考本地脚本

参数:
-姓名:azureSubscription
类型:字符串
-姓名:姓名
类型:字符串
工作:
-工作:HelloWorld
显示名称:你好,世界
变量:
名称:${parameters.name}
步骤:
-任务:AzurePowerShell@4
姓名:HelloWorld
显示名称:你好,世界
投入:
azureSubscription:${{parameters.azureSubscription}}
脚本类型:文件路径
脚本路径:./HelloWorld.ps1
azurePowerShellVersion:最新版本
故障标准错误:正确
脚本参数:
-名称“$(名称)”
模板#2


资源:
存储库:
-存储库:helloworld
类型:git
姓名:HelloWorld
阶段:
-阶段:变量
工作:
-模板:Scripts/helloworld。yml@helloworld
参数:
azureSubscription:“我的订阅”
名字:“比利”

如果您的管道在另一个存储库中有模板,或者如果您希望与需要服务连接的存储库一起使用,则必须让系统知道该存储库。repository关键字用于指定外部存储库

有关详细信息,请参阅和

例如在模板库中,我们希望在根目录中找到test.yml

resources:
  repositories:
  - repository: templates
    type: git
    name: templates
steps:
- template: test.yml@templates
更新1

我更新了你的代码,它工作了,请检查它

repo HelloWord结构和repo包含模板文件和PS1文件

模板#1参考本地脚本。 注意:我添加了签出步骤来签出HelloWord和管道自回购

  parameters:
  - name: azureSubscription
    type: string
  - name: name
    type: string
  
  jobs:
  - job: HelloWorld
    displayName: Hello World
    variables:
      name: ${{ parameters.name }}
    steps:
    - checkout: self
    - checkout: helloworld
    - task: AzurePowerShell@4
      name: HelloWorld
      displayName: Hello World
      inputs:
        azureSubscription: ${{ parameters.azureSubscription }}
        scriptType: filePath
        scriptPath: $(build.sourcesdirectory)/Helloworld/Subfolder/HelloWorld.ps1
        azurePowerShellVersion: latestVersion
        failOnStandardError: true
        scriptArguments:
          -Name "$(name)"
在另一个repo中创建yaml构建定义

resources:
  repositories:
    - repository: helloworld
      type: git
      name: HelloWorld
pool:
  vmImage: 'windows-latest'
 
stages:
 
- stage: Variables
  jobs:
  - template: hello-world.yml@helloworld
    parameters:
      azureSubscription: 'My Subscription'
      name: 'Billy'
结果:


如果您的管道在另一个存储库中有模板,或者如果您希望与需要服务连接的存储库一起使用,则必须让系统知道该存储库。repository关键字用于指定外部存储库

有关详细信息,请参阅和

例如在模板库中,我们希望在根目录中找到test.yml

resources:
  repositories:
  - repository: templates
    type: git
    name: templates
steps:
- template: test.yml@templates
更新1

我更新了你的代码,它工作了,请检查它

repo HelloWord结构和repo包含模板文件和PS1文件

模板#1参考本地脚本。 注意:我添加了签出步骤来签出HelloWord和管道自回购

  parameters:
  - name: azureSubscription
    type: string
  - name: name
    type: string
  
  jobs:
  - job: HelloWorld
    displayName: Hello World
    variables:
      name: ${{ parameters.name }}
    steps:
    - checkout: self
    - checkout: helloworld
    - task: AzurePowerShell@4
      name: HelloWorld
      displayName: Hello World
      inputs:
        azureSubscription: ${{ parameters.azureSubscription }}
        scriptType: filePath
        scriptPath: $(build.sourcesdirectory)/Helloworld/Subfolder/HelloWorld.ps1
        azurePowerShellVersion: latestVersion
        failOnStandardError: true
        scriptArguments:
          -Name "$(name)"
在另一个repo中创建yaml构建定义

resources:
  repositories:
    - repository: helloworld
      type: git
      name: HelloWorld
pool:
  vmImage: 'windows-latest'
 
stages:
 
- stage: Variables
  jobs:
  - template: hello-world.yml@helloworld
    parameters:
      azureSubscription: 'My Subscription'
      name: 'Billy'
结果:


当您的管道中有多个存储库,并且当您使用另一个repo中的模板时,它们会被提取到自己的文件夹中,而不是根文件夹中

如果作业中有多个签出步骤,则源代码将签出到以存储库命名的目录中,作为(Agent.BuildDirectory)中的s的子文件夹。如果(Agent.BuildDirectory)是C:\Agent\u work\1,并且您的存储库命名为tools和code,那么您的代码将签出到C:\Agent\u work\1\s\tools和C:\Agent\u work\1\s\code

因此,当您在另一个repo中的模板内调用脚本时,您可能会被迫为repo提供别名,以便正确定位脚本

例如:

trigger: none
pr: none

resources:
  repositories:
    - repository: devops
      type: github
      name: kmadof/devops-templates
      endpoint: kmadof

stages:
- template: templates/start.yaml@devops
  parameters:
    repo: devops-templates
    buildSteps:
      - checkout: self
      - checkout: devops
      - bash: echo Test #Passes
        displayName: succeed
      - bash: echo "Test"
        displayName: succeed
我正在传递repo参数以在我的模板中使用它:

参数:
-名称:repo#未指定参数的默认值
默认值:“”
步骤:
-任务:PowerShell@2
投入:
文件路径:${parameters.repo}}/scripts/myscript.ps1

当您的管道中有多个存储库时,您可以检查代码,当您使用另一个repo中的模板时,您可以检查代码,这些存储库将被提取到自己的文件夹中,而不是根文件夹中

如果作业中有多个签出步骤,则源代码将签出到以存储库命名的目录中,作为(Agent.BuildDirectory)中的s的子文件夹。如果(Agent.BuildDirectory)是C:\Agent\u work\1,并且您的存储库命名为tools和code,那么您的代码将签出到C:\Agent\u work\1\s\tools和C:\Agent\u work\1\s\code

因此,当您在另一个repo中的模板内调用脚本时,您可能会被迫为repo提供别名,以便正确定位脚本

例如:

trigger: none
pr: none

resources:
  repositories:
    - repository: devops
      type: github
      name: kmadof/devops-templates
      endpoint: kmadof

stages:
- template: templates/start.yaml@devops
  parameters:
    repo: devops-templates
    buildSteps:
      - checkout: self
      - checkout: devops
      - bash: echo Test #Passes
        displayName: succeed
      - bash: echo "Test"
        displayName: succeed
我正在传递repo参数以在我的模板中使用它:

参数:
-名称:repo#未指定参数的默认值
默认值:“”
步骤:
-任务:PowerShell@2
投入:
文件路径:${parameters.repo}}/scripts/myscript.ps1

您可以检查代码

我们也是这样做的。但是,非常不幸的是,您被迫在根管道中为存储库资源使用特定的名称(即,与您在模板的签出步骤中使用的名称完全相同)。事实上,我遇到这个问题是因为我正在寻找一种方法,从模板中找出使用了哪个存储库资源(或者至少是哪个分支引用)来访问模板。我们也是这样做的。但是,非常不幸的是,您被迫在根管道中为存储库资源使用特定的名称(即,与您在模板的签出步骤中使用的名称完全相同)。事实上,我遇到这个问题是因为我正在寻找一种方法,从模板中找出使用了哪个存储库资源(或者至少是哪个分支引用)