15

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?

Community
  • 1
  • 1
san1deep2set3hi
  • 4,904
  • 1
  • 23
  • 23
  • Are you executing your code from inside of TeamCity plugin? – Sergi Oct 12 '16 at 15:22
  • 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 Oct 12 '16 at 17:52
  • 1
    The build id is not exposed as environment variable. Did you check any other way of including URL is good enough -- https://confluence.jetbrains.com/display/TCD10/Patterns+For+Accessing+Build+Artifacts – Jayan Apr 03 '17 at 15:09
  • 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 Apr 03 '17 at 15:46

4 Answers4

6

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)

Daniel Scott
  • 7,418
  • 5
  • 39
  • 58
  • Hi. I don't want to set the variable beforehand instead I want to read its value during execution – san1deep2set3hi Mar 31 '17 at 13:44
  • Yes, but during execution of what? The build, or when you're running the jar which is built from that build? – Daniel Scott Mar 31 '17 at 13:48
  • here I am referring to the execution of build. Thanks. – san1deep2set3hi Mar 31 '17 at 16:18
  • Then i have answered your question, it is available in your build runner using the code I've shown – Daniel Scott Mar 31 '17 at 16:21
  • From maven I don't want to pass system.teamcity.build.id as param. Instead system.teamcity.build.id is a system parameter present in TeamCity. I want to read this value as it is needed in URL of a artifact and this value changes with every build. I am really appreciating your help and would be very happy to give bounty points if I get the answer I need. – san1deep2set3hi Mar 31 '17 at 17:15
  • This is the first time you mentioned maven! You can just access the variable directly in maven using ${teamcity.build.id} - I edited my answer. – Daniel Scott Apr 01 '17 at 06:00
2

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");
san1deep2set3hi
  • 4,904
  • 1
  • 23
  • 23
0

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.

JGooLaaR
  • 94
  • 7
0

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.

KIR
  • 5,614
  • 1
  • 31
  • 23