Skip to content
You're viewing an older version of this GitHub Action. Do you want to see the latest version instead?
package

GitHub Action

Execute Kotlin Script

v2.3

Execute Kotlin Script

package

Execute Kotlin Script

Run the Kotlin Script with a compiler plugin

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Execute Kotlin Script

uses: sureshg/[email protected]

Learn more about this action in sureshg/kts-exec

Choose a version

Kotlin Script Github Action

Version Kotlin Script Test

Kotlin can also be used as a scripting language, which is more safer, concise, and fun to write than bash or python. Github has recently enabled Kotlin on Github Action runners, which enables us to use Kotlin Script on Github Action out of the box. But using compiler plugins ( eg: kotlinx-serialization) is not straightforward with Kotlin script and requires a bit of fiddling. kts-exec is a composite Github Action to execute the Kotlin Script (on mac/linux /windows) with a given kotlin compiler plugin and dependency caching.

Inputs

script

Required The Kotlin script to execute. Default script name is script.main.kts.

compiler-plugin

Optional Kotlin compiler plugin to use. Currently supported values are,

  • kotlinx-serialization (Default)
  • allopen
  • noarg
  • lombok
  • sam-with-receiver
  • kotlin-imports-dumper

cache

Optional Enable kotlin script dependencies caching. It is enabled (true) by default.

Outputs

plugin-path

Local path to the kotlin compiler-plugin. You may also access the path via ${{ env.PLUGIN_PATH }}.

kotlin-root

The kotlin installation path. You may also access the path via ${{ env.KOTLIN_ROOT }}.

Usage

Say, you want to execute the kotlin script with a Serializable data class for JSON processing

script.main.kts
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.1")

import kotlinx.serialization.*
import kotlinx.serialization.json.*

@Serializable
data class Lang(val name: String, val version: String)

val arg = args.firstOrNull() ?: "Kotlin"
println("Hello $arg!")

val serialized = Json.encodeToString(Lang("Kotlin", KotlinVersion.CURRENT.toString()))
println(serialized)

Add the kt-exec to your workflow and run your kotlin script.

jobs:
  build:
    runs-on: ubuntu-lastest
    steps:
      - name: Check out repository
        uses: actions/checkout@v2

      - name: Run Kotlin Script
        uses: sureshg/[email protected]
        with:
          script: "script.main.kts"