skip to Main Content

I am executing a single pipeline from 2 different projects. Repo1 and Repo2.
Repo2 is a submodule in Repo1.

In Repo 1 I am calling this:

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

stages:
- template: tool.yml@pl

However, the main YAML in repo 2 contains code which is executing as such

stages:

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

the main issue is in git_control Template

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

Because the argument of BuildRepositoryLocalPath is different when executing from different areas. I need to change it according to what project is. when executing from the repo2 It will execute fine.
However when executing from repo1, %Build_Repository_LocalPath%\script.py needs to be

%Build_Repository_LocalPath%pipelinessupportrepo2script.py

I understand that a may have to manually write pipelines\support\repo2

But i can not get the passing of variables correct depending on the System.TeamProject which is the environmental variable I am using to distinguish between the 2 repos.

Tried Variables and Parameters. I think this is the way to go but cant get the syntax right.

2

Answers


  1. As per System Variables, System.TeamProject is available in templates.

    This means you can use a condition like ${{ if ... }} to set a variable with the right path for the project being used.

    Example:

    variables:
      - ${{ if eq(variables['System.TeamProject'], 'project1') }}: # replace with proper project name
        - name: 'scriptPath'
          value: '$(Build.Repository.LocalPath)pipelinessupportrepo2script.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 "$(scriptPath)"
        displayName: 'Run Python script using CmdLine task'
    
      # as an alternative, use script task
      - script: |
          python "$(scriptPath)"
        displayName: 'Run Python Script using script task'
    
    Login or Signup to reply.
  2. The predefined variable "System.TeamProject" is not to get the name of the repository. It is to get the name of team project which the pipeline is running in.

    For example, a URL of Git repository in Azure DevOps is "https://dev.azure.com/{organization}/{project}/_git/{repository}", when running a pipeline for the {repository} in {project}, the variable "System.TeamProject" will return the name of ‘{project}‘ instead of ‘{repository}‘.

    For your case, if Repo1 and Repo2 are in the same team project, it is not possible to use the variable "System.TeamProject" to identify the pipeline is running for which repository. Instead, you can use the predefined variable "Build.Repository.Name" to identify the repository.

    When a pipeline is triggered by Repo1, it will the value of "Build.Repository.Name" is the name of Repo1. When a pipeline is triggered by Repo2, the value is name of Repo2.

    So, in the "templates/git_control.yml", you can set the condition like as below.

    steps:
    - task: CmdLine@1
      condition: eq(variables['Build.Repository.Name'], 'Repo2')
      inputs:
        filename: call
        arguments: '%PYTHON_HOME%python $(Build.Repository.LocalPath)script.py'
    
    - task: CmdLine@1
      condition: ne(variables['Build.Repository.Name'], 'Repo2')
      inputs:
        filename: call
        arguments: '%PYTHON_HOME%python $(Build.Repository.LocalPath)pipelinessupportrepo2script.py'
    

    With above configuration, regardless of whether Repo1 and Repo2 are in the same project, when the pipeline is running for Repo2, the first CmdLine@1 task runs and the second gets skipped. Otherwise the first gets skipped and the second runs.


    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search