15
votes

Teamcity Build ID (which is different from BUILD_NUMBER) is used in various URLs. I want to send an email having path of a build's artifacts/ overview etc.

In Java, we can get currently running teamcity build number as follows:

String tc_BuildNumber = System.getenv("BUILD_NUMBER");

This is because TC provides an environment variable namely BUILD_NUMBER. But unfortunately, there is no environment variable corresponding to BUILD_ID.

TeamCity does provide Configuration parameters (like teamcity.build.id) and System property (like system.teamcity.auth.userId) but I don't know how to access these using Java. I want to read the value of teamCity.build.id jusy like we can read environment variables names mentioned in How to fetch the Value of Teamcity Configuration in java?

4
Are you executing your code from inside of TeamCity plugin?Sergi
Above code in Java class file. If the java project is ran through one of TC build steps, above line gets executed and gives current build number.san1deep2set3hi
The build id is not exposed as environment variable. Did you check any other way of including URL is good enough -- confluence.jetbrains.com/display/TCD10/…Jayan
Hay @Jayan.The link shared by you worked for me. I would be glad to share the bounty to you. Could you please put your comment as answer. I will accept that..san1deep2set3hi

4 Answers

6
votes

Are you executing the java code using a build runner?

If so, then you should be able to pass %system.teamcity.build.id% to the runner, and make it available to your code.

i.e. If you're using the command line runner

java -Dbuild_id=%system.teamcity.build.id%

which you can then access as system arguments

Or if you're using gradle, you can do something like

if (project.hasProperty("teamcity")) {
    version = project.teamcity["teamcity.build.id"]
}

and pass 'version' to the java command line.

In maven, you can just access it using:

${teamcity.build.id}

in your pom.xml

(I could do with a little more info about how you're running java to answer this specifically)

2
votes

I've noticed that lots of people want to know the answer to this question. Fortunately with the help of comment from @Jayan I was able to do solve my exact problem which was how to get URL for build artifacts.

As mentioned in link https://confluence.jetbrains.com/display/TCD10/Patterns+For+Accessing+Build+Artifacts, by default, TeamCity uses Internal Build ID for the path that can be used to access build artifacts:

/repository/download/BUILD_TYPE_EXT_ID/BUILD_ID:id/ARTIFACT_PATH

Accessing build Id could be difficult in the runtime(That is the reason of this question), but we can also use Build Number to access artifacts

/repository/download/BUILD_TYPE_EXT_ID/BUILD_NUMBER/ARTIFACT_PATH

And as shown in my question build number can be accessed as

String BUILD_NUMBER= System.getenv("BUILD_NUMBER");

and

String BUILD_TYPE_EXT_ID = System.getenv("TEAMCITY_BUILDCONF_NAME");
0
votes

Yes, but you can create env var with value "%system.teamcity.buildType.id%" and read it in build. After that you can do an api request like:

$APIURL = "${API_BaseUrl}/httpAuth/app/rest/builds/?locator=buildType:${API_BuildType},state:running,count:1"

$APIXML = (Invoke-RestMethod -Headers $API_CredentialsHeader -Credential $API_Credentials -Uri $APIURL -Method GET -ContentType "application/xml" -TimeoutSec 20)
# Here you build id.
$APIXML.builds.build.id

This is PS example. But idea the same. In Java that might be more easy.

0
votes

A link to a TeamCity build can use build number instead of buildID. But, it requires buildTypeId as well (can be seen in build configuration page URL).

A sample of such link is:

https://buildserver/viewLog.html?buildTypeId=Project_Trunk&buildNumber=46523

Hope this helps someone.