Getting started
As an exercise, we'll add a job that prints out Hello world!
. Feel free to replace the actual workflow's logic and all
names with your own.
- Install Kotlin as a stand-alone binary, e.g. from Snap Store when on Linux:
Make sure this is the newest version available. Kotlin scripting still has some rough edges, and improvements are introduced with each new Kotlin release. Also make sure that you use Java 11+.
sudo snap install kotlin --classic
- Create a new executable file in your repository:
This location is not a hard requirement, it's just recommended for consistency with enforced location of actual GitHub Actions workflows.
touch .github/workflows/hello_world_workflow.main.kts chmod +x .github/workflows/hello_world_workflow.main.kts
- Put this content into the previously created file and save it:
Explanation: first, we create a workflow with the DSL provided by this library. The reason it needs source file path is to be able to generate consistency checks, to ensure that both source and target files are in sync. You'll see it in a moment in the generated file. What's written to the
#!/usr/bin/env kotlin @file:DependsOn("io.github.typesafegithub:github-workflows-kt:1.15.0") import io.github.typesafegithub.workflows.actions.actions.CheckoutV4 import io.github.typesafegithub.workflows.domain.RunnerType.UbuntuLatest import io.github.typesafegithub.workflows.domain.triggers.Push import io.github.typesafegithub.workflows.dsl.workflow import io.github.typesafegithub.workflows.yaml.writeToFile workflow( name = "Test workflow", on = listOf(Push()), sourceFile = __FILE__.toPath(), ) { job(id = "test_job", runsOn = UbuntuLatest) { uses(name = "Check out", action = CheckoutV4()) run(name = "Print greeting", command = "echo 'Hello world!'") } }.writeToFile()
workflow
variable is an object of typeio.github.typesafegithub.workflows.domain.Workflow
, it's not a YAML yet. However, a call towriteToFile()
extension function does the final piece of job.
Alternatively, apart fromwriteToFile()
which puts the string straight into the file under path inferred by the library or name overridden withtargetFileName
workflow argument, there's alsotoYaml()
which doesn't touch any files, it just returns a string with the YAML. It may come in handy when more control over the YAML is needed, e.g. to do some post-processing. - Generate the YAML by calling the above script:
It can be also executed straight from IntelliJ, by clicking the green ▶️ button next to the shebang. Notice that there's an extra job generated by the library that regenerates the YAML in job's runtime and ensures that it's equal to the YAML committed to the repository.
.github/workflows/hello_world_workflow.main.kts
- Commit both files, push the changes to GitHub and make sure the workflow is green when ran on GitHub Actions.