https://kotlinlang.org logo
#github-workflows-kt
Title
# github-workflows-kt
d

Damien O'Hara

02/13/2023, 1:39 PM
hi all. I'm trying to port this workflow to
github-workflows-kt
but I am not sure how to tackle the line
url: ${{ steps.deployment.outputs.page_url }}
. I don't mind hard coding it in a _customArgument it but I don't see a way to provide the
deployment
step id to that deployment step. hard-coding the generated id (e.g.
step-4
) seems brittle since the ids are positional. any thoughts on the best approach here?
p

Piotr Krzemiński

02/13/2023, 1:40 PM
you don’t need to play with step IDs then, everything’s managed by the library
d

Damien O'Hara

02/13/2023, 1:47 PM
I might have missed something but I don't think this is applicable. in this case the step output is needed before the step is declared
v

Vampire

02/13/2023, 1:50 PM
I wonder that actually works in GitHub at all. To have an environment value for the job coming from the output of a step inside. Where is
url
then used?
p

Piotr Krzemiński

02/13/2023, 1:50 PM
that’s weird… how does it even work? now I see the page URL is used in
environment
, so before the steps run?
d

Damien O'Hara

02/13/2023, 1:52 PM
I'm not fully across how environments work in GitHub but it seems like a real thing. here is an example from GH docs: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-using-output-as-url
v

Vampire

02/13/2023, 1:59 PM
It's a GitHub actions integrated feature. The url is evaluated after the workflow is finished and then used at various places about "deployment" in the GitHub actions UI
d

Damien O'Hara

02/13/2023, 2:17 PM
I think this makes sense. referencing step outputs ahead of time is how job outputs depend on steps after all. I think for now I will keep this action in YAML
p

Piotr Krzemiński

02/13/2023, 2:19 PM
ok, it’s slightly difficult for me to wrap my head around it, so if you come up with an elegant way to support it in the library, feel free to cut a feature request FTR, the library also has API for dependencies on job A’s output in job B, in case you ever need it: https://github.com/krzema12/github-workflows-kt/blob/5f45a621b9aba292541e1d47c4738[…]/src/test/kotlin/it/krzeminski/githubactions/IntegrationTest.kt
v

Vampire

02/13/2023, 2:20 PM
You can also define job outputs from step outputs: https://krzema12.github.io/github-workflows-kt/user-guide/job-outputs/
Actually all that is missing is
env
on
WorkflowBuilder
like there is on
JobBuilder
, but you can already do it
Copy code
lateinit var deployment: ExternalActionStepWithOutputs<CustomAction.Output>
workflow(
    ...
) {
    job(...) {
        deployment = uses(
            action = CustomAction(
                actionOwner = "actions",
                actionName = "deploy-pages",
                actionVersion = "v1",
                inputs = mapOf()
            )
        )
    }
}.apply {
    env["url"] = expr(deployment.outputs["page_url"])
    writeToFile()
}
If there were
env
on
WorkflowBuilder
too, you could instead do the nicer
Copy code
workflow(
    ...
) {
    lateinit var deployment: ExternalActionStepWithOutputs<CustomAction.Output>
    job(...) {
        deployment = uses(
            action = CustomAction(
                actionOwner = "actions",
                actionName = "deploy-pages",
                actionVersion = "v1",
                inputs = mapOf()
            )
        )
    }
    env["url"] = expr(deployment.outputs["page_url"])
}.writeToFile()
or even the neat
Copy code
workflow(
    ...
) {
    job(...) {
        val deployment = uses(
            action = CustomAction(
                actionOwner = "actions",
                actionName = "deploy-pages",
                actionVersion = "v1",
                inputs = mapOf()
            )
        )
        this@workflow.env["url"] = expr(deployment.outputs["page_url"])
    }
}.writeToFile()
@Damien O'Hara just in case you missed to follow along, you can already do it, see above 🙂
d

Damien O'Hara

02/13/2023, 3:02 PM
thanks Björn. I have moved on to a new task for now but will give the conversion another go in a few days
d

Damien O'Hara

02/13/2023, 3:59 PM
looking at the generated Yaml I'm not sure this is the same thing. with GitHub pages you set
url
on
jobs.<job_id>.environment
. this
environment
field is different to the
env
field on jobs and workflows. I think it could only be provided through
_customArguments
at the moment
v

Vampire

02/13/2023, 4:01 PM
Oh, it's on the job environment, then it is already nicely supported
Copy code
workflow(
    ...
) {
    job(...) {
        val deployment = uses(
            action = CustomAction(
                actionOwner = "actions",
                actionName = "deploy-pages",
                actionVersion = "v1",
                inputs = mapOf()
            )
        )
        env["url"] = expr(deployment.outputs["page_url"])
    }
}.writeToFile()
d

Damien O'Hara

02/13/2023, 4:03 PM
I am a bit confused here. is this setting
environment
or
env
?
v

Vampire

02/13/2023, 4:12 PM
Oh, I'm stupid today, sorry. How could I confuse
env
with
environment
(damn you GitHub)
You are totally right of course, it has nothing to do with the
env
variables
The whole
environment
key is not supported by this lib yet, so you should definitely open a feature request if there is none yet
d

Damien O'Hara

02/13/2023, 4:13 PM
yes, took me a while to register - still getting my head around how GitHub environments work. could a similar fix work by setting
job._customArguments
after the step builder similar to how env["url"] is set above?
I will take some time to look at it this week and then open a feature request for environment support
v

Vampire

02/13/2023, 4:15 PM
I fear the custom arguments are read-only and only settable from the
job()
call currently
d

Damien O'Hara

02/13/2023, 4:25 PM
this works but relies on the implementation not cloning
_customArguments
Copy code
workflow(
    "Test Workflow",
    on = listOf(Push())
) {
    val jobCustomArguments = mutableMapOf<String, Any?>()

    job("test-job",
        runsOn = RunnerType.UbuntuLatest,
        _customArguments = jobCustomArguments
    ) {
        val deployment = uses(CustomAction(
            actionOwner = "actions",
            actionName = "deploy-pages",
            actionVersion = "v1",
            inputs = mapOf()
        ))

        jobCustomArguments["environment"] = mapOf(
            "url" to expr(deployment.outputs["page_url"])
        )
    }
}
5 Views