skip to Main Content

I have a project in Azure that has many repositories. All of the repositories need to run with the same pipeline, so I’m looking for a way to define the repository name as a variable.

Here’s an example of what I’m trying to achieve:

trigger:
- master

pool:
  name: 'xxxx'

variables:
  repoName: 'repoA'

resources:
  repositories:
    - repository: repo
      type: git
      name: '50aa1c1f-21f0-4c61-8d85-d83218e705c9/$(repoName)'

steps:
- checkout: self
- checkout: repo

- script: echo Hello, world!
  displayName: 'Run a one-line script'

Does anyone have any idea how to get this to work?

2

Answers


  1. I have a project in Azure that has many repositories. All of the repositories need to run with the same pipeline, so I’m looking for a way to define the repository name as a variable.

    It’s not supported to use variable as the repository name in Yaml resource. Please check the doc below:

    enter image description here

    If you would like to checkout multiple repo, and define reponame in variable, as an alternative, you can use checkout task.

    - checkout: git://projectname/${{ variables.repoName}}@${{ variables.branchRef }}
    

    It’s mentioned in doc:
    enter image description here

    Please check similar ticket for the details.

    Login or Signup to reply.
  2. I’d like to suggest a different approach:

    1. Create a base pipeline in a shared repository with all the stages/jobs/steps used by all repositories/applications
    2. for each repository or application, create a pipeline that uses the base pipeline (via extends
      templates
      ).

    The base pipeline will ensure a standard structure for your pipelines, and will prevent code duplication.

    Yes, there will be multiple pipelines to manage. But on the other hand, you don’t need to have complicated logic to determine which repository/application is being used, etc. Also, it will be much easier to customize agent pools, resources, triggers, CRON schedules for each one, if required.

    Example

    Base pipeline (shared templates repository):

    # Project: my-project
    # Repository: shared-pipeline-templates
    # 
    # /pipelines/base-pipeline.yaml
    
    parameters:
      # other parameters here
    
      - name: applicationName
        displayName: Name of the application to build and deploy
        type: string
    
      # As an alternative, and in case the stages are always the same for all services,
      # consider removing this parameter and hard-code the stages in the pipeline.
      - name: environments
        displayName: List of environments to deploy to
        type: object
        default: 
          - name: dev
            dependsOn: build
          - name: qa
            dependsOn: dev
          - name: prod
            dependsOn: qa
    
    variables:
      # Common variables that are used by all repositories/applications
      - template: /pipelines/variables/common-variables.yaml
    
    # add common resources (used by all repositories/applications) here
    
    stages:
      - stage: Build
        dependsOn: []
        jobs:
          - job: Build
            displayName: Build
            steps:
              - script: echo Building the application
                displayName: 'Build the application'
    
      - ${{ each environment in parameters.environments }}:
        - stage: ${{ environment.name }}
          displayName: Deploy ${{ environment.name }}
          dependsOn: ${{ environment.dependsOn }}
          jobs:
            - deployment: Deploy${{ environment.name }}
              displayName: Build and Deploy to ${{ environment.name }}
              environment: ${{ environment.name }} # Azure DevOps environment. Use one per environment or application/environment
              variables:
                # Get the variables for the specific application and environment
                # - template: /pipelines/variables/${{ parameters.applicationName }}/${{ environment.name }}-variables.yaml
              strategy:
                runOnce:
                  deploy:
                    steps:
                      - script: echo Deploying to ${{ environment.name }} Environment
                        displayName: 'Deploy to ${{ environment.name }}'
    

    Pipeline for repository/application foo:

    # /pipelines/foo-pipeline.yaml
    
    name: foo_$(Date:yyMMdd)$(Rev:rr)
    
    # Add specific triggers, resources, cron schedules, etc for foo application here
    
    resources:
      repositories:
        - repository: shared
          type: git
          name: my-project/shared-pipeline-templates
          ref: releases/0.1.2
    
    extends:
      template: /pipelines/base-pipeline.yaml@shared
      parameters:
        applicationName: foo # <----------- application name can be hard-coded or use the value of a parameter
        # other parameters here
    

    Pipeline for repository/application bar:

    # /pipelines/bar-pipeline.yaml
    
    name: bar_$(Date:yyMMdd)$(Rev:rr)
    
    # Add specific triggers, resources, cron schedules, etc for bar application here
    
    resources:
      repositories:
        - repository: shared
          type: git
          name: my-project/shared-pipeline-templates
          ref: releases/0.1.2
    
    extends:
      template: /pipelines/base-pipeline.yaml@shared
      parameters:
        applicationName: bar # <----------- application name can be hard-coded or use the value of a parameter
        # other parameters here
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search