Search code examples
azure-devops

Trying to make pipeline more flexible


In general, I have one pipeline (SourcePipeline) which triggers the another one (DependantPipeline), and two of them are in specific repository devops-med. Also, two of them are using pipeline definitions from other repository global-pipelines.

My set of pipelines works as expected, however they need some adjustment not to use the hardcoded names in some pipelines definitions. I am using the hardcoded name of the repository to which pipeline is attached to.

Pipelines definitions in devops-med repository based on my already working approach:

devops-med/devops/pipeline.yml

name: $(Build.DefinitionName) # added new line on 08/31
name: SourcePipeline  # removed that line on 08/31

resources:
  repositories:
    - repository: global-pipelines
      type: git
      name: global-pipelines
      ref: test/pipeline

trigger:
  batch: true
  branches:
    include:
      - test/pipeline

pr: none

parameters:
  - name: pipelineMode
    displayName: Pipeline Mode
    type: string
    default: main
    values:
      - auto
      - feature
      - main
    
extends:
  template: /cicdTemplates/pipeline.yml@global-pipelines
  parameters:
    variablesTemplate: devops/variables/global_variables.yml@self
    pipelineMode: ${{parameters.pipelineMode}}
    buildJobs: devops/build_jobs.yml@self

To that one I added in first line name: $(Build.DefinitionName), and removed name: SourcePipeline. Still works.

devops-med/devops/dependantpipeline.yml

trigger: none

resources:
  repositories:
    - repository: global-pipelines
      type: git
      name: global-pipelines
      ref: test/pipeline
  pipelines:
    - pipeline: SourcePipeline
      project: DEV
      # source: 'devops-med'
      source: $(Build.DefinitionName) # added new line on 08/31
      trigger:
        branches:
          include:
            - test/pipeline

parameters:
  - name: pipelineMode
    displayName: Pipeline Mode
    type: string
    default: main
    values:
      - auto
      - feature
      - main

extends:
  template: /cicdTemplates/pipeline.yml@global-pipelines
  parameters:
    variablesTemplate: devops/variables/global_variables.yml@self
    pipelineMode: ${{parameters.pipelineMode}}
    devEnvironment: dev
    devVariables: devops/ew/variables/dev_variables.yml@self
    deployEnvironmentTemplate: devops/deploy_environment.yml@self

In that one I changed source: 'devops-med' to source: $(Build.DefinitionName) as described in above pipeline definition. It does not work, I am getting the error in Azure DevOps:

/devops/dependantpipeline.yml (Line: 12, Col: 17): Pipeline Resource SourcePipeline Input Must be Valid.

Maybe I am wrong with my assumption and can't use Build.DefinitionName on dependantpipeline.yml and it must be hardcoded to know which excatly pipeline triggered it. However, I tried based on the documentation: https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

In general, the goal is not to use the hardcoded name devops-med.

EDIT 1.

When I did it in this way:

    parameters:
      - name: sourcePipelineName
        type: string
        default: 'devops-med'
    
    resources:
      pipelines:
        - pipeline: SourcePipeline
          project: DEV
          source: ${{ parameters.sourcePipelineName }}
          trigger:
            branches:
              include:
                - test/pipeline
    extends:
      template: /cicdTemplates/pipeline.yml@global-pipelines
      parameters:
        variablesTemplate: devops/variables/global_variables.yml@self
        pipelineMode: ${{parameters.pipelineMode}}
      devEnvironment: dev
      devVariables: devops/ew/variables/dev_variables.yml@self
      deployEnvironmentTemplate: devops/deploy_environment.yml@self

I got error:

devops/dependantpipeline.yml (Line: 17, Col: 15): A template expression is not allowed in this context


Solution

  • According to your description, you have two Azure DevOps pipelines (SourcePipeline and DependantPipeline) in the devops-med repository. The DependantPipeline is triggered by the SourcePipeline using a pipeline resource trigger.

    The name of the SourcePipeline is devops-med, which is the same as the repository name, so you use source: 'devops-med' in the pipeline resource trigger.

    If you don't want to the hardcoded it in the pipeline resource trigger, you can change the pipeline to use a new name instead of the repository name.

    You can also consider using the Generic webhook based triggers for YAML pipelines to trigger the second pipeline when the first one completes instead of using the pipeline resource triggers.

    Here are the detailed steps:

    1. Create an "Incoming Webhook" service connection in Project settings > Service connections service connection

      Incoming Webhooks service connection

    2. Create a Service hook web hook with work item related event in Project settings > Service Hooks and choose the Build completed event for the SourcePipeline devops-med.

      web hook

      The Request Url is

      "https://dev.azure.com/<ADO Organization>/_apis/public/distributedtask/webhooks/<WebHook Name>?api-version=6.0-preview"
      

      The WebHook Name in the URL is the one you set in the service connection.

      WebHook Name

    3. Modify the DependantPipeline with the resource type webhooks. The pipeline will be triggered when the SourcePipeline devops-med is successful.

    Sample YAML:

    trigger: none
    
    resources:
      repositories:
        - repository: global-pipelines
          type: git
          name: global-pipelines
          ref: test/pipeline
      webhooks:
        - webhook: MyWebhookTrigger         ### Webhook name
          connection: MyWebhookConnection    ### Incoming webhook service connection name