YAML Azure Pipelines 根据构建项目动态传递文件路径变量

问题描述 投票:0回答:1

我正在执行两个不同项目的单个管道。回购协议 1 和回购协议 2。 Repo2 是 Repo1 中的子模块。

在 Repo 1 中我称之为:

resources:
  repositories:
    - repository: pl
      type: git
      name: ###

stages:
- template: tool.yml@pl

但是,repo 2 中的主要 YAML 包含这样执行的代码

stages:

- stage: build_prj
  jobs:
  - job: config
    steps:
    - template: templates/git_control.yml

主要问题在 git_control 模板中

- task: CmdLine@1
  inputs:
    filename: Call
    arguments: '%PYTHON_HOME%\python %Build_Repository_LocalPath%\script.py'

因为从不同区域执行时

BuildRepositoryLocalPath
的参数是不同的。我需要根据项目进行更改。当从 repo2 执行时它会执行得很好。 然而,当从 repo1 执行时,
%Build_Repository_LocalPath%\\script.py
需要是

%Build_Repository_LocalPath%\pipelines\support\repo2\script.py

我知道a可能需要手动编写

pipelines\\support\\repo2

但是我无法根据

System.TeamProject
(我用来区分两个存储库的环境变量)来正确传递变量。

尝试变量和参数。我认为这是正确的方法,但无法获得正确的语法。

azure azure-devops yaml azure-pipelines azure-pipelines-yaml
1个回答
0
投票

根据系统变量

System.TeamProject
在模板中可用,因此您可以使用
${{ if ... }}
之类的条件来设置具有正确路径的变量,具体取决于所使用的项目。

示例:

variables:
  - ${{ if eq(variables['System.TeamProject'], 'project1') }}: # replace with proper project name
    - name: 'scriptPath'
      value: '$(Build.Repository.LocalPath)\pipelines\support\repo2\script.py'
  - ${{ if ne(variables['System.TeamProject'], 'project2') }}: # replace with proper project name
    - name: 'scriptPath'
      value: '$(Build.Repository.LocalPath)\script.py'

steps:
  - checkout: self

  - task: UsePythonVersion@0
    inputs:
      versionSpec: '3.x'  # Specify the Python version you need
      addToPath: true
  
  - task: CmdLine@1
    inputs:
      filename: Call
      arguments: |
        %PYTHON_HOME%\python "$(scriptPath)"
    displayName: 'Run Python script using CmdLine task'

  # as an alternative, use script task
  - script: |
      python "$(scriptPath)"
    displayName: 'Run Python Script using script task'
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.