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
:
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: