In many CI/CD scenarios it’s necessary to adjust the build, test or deployment process depending on which GIT branch has triggered the pipeline. In our case we build lots of Angular apps automatically with their desired target environment, e.g.
ng build --configuration="production"
or ng build --configuration="staging"
depending on where the artifact should be deployed afterwards. This can be achieved by adding some YAML that looks like this:
If you want to copy the code, please do so from “view raw” since the pretty-fied version might cause some issues in YAML.
However, this will not work for builds triggered by Pull Requests since in those cases the source branch info will look something like this:
BUILD_SOURCEBRANCH = refs/pull/101/merge
BUILD_SOURCEBRANCHNAME = merge
BUILD_SOURCEVERSIONMESSAGE = Merge pull request 101 from dev/myDevBranch into master
So to get the correct branch name for your builds you’ll need to distinguish between PRs and “regular” builds before deciding on how to build your app. For our pipeline decided to create a variable “branchName” which we can then use in our conditions.
Original Photo by Mike Benna on Unsplash
Thanks to David for the idea!
Looks like you have a typo in:
variables:
${{ if startsWith(variables[‘Build.SourceBranch’], ‘refs/heads/’) }}:
branchName: $[ replace(variables[‘Build.SourceBranch’], ‘refs/heads/’, ”) ]
${{ if startsWith(variables[‘Build.SourceBranch’], ‘refs/pull/’) }}:
branchName: $[ replace(variables[‘System.PullRequest.TargetBranch’], ‘refs/heads/’, ”) ]
Should probably be:
variables:
${{ if startsWith(variables[‘Build.SourceBranch’], ‘refs/heads/’) }}:
branchName: $[ replace(variables[‘Build.SourceBranch’], ‘refs/heads/’, ”) ]
${{ if startsWith(variables[‘Build.SourceBranch’], ‘refs/pull/’) }}:
branchName: $[ replace(variables[‘System.PullRequest.TargetBranch’], ‘refs/pull/’, ‘
‘) ]