github-actions-kotlin-script-template
GitHub Actions support Kotlin Script - see actions/runner-images#3687
Let's try it!
IntelliJ IDEA 2022.1 | GitHub Actions |
---|---|
How to use this GitHub Template
Step 1 - Clone
Just click on button to create a new repo starting from this template.
Step 2 - Update the script content
Update project.main.kts. Optionally you could change the file name.
Example 1: Example with external libraries:
#!/usr/bin/env kotlin
@file:Repository("https://repo.maven.apache.org/maven2")
@file:Repository("https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven")
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-html-jvm:0.7.3")
import kotlinx.html.body
import kotlinx.html.h1
import kotlinx.html.html
import kotlinx.html.p
import kotlinx.html.stream.createHTML
import java.util.Date
val addressee = "World"
println(createHTML().html {
body {
h1 { +"Hello, $addressee!" }
p { +"Last run: ${Date()}" }
}
})
Result:
<html>
<body>
<h1>Hello, World!</h1>
<p>Last run: Mon Jun 20 17:12:31 CEST 2022</p>
</body>
</html>
Step 3 - Update main.yml (Optional)
Update .github/workflows/main.yml
# Update the file name here
run: kotlinc -script ./project.main.kts
# Update the commit message
git commit -m "Run script (using Github Action)"
Step 4 - Launch this Action! π
Go to the Actions tab in GitHub. Have fun!
Other examples
KotlinScriptToolbox
utils
Example 2: #!/usr/bin/env kotlin
@file:Repository("https://repo.maven.apache.org/maven2")
@file:Repository("https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven")
@file:DependsOn("com.github.omarmiatello.kotlin-script-toolbox:zero-setup:0.1.5")
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-html-jvm:0.7.3")
import com.github.omarmiatello.kotlinscripttoolbox.core.launchKotlinScriptToolbox
import kotlinx.html.body
import kotlinx.html.h1
import kotlinx.html.html
import kotlinx.html.p
import kotlinx.html.stream.createHTML
import java.util.*
launchKotlinScriptToolbox(
scope = BaseScope.fromDefaults(),
scriptName = "My example",
) {
val addressee = "World"
val res = createHTML().html {
body {
h1 { +"Hello, $addressee!" }
p { +"Last run: ${Date()}" }
}
}
println(res)
writeText("example.html", res)
}
Result:
π My example - Start!
<html>
<body>
<h1>Hello, World!</h1>
<p>Last run: Mon Jun 20 17:07:49 CEST 2022</p>
</body>
</html>
π My example - Completed in 23ms
Example 3: data classes + read/write files + read system property
#!/usr/bin/env kotlin
@file:Repository("https://repo.maven.apache.org/maven2")
@file:DependsOn("com.github.omarmiatello.kotlin-script-toolbox:zero-setup:0.1.5")
import com.github.omarmiatello.kotlinscripttoolbox.core.launchKotlinScriptToolbox
import com.github.omarmiatello.kotlinscripttoolbox.gson.readJsonOrNull
import com.github.omarmiatello.kotlinscripttoolbox.gson.writeJson
import com.github.omarmiatello.kotlinscripttoolbox.zerosetup.ZeroSetupScope
data class DataExample(val p1: Int = 1)
launchKotlinScriptToolbox(scope = ZeroSetupScope()) {
// read secrets from system property or local.properties
readSystemPropertyOrNull(propertyName = "SECRET_API_KEY")
// read/write text
writeText(pathname = "file.txt", text = "Ciao!")
val content1: String? = readTextOrNull(pathname = "file.txt")
// read/write object
writeJson(pathname = "file.json", obj = DataExample(p1 = 2))
val content2: DataExample? = readJsonOrNull(pathname = "file.json")
// notifications
sendTelegramMessage("Hi!")
sendTweet("Hi!")
}
Example 4: Stadia Games API (parse + write file + send notification)
See: https://github.com/omarmiatello/stadia-games-api/blob/main/update-api.main.kts