github-workflows-kt icon indicating copy to clipboard operation
github-workflows-kt copied to clipboard

[Core feature request] Add support to Workflow Dispatch Inputs

Open LeoColman opened this issue 2 years ago • 5 comments

Discussed in Kotlin's Slack: https://app.slack.com/client/T09229ZC6/C02UUATR7RC/thread/C02UUATR7RC-1682902161.628869

What feature do you need? Workflows Dispatch Inputs: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onworkflow_dispatchinputs

Do you have an example usage?

https://github.com/kotest/kotest/blob/80bc809be1e0452dc1f3ee2e67aec2f6c7f6c251/.github/workflows/release_base.yml#L15


env:
   RELEASE_VERSION: ${{ inputs.version }}
   OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }}
   OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }}
   ORG_GRADLE_PROJECT_signingKey: ${{ secrets.SIGNING_KEY }}
   ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.SIGNING_PASSWORD }}
   GRADLE_OPTS: -Dorg.gradle.configureondemand=true -Dorg.gradle.parallel=false -Dkotlin.incremental=false -Dorg.gradle.jvmargs="-Xmx3g -XX:MaxMetaspaceSize=756m -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8"

Is there a workaround for not having this feature? If yes, please describe it.

Yes. One can use WorkflowDispatch normally, and use expr { github["event.inputs.XXXX"]!! } to get the value when they need it.

workflow(
  name = "Publish",
  on = listOf(
    WorkflowDispatch(
      mapOf("RELEASE_VERSION" to Input("The release version", true, String))
    )
  ),
  sourceFile = __FILE__.toPath(),
  env = linkedMapOf(
    "OSSRH_USERNAME" to expr { OSSRH_USERNAME },
    "OSSRH_PASSWORD" to expr { OSSRH_PASSWORD },
    "ORG_GRADLE_PROJECT_signingKey" to expr { ORG_GRADLE_PROJECT_signingKey },
    "ORG_GRADLE_PROJECT_signingPassword" to expr { ORG_GRADLE_PROJECT_signingPassword },
    "RELEASE_VERSION" to expr { github["event.inputs.version"]!! }
  )
)

LeoColman avatar May 01 '23 13:05 LeoColman

As you've already noticed, some support for it is already in place (I forgot about it when asking you to create this request, sorry!): https://github.com/typesafegithub/github-workflows-kt/blob/9eee511569659eb3d1c3228a2d96e3fbd2bed314/library/src/main/kotlin/io/github/typesafegithub/workflows/domain/triggers/WorkflowDispatch.kt#L9-L12

From what I understand, you're missing a type-safe way of referring to the input values, right? On GitHub, the inputs are available within inputs context, and the library already supports several other contexts, see here. It should be enough to extend the library to support something like:

workflow(
  name = "Publish",
  on = listOf(
    WorkflowDispatch() // After the change, the inputs wouldn't be defined here...
  ),
  sourceFile = __FILE__.toPath(),
) {
    // ...but here instead. It's in line with what's already possible with env vars and secrets.
    val RELEASE_VERSION by Contexts.input(required = true, type = String)

    // ...
    // inside a job that actually uses the secret:
    run(
        name = "Release",
        env = linkedMapOf(
            VERSION to expr { RELEASE_VERSION },
        ),
        command = "./gradlew release",
    )
}

WDYT?

krzema12 avatar May 02 '23 06:05 krzema12

@jmfayard do you recall why we originally haven't implemented it this way?

krzema12 avatar May 02 '23 06:05 krzema12

@krzema12 we probably implemented WorkflowDispatch before I implemented the typesafe expressions, meaning we didn't design typesafe inputs at all

jmfayard avatar May 02 '23 10:05 jmfayard

Yes, I think the syntax you presented seems what I expect @krzema12 . Interesting to know it predates the typesafety

LeoColman avatar May 02 '23 18:05 LeoColman