1
votes

I have an Azure DevOps Pipeline for a Git repository. I currently have a script to validate the PR comments in the Azure Pipeline.

When the code is merged into the main branch I want to trigger a build. I am not sure how to achieve this with a Azure DevOps pipeline.

#Trigger for Development
trigger:
 branches:
   include:
     - development
     - master
#Trigger checks for PR
pr: 
 branches:
    include:
      - development
      - master
      - feature
      - main
 paths:
   exclude:
     - README/*
2

2 Answers

0
votes

When the code is merged into the main branch I wanted to trigger build

If you want to verify the comments after the code is merged into the main branch, we need to trigger the build after the PR completed instead of when PR is created.

So, the PR triggers could not meet our requirement in this case.

To resolve this issue, we could enable CI triggers for the main branch with ** condition** eq(variables['Commitcomment'], 'Merge pull request') for the task of script to validate the PR comments.

With this condition, the pipeline will execute the job only when the Commitcomment is Merge pull request, this can filter out modifications not done by PR.

To get the value of the variable Commitcomment, we could to check the commits message on our github by the variable Build.SourceVersionMessage:

enter image description here

If the commit comes from PR, it will given a default comment, starting with: Merge pull request xxx, we could add a bash\powershell script to get the first few fields.

Then use Logging Command to set the variable Commitcomment to true if the first few fields is Merge pull request:

  - task: CmdLine@2
    displayName: get the first few fields
    inputs:
      script: >-
        echo $(Build.SourceVersionMessage)
        set  TempVar=$(Build.SourceVersionMessage)
        set Commitcomment=%TempVar:~0,18%
        echo %Commitcomment%
        echo ##vso[task.setvariable variable=Commitcomment]%Commitcomment%

Reference link: Is there a short 7-digit version of $(SourceVersion) in Azure Devops?

Then add this variable as condition condition: and(succeeded(), eq(variables['Commitcomment'], 'Merge pull request')) for your task to verify the PR comments:

  - task: CmdLine@2
    displayName: script to validate the PR comments
    condition: and(succeeded(), eq(variables['Commitcomment'], 'Merge pull request'))
    inputs:
      script: >
        echo To validate the PR comments

In this case, if the commit not comes from PR, it will skip the PR comments verify task:

enter image description here

0
votes

If you just want to launch a build when the merge is done (pull request validated) in a specific branch, your code is good.

If you want to run a validation build currently it is not integrated into the Yaml pippeline configuration (https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema%2Cparameter-schema#pr-trigger)

To do this, it must be done via the graphical interface: Project Settings -> Repositories -> Select your repo -> Policies -> Branch Policies -> Select your branch -> Build Validation -> + -> add build information

(https://docs.microsoft.com/en-us/azure/devops/repos/git/branch-policies?view=azure-devops#build-validation)