Skip to content

Commit 0406c8d

Browse files
authored
feat(library): allow building ActionStep outside of job (#1785)
Closes #1780.
1 parent cb94d37 commit 0406c8d

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

github-workflows-kt/api/github-workflows-kt.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,7 @@ public final class io/github/typesafegithub/workflows/dsl/JobBuilder : io/github
18571857
public final fun run ([Lkotlin/Unit;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Ljava/lang/Boolean;Ljava/lang/Integer;Lio/github/typesafegithub/workflows/domain/Shell;Ljava/lang/String;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Lio/github/typesafegithub/workflows/domain/KotlinLogicStep;
18581858
public static synthetic fun run$default (Lio/github/typesafegithub/workflows/dsl/JobBuilder;[Lkotlin/Unit;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Integer;Lio/github/typesafegithub/workflows/domain/Shell;Ljava/lang/String;Ljava/util/Map;ILjava/lang/Object;)Lio/github/typesafegithub/workflows/domain/CommandStep;
18591859
public static synthetic fun run$default (Lio/github/typesafegithub/workflows/dsl/JobBuilder;[Lkotlin/Unit;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Ljava/lang/Boolean;Ljava/lang/Integer;Lio/github/typesafegithub/workflows/domain/Shell;Ljava/lang/String;Ljava/util/Map;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lio/github/typesafegithub/workflows/domain/KotlinLogicStep;
1860+
public final fun uses (Lio/github/typesafegithub/workflows/domain/ActionStep;)V
18601861
public final fun uses ([Lkotlin/Unit;Lio/github/typesafegithub/workflows/domain/actions/Action;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Integer;Ljava/util/Map;)Lio/github/typesafegithub/workflows/domain/ActionStep;
18611862
public static synthetic fun uses$default (Lio/github/typesafegithub/workflows/dsl/JobBuilder;[Lkotlin/Unit;Lio/github/typesafegithub/workflows/domain/actions/Action;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Integer;Ljava/util/Map;ILjava/lang/Object;)Lio/github/typesafegithub/workflows/domain/ActionStep;
18621863
}

github-workflows-kt/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dependencies {
3434

3535
// GitHub action bindings
3636
testImplementation("actions:checkout:v4")
37+
testImplementation("actions:deploy-pages:v4")
3738
testImplementation("actions:setup-java:v4")
3839
testImplementation("actions:upload-artifact:v3")
3940
testImplementation("aws-actions:configure-aws-credentials:v4")

github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/dsl/JobBuilder.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,13 @@ public class JobBuilder<OUTPUT : JobOutputs>(
200200
return newStep
201201
}
202202

203+
/**
204+
* Useful when you need to build your action step outside the job, then you can call this function to register the
205+
* step in the job.
206+
*/
207+
public fun <T : Action.Outputs> uses(actionStep: ActionStep<T>) {
208+
job = job.copy(steps = job.steps + actionStep)
209+
}
210+
203211
public fun build(): Job<OUTPUT> = job
204212
}

github-workflows-kt/src/test/kotlin/io/github/typesafegithub/workflows/dsl/JobBuilderTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package io.github.typesafegithub.workflows.dsl
22

33
import io.github.typesafegithub.workflows.actions.actions.Checkout
4+
import io.github.typesafegithub.workflows.actions.actions.DeployPages
45
import io.github.typesafegithub.workflows.actions.actions.SetupJava
56
import io.github.typesafegithub.workflows.actions.actions.SetupJava.Distribution.Adopt
7+
import io.github.typesafegithub.workflows.domain.ActionStep
8+
import io.github.typesafegithub.workflows.domain.Environment
69
import io.github.typesafegithub.workflows.domain.RunnerType
710
import io.github.typesafegithub.workflows.domain.Workflow
811
import io.github.typesafegithub.workflows.domain.triggers.Push
12+
import io.github.typesafegithub.workflows.dsl.expressions.expr
913
import io.kotest.assertions.throwables.shouldThrow
1014
import io.kotest.core.spec.style.FunSpec
1115
import io.kotest.engine.spec.tempdir
@@ -209,4 +213,36 @@ class JobBuilderTest :
209213
}
210214
}
211215
}
216+
217+
test("action output passed to environment") {
218+
// When
219+
var workflow: Workflow? = null
220+
workflow(
221+
name = "test",
222+
on = listOf(Push()),
223+
sourceFile = sourceTempFile,
224+
useWorkflow = { workflow = it },
225+
) {
226+
val deploymentStep =
227+
ActionStep(
228+
id = "deployment",
229+
name = "Deploy to GitHub Pages",
230+
action = DeployPages(),
231+
)
232+
job(
233+
id = "deploy",
234+
runsOn = RunnerType.UbuntuLatest,
235+
environment =
236+
Environment(
237+
name = "github-pages",
238+
url = expr(deploymentStep.outputs.pageUrl),
239+
),
240+
) {
241+
uses(actionStep = deploymentStep)
242+
}
243+
}
244+
245+
// Then
246+
workflow!!.jobs[0].environment?.url shouldBe "${'$'}{{ steps.deployment.outputs.page_url }}"
247+
}
212248
})

0 commit comments

Comments
 (0)