Whichever CI server you use for development, being able to access it programmatically via REST API is an essential capability for implementing all kinds of automation. In this article you will learn how to utilize TeamCity REST API using TeamCity Kotlin REST Client.
Let’s start by setting up some goals.
I want to be able to access my TeamCity server using Kotlin programming language.
The goal is set and to help me with the task JetBrains provides a REST client which I’ve mentioned earlier. So all I have to do is write a simple Kotlin script that uses REST client and implement whichever automation I want, right? Well, except that I’m not a Java or Android developer and building Kotlin source code is totally new to me, so this article will cover some basics.
I’m working on a Mac OS X machine, so I’ll start by getting Gradle Build Tool installed.
Next, I’ll create a new directory for my project as well as the main Kotlin file and a Gradle wrapper.
From now on I should be running all Gradle commands using the wrapper, e.g.
./gradlew build. Wrapper will be saved in
gradle/wrapper folder. This folder can be committed to source control which removes the dependency on global Gradle installation and makes the project self-contained.
I want to use Kotlin to write build scripts, so I create
build.gradle.kts file in the project root with the following contents:
This is to make sure I’m using exact version of Gradle and it doesn’t autoupdate.
This may sound unusual, but in this case I choose to write the Kotlin code first and then make it compile and run. Based on the example on REST client project page, I came up with the following:
In a nutshell this code connects to TeamCity using username and password provided as input arguments, then fetches latest build information for a specified build configuration and prints it out to console. Check inline comments for more details.
After a lot of trial an error I came up with the the following build file:
Check the inline comments for detailed information. This build script builds Kotlin application targeted for running on top of JVM. The application depends on TeamCity REST client.
Note that you have to install Java version 1.8 to build and run the application.
It’s time to build it:
I can now run the Kotlin application:
After customizing input arguments I am able to see something like this in the logs:
That’s just the beginning! I can now use nice modern programming language to automate various CI/CD tasks on TeamCity.
For a full example project see this repository.