|
1 | | -# kts-exec |
2 | | -A composite Github Action to execute the Kotlin Scripts! |
| 1 | +# Kotlin Script Github Action |
| 2 | + |
| 3 | +[](https://github.com/sureshg/kts-exec/actions/workflows/test.yml) |
| 4 | + |
| 5 | +`Kotlin` can also be used as a scripting language, which is more safer, concise, and fun to write |
| 6 | +than `bash` or `python`. Github has recently enabled `Kotlin` on `Github Action` runners, which |
| 7 | +enables us to use [Kotlin Script][0] on Github Action out of the box. But using compiler plugins ( |
| 8 | +eg: [kotlinx-serialization][1]) is not straightforward with Kotlin script and requires a bit of |
| 9 | +fiddling. `kts-exec` is a composite Github Action to execute the Kotlin Script (on `mac`/`linux` |
| 10 | +/`windows`) with a given kotlin compiler plugin. |
| 11 | + |
| 12 | +## Inputs |
| 13 | + |
| 14 | +### `script` |
| 15 | + |
| 16 | +**Required** The Kotlin script to execute. Default script name is `script.main.kts`. |
| 17 | + |
| 18 | +### `compiler-plugin` |
| 19 | + |
| 20 | +**Optional** Kotlin compiler plugin to use. Currently supported values are, |
| 21 | + |
| 22 | +- `kotlinx-serialization` (**Default**) |
| 23 | +- `allopen` |
| 24 | +- `noarg` |
| 25 | +- `lombok` |
| 26 | +- `sam-with-receiver` |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +Say, you want to execute the kotlin script with a `Serializable` data class for JSON processing |
| 31 | + |
| 32 | +<details open> |
| 33 | +<summary>script.main.kts</summary> |
| 34 | + |
| 35 | +```kotlin |
| 36 | +@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2") |
| 37 | + |
| 38 | +import kotlinx.serialization.* |
| 39 | +import kotlinx.serialization.json.* |
| 40 | + |
| 41 | +@Serializable |
| 42 | +data class Lang(val name: String, val version: String) |
| 43 | + |
| 44 | +val arg = args.firstOrNull() ?: "Kotlin" |
| 45 | +println("Hello $arg!") |
| 46 | + |
| 47 | +val serialized = Json.encodeToString(Lang("Kotlin", KotlinVersion.CURRENT.toString())) |
| 48 | +println(serialized) |
| 49 | +``` |
| 50 | + |
| 51 | +</details> |
| 52 | + |
| 53 | +Add the `kt-exec` to your workflow and run your kotlin script. |
| 54 | + |
| 55 | +```yml |
| 56 | +jobs: |
| 57 | + build: |
| 58 | + runs-on: ubuntu-lastest |
| 59 | + steps: |
| 60 | + - name: Check out repository |
| 61 | + uses: actions/checkout@v2 |
| 62 | + |
| 63 | + - name: Run Kotlin Script |
| 64 | + uses: sureshg/kts-exec@v2 |
| 65 | + with: |
| 66 | + script: "script.main.kts" |
| 67 | +``` |
| 68 | +
|
| 69 | +[0]: https://kotlinlang.org/docs/command-line.html#run-scripts |
| 70 | +
|
| 71 | +[1]: https://kotlinlang.org/docs/serialization.html |
0 commit comments