Trying to give a shot to kotlin scripting for GH a...
# scripting
s
Trying to give a shot to kotlin scripting for GH actions and I am stumped on this issue: I want to do something as simple as write something to GH env variables. Not using kotlin scripting I can do
Copy code
- name: This works
  run: echo SOME_VAR=asd >> $GITHUB_ENV
- name: Using it
  run: echo $SOME_VAR
  env:
    SOME_VAR: ${{ env.SOME_VAR }}
And this works perfectly fine. In Kotlin I try to do:
Copy code
runCommand("echo SOME_VAR=asd >> \$GITHUB_ENV")
(where runCommand comes from here https://github.com/apollographql/apollo-kotlin/blob/65dd7a44a74ed016988777521248da164de32ea0/scripts/bump-kotlin-nightlies.main.kts#L71C1-L85C2 basically using
ProcessBuilder
) And then I try to do the same exact
Using it
task, but nothing at all gets printed. The contents of
SOME_VAR
are just empty. Is this some issue with ProcessBuilder, or something I am doing which is wrong here?e
The context is that I do some things in this kotlin script, and i want to make it available for further steps down the line, however I am having a hard time to get it to work to write something to the GH env variables so that I can use it later on. I could just re-write all my logic in bash instead, which I suppose is what I will do if I don't find a solution to this
m
That
runCommand()
is not really what I'd call "production ready", this was hacked together quickly 😅
s
Hehe that's fair, but I can't say I know of any other way to do this :D
m
The redirection is a shell feature,
ProcessBuilder
doesn't know about shell
Copy code
fun runCommand(args: String): String {
  return runCommand(*args.split(" ").toTypedArray())
}
This basically is going to echo
">>"
👍 1
Because it doesn't go through the initial "shell" parser
So in kotlin scripting, you could read
System.getenv("GITHUB_ENV")
, hope it's a file (I think it should be?) and then append to it with the usual
outputStream
Else you could look into https://github.com/jakubriegel/kotlin-shell that provides redirections, etc..
e
Copy code
ProcessBuilder(listOf("/bin/sh", "-c", "echo PACKAGE_SWIFT_FILE_CONTENT=asd >> $GITHUB_ENV"))
is equivalent to what https://pubs.opengroup.org/onlinepubs/007904975/functions/system.html does
👍 1
if you want to run on Windows though, there's no
sh
, but rather
Copy code
"cmd.exe", "/c", "..."
or maybe
Copy code
"powershell.exe", "-Command", "..."
s
Well I just wanted this to work on a macos runner for my GH action really, not worried about any other platform considerations. But I feel like I am biting more than I can chew, I am trying to look into just not using kotlin at all here instead
p
I'd actually suggest what @mbonnin wrote about using Kotlin/Java's API. It's done in a similar way when writing outputs in github-workflows-kt: https://github.com/typesafegithub/github-workflows-kt/blob/b5b184600c0dc5db6aa8632305e339b3eaed7ece/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/domain/contexts/OutputsContext.kt#L10