1
votes

So ive been building a build pipeline, that is triggered whenever a pull request is done to master, so we have a branch policy such that the only change to the master branch is through pull requests.

I want the build pipeline to checkout the source branch of the PR and do some commits to the source branch as part of the build pipeline. I thought i could just use the Build.SourceBranchName variable but when the pipeline is triggered the SourceBranchName is master. So I could not use it.

Are there any easy ways of doing this?

1
You can use the System.PullRequest.SourceBranch variable to access the branch that is being reviewed in a pull request. This variable will be available only if the Build.Reason equals PullRequest.LJ.
@LJ. thanks! That works, now the issue becomes that because of a new commit to the PR it runs the pipeline again, this should not happen since i have [skip ci] in the commit message. Do you know of a fix for this?Shadesfear
I believe [skip-ci] is not applicable to PR builds - it works only for IndividualCI builds. Workaround for this could be to add run conditions that make the build skip all the tasks, if [skip ci] is included in the commit message - like this condition: not(contains(variables['Build.SourceVersionMessage'], '[skip ci]'))LJ.
How about the issue? Does the answer below resolved your question, If not, would you please let me know the latest information about this issue?Leo Liu-MSFT

1 Answers

1
votes

I want the build pipeline to checkout the source branch of the PR

To checkout the source branch of the PR, you could use the predefined system variables about PR:

System.PullRequest.SourceBranch and System.PullRequest.TargetBranch

To get the branch that is being reviewed in a pull request, we should select the variable System.PullRequest.SourceBranch.

now the issue becomes that because of a new commit to the PR it runs the pipeline again, this should not happen since i have [skip ci] in the commit message.

As we know, the [skip ci] or [ci skip] is used to skip running CI, like the option Enable continuous integration on UI:

enter image description here

However, our current scenario is branch policy for build validation instead of CI. This is very different from CI, although they seem to be doing the same build task. Branch policy is to protect our branches from being corrupted by incorrect submit. This is a verified operation instead of continuous integration.

Check the document Skipping CI for individual commits for some more details.

So, this is two different scenarios, we could not apply the CI settings to the branch policy.

Second, Branch policy is used to protect our branches, any commit requires validation by branch pliocy, although sometimes we can know that our modifications don't require build validation, but we're not sure if there are any where we overlook that cause our target branch to be broken. Skip unnecessary verification will bring us some construction convenience, but with the risk measurement it brings, these conveniences are negligible, so we don't recommend skipping the verification of the branch office strategy.

If skipping Build Validation is your insistence, you can try LJ’s suggestion.