Skip to content

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.

  1. Install Kotlin as a stand-alone binary, e.g. from Snap Store when on Linux:
    sudo snap install kotlin --classic
    
    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+.
  2. Create a new executable file in your repository:
    touch    .github/workflows/hello_world_workflow.main.kts
    chmod +x .github/workflows/hello_world_workflow.main.kts
    
    This location is not a hard requirement, it's just recommended for consistency with enforced location of actual GitHub Actions workflows.
  3. Put this content into the previously created file and save it:
    #!/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()
    
    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 workflow variable is an object of type io.github.typesafegithub.workflows.domain.Workflow, it's not a YAML yet. However, a call to writeToFile() extension function does the final piece of job.
    Alternatively, apart from writeToFile() which puts the string straight into the file under path inferred by the library or name overridden with targetFileName workflow argument, there's also toYaml() 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.
  4. Generate the YAML by calling the above script:
    .github/workflows/hello_world_workflow.main.kts
    
    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.
  5. Commit both files, push the changes to GitHub and make sure the workflow is green when ran on GitHub Actions.