typesafegithub / github-workflows-kt

Authoring GitHub Actions workflows in Kotlin. You won't go back to YAML!
https://typesafegithub.github.io/github-workflows-kt/
Apache License 2.0
496 stars 24 forks source link

Type-safe Gradle task accessors #1350

Open krzema12 opened 3 months ago

krzema12 commented 3 months ago

Problem

Currently, the only way of calling a specific Gradle task is to give it as string, e.g.: https://github.com/typesafegithub/github-workflows-kt/blob/7f4b41b5156d811a0d129ade49ebb64569d85ded/.github/workflows/release.main.kts#L48

It requires great attention to type them correctly. It's also risky to keep these as strings with regards to automatic dependency updates. E.g. see https://github.com/gradle-nexus/publish-plugin/releases/tag/v2.0.0:

Backward incompatible changes

  • closeAndReleaseStagingRepository has been renamed to closeAndReleaseStagingRepositories for consistency

In this particular example, we don't call the task that was renamed, but if we did, Renovate would allow merging such change. The worst thing is that it would be caught at the moment of running the release workflow, which currently e.g. for github-workflows-kt happens once a month.

Idea

In the spirit of https://docs.gradle.org/current/userguide/kotlin_dsl.html#type-safe-accessors.

Rough notes, nothing formal:

Example

Before:

val librariesToPublish = listOf(
    ":shared-internal",
    ":github-workflows-kt",
    ":action-binding-generator",
)

librariesToPublish.forEach { library ->
    run(
        name = "Publish '$library' to Sonatype",
        command = "./gradlew $library:publishToSonatype closeAndReleaseSonatypeStagingRepository --no-configuration-cache",
    )
}

After (rough idea, subject to discussion):

val librariesToPublish = listOf(
    projects.sharedInternal,
    projects.githubWorkflowsKt,
    projects.actionBindingGenerator,
)

librariesToPublish.forEach { library ->
    runGradle(
        name = "Publish '$library' to Sonatype",
        tasks = listOf(
            library.publishToSonatype,
            tasks.closeAndReleaseSonatypeStagingRepository,
        ),
        flags = listOf(noConfigurationCache),
    )
}

[1]

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project 'github-workflows-kt-monorepo'
------------------------------------------------------------

Application tasks
-----------------
automation:code-generator:run - Runs this project as a JVM application
jit-binding-server:run - Runs this project as a JVM application
jit-binding-server:runShadow - Runs this project as a JVM application using the shadow jar
jit-binding-server:startShadowScripts - Creates OS specific scripts to run the project as a JVM application using the shadow jar

Build tasks
-----------
assemble - Assembles the outputs of this project.
action-binding-generator:assemble - Assembles the outputs of this project.
automation:code-generator:assemble - Assembles the outputs of this project.
github-workflows-kt:assemble - Assembles the outputs of this project.
jit-binding-server:assemble - Assembles the outputs of this project.
maven-binding-builder:assemble - Assembles the outputs of this project.
...
krzema12 commented 2 months ago

Discussion along with some suggestions: https://kotlinlang.slack.com/archives/C02UUATR7RC/p1712389327584009

krzema12 commented 2 weeks ago

Experimenting: https://github.com/typesafegithub/github-workflows-kt/pull/1511

krzema12 commented 2 weeks ago

New take on the API: