4

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/*
halfer
  • 19,824
  • 17
  • 99
  • 186
Rasmi
  • 501
  • 1
  • 6
  • 26

2 Answers2

2

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

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • @Rasmi, I have updated my answer with more detailed info and scripts, hope it helps you. Have nice day. – Leo Liu Dec 10 '20 at 08:34
  • I have used this and worked fine!! thanks! – Rasmi Dec 21 '20 at 23:53
  • I too have used this and worked fine!! thanks! – Greg Trevellick Jan 14 '22 at 16:00
  • 1
    I like the spirit of the idea but hinging it on certain words in a message is a big no-no for me. Just an accident waiting to happen. Isn't there a way to look at some status instead for the commit we're processing? – David S Mar 18 '22 at 09:21
1

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://learn.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://learn.microsoft.com/en-us/azure/devops/repos/git/branch-policies?view=azure-devops#build-validation)

yfouillet
  • 109
  • 4
  • My branch is a GitHub branch i am not sure if I can still add branch policy to it!? – Rasmi Dec 09 '20 at 10:44
  • @Rasmi, Github Protected branches are available to Pro, Team, and Enterprise users not support for free account. In addition, even if we have Github Protected branches, it should still not meet your needs. The Build Validation set by Github Protected branches is triggered when the PR is opened, but if we add comment in PR after the build is triggered, the newly added comment will not be validated. We need to validate all comments after the PR is completed . – Leo Liu Dec 09 '20 at 16:59