Run Kotlin Code
As the Automation DSL is based on Kotlin, you can run any Kotlin code inside job steps. In case of a container, you should put your code inside the
kotlinScript block. For example:
How it works under the hood (with some simplification): Space compiles the script into a .jar file and runs it with a command like
java -jar script.jar. This means that the image you use to run the script must include JRE/JDK (version 9 or later).
Working with Space modules and external tools
kotlinScript block, you have access to various APIs: Space module APIs and APIs for various external tools (Gradle, dotnet, and others).
In the example below, we use
api.gradle to run Gradle commands and
api.space().chats to access the Chats module. For the full list of APIs, refer to job.container.kotlinScript.
Using functions declared in external packages
kotlinScript lets you use functions from external packages. To do this, you should reference the required package with the
@file:DependsOn("$package_name") annotation. The
$package_name is the name of the package available on Maven Central.
For example, the following script gets a random joke from icanhazdadjoke.com using the OkHttp client and prints it to the job log.