I am working on integration of BitBucket
, TeamCity
and SonarQube
. My scenario is as follows:
- A developer starts a new PR or changes an existing one;
TeamCity
starts building the PR automatically;TeamCity
posts the analysis results toSonarQube
via SonarQube Runner with-Dsonar.branch.name=%teamcity.build.branch%
;BitBucket
requests the analysis details fromSonarQube
by the branch name and displays them on the PR page.
So the problem is that I cannot deduce the name of the branch the PR is based on. Here is what happens:
- I configure
TeamCity
to listen to the+:refs/pull-requests/*/from
reference in theVCS
branch specification; - When
TeamCity
discovers a new PR it starts a build - The name of the branch (
teamcity.build.branch
) gets to be equal to the number of the PR (because of the asterisk in the reference); BitBucket
cannot retrieve the analysis details by the PR's branch name, because they are stored inSonarQube
under the name which is equal to the number of the PR and not the name of the branch.
Solution 1 (dynamic parameters):
- to define some sort of a dynamic parameter;
- to assign a value to the parameter in one of the build steps;
- to use that value to post analysis results to
SonarQube
.
Solution 2:
- to listen to both references:
+:refs/pull-requests/*/from
and+:refs/heads/*
; - to set up a
VCS
trigger that listens to+:refs/heads/*
only; - to fail the build in the first build step if no pull-request reference for the current branch is found.
It does not seem like a good solution.
So it seems to me that the solution should be something like:
- to make TeamCity trigger a build when a new PR is found (the way it works now);
- to make it to figure out the correct branch name (by commit hash) and store it in a dynamic parameter;
- to pass the value of this parameter into
SonarQube
Runner (-Dsonar.branch.name=%dynamic.branch...%
)
I read the documentation about TeamCity predefined branch parameters, but have not found anything helpful.
Please help me figure out how to config it.