Skip to content

Commit aeb2e20

Browse files
authored
fix(library)!: allow running Kotlin-based step if multiple workflows in single file (#1870)
Resolves #1811. Includes target workflow name in an environment variable used to select which Kotlin-based step to run. Before this fix, if there were multiple workflows per a .main.kts file, there was a runtime error. Thanks to this fix, things work fine as long as each workflow has a different `targetFileName`.
1 parent b677ddd commit aeb2e20

File tree

5 files changed

+110
-4
lines changed

5 files changed

+110
-4
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# This file was generated using Kotlin DSL (.github/workflows/end-to-end-tests.main.kts).
2+
# If you want to modify the workflow, please change the Kotlin file and regenerate this YAML file.
3+
# Generated with https://github.com/typesafegithub/github-workflows-kt
4+
5+
name: 'End-to-end tests (2nd workflow)'
6+
on:
7+
push:
8+
branches:
9+
- 'main'
10+
pull_request: {}
11+
jobs:
12+
check_yaml_consistency:
13+
name: 'Check YAML consistency'
14+
runs-on: 'ubuntu-latest'
15+
steps:
16+
- id: 'step-0'
17+
name: 'Check out'
18+
uses: 'actions/checkout@v4'
19+
- id: 'step-1'
20+
name: 'Set up JDK'
21+
uses: 'actions/setup-java@v4'
22+
with:
23+
java-version: '11'
24+
distribution: 'zulu'
25+
- id: 'step-2'
26+
uses: 'gradle/actions/setup-gradle@v4'
27+
- id: 'step-3'
28+
name: 'Publish to Maven local'
29+
run: './gradlew publishToMavenLocal'
30+
- id: 'step-4'
31+
name: 'Execute script'
32+
run: 'rm ''.github/workflows/end-to-end-tests-2nd-workflow.yaml'' && ''.github/workflows/end-to-end-tests.main.kts'''
33+
- id: 'step-5'
34+
name: 'Consistency check'
35+
run: 'git diff --exit-code ''.github/workflows/end-to-end-tests-2nd-workflow.yaml'''
36+
another_job:
37+
runs-on: 'ubuntu-latest'
38+
needs:
39+
- 'check_yaml_consistency'
40+
steps:
41+
- id: 'step-0'
42+
name: 'Check out'
43+
uses: 'actions/checkout@v4'
44+
- id: 'step-1'
45+
name: 'Set up JDK'
46+
uses: 'actions/setup-java@v4'
47+
with:
48+
java-version: '11'
49+
distribution: 'zulu'
50+
- id: 'step-2'
51+
uses: 'gradle/actions/setup-gradle@v4'
52+
- id: 'step-3'
53+
name: 'Publish to Maven local'
54+
run: './gradlew publishToMavenLocal'
55+
- id: 'step-4'
56+
name: 'Step with a Kotlin-based logic, in a different workflow'
57+
env:
58+
GHWKT_GITHUB_CONTEXT_JSON: '${{ toJSON(github) }}'
59+
run: 'GHWKT_RUN_STEP=''end-to-end-tests-2nd-workflow.yaml:another_job:step-4'' ''.github/workflows/end-to-end-tests.main.kts'''

.github/workflows/end-to-end-tests.main.kts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,36 @@ workflow(
273273
)
274274
}
275275
}
276+
277+
workflow(
278+
name = "End-to-end tests (2nd workflow)",
279+
on = listOf(
280+
Push(branches = listOf("main")),
281+
PullRequest(),
282+
),
283+
consistencyCheckJobConfig = DEFAULT_CONSISTENCY_CHECK_JOB_CONFIG.copy(
284+
additionalSteps = {
285+
publishToMavenLocal()
286+
},
287+
),
288+
sourceFile = __FILE__,
289+
targetFileName = "end-to-end-tests-2nd-workflow.yaml",
290+
) {
291+
job(
292+
id = "another_job",
293+
runsOn = RunnerType.UbuntuLatest,
294+
) {
295+
uses(
296+
name = "Check out",
297+
action = Checkout(),
298+
)
299+
300+
publishToMavenLocal()
301+
302+
run(
303+
name = "Step with a Kotlin-based logic, in a different workflow",
304+
) {
305+
println("Hello from Kotlin!")
306+
}
307+
}
308+
}

.github/workflows/end-to-end-tests.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ jobs:
131131
name: 'Evaluating condition for ''Step with a Kotlin-based logic'''
132132
env:
133133
GHWKT_GITHUB_CONTEXT_JSON: '${{ toJSON(github) }}'
134-
run: 'GHWKT_RUN_STEP=''test_job_1:step-18'' ''.github/workflows/end-to-end-tests.main.kts'''
134+
run: 'GHWKT_RUN_STEP=''end-to-end-tests.yaml:test_job_1:step-18'' ''.github/workflows/end-to-end-tests.main.kts'''
135135
- id: 'step-19'
136136
name: 'Step with a Kotlin-based logic'
137137
env:
138138
GHWKT_GITHUB_CONTEXT_JSON: '${{ toJSON(github) }}'
139-
run: 'GHWKT_RUN_STEP=''test_job_1:step-19'' ''.github/workflows/end-to-end-tests.main.kts'''
139+
run: 'GHWKT_RUN_STEP=''end-to-end-tests.yaml:test_job_1:step-19'' ''.github/workflows/end-to-end-tests.main.kts'''
140140
if: '${{ steps.step-18.outputs.evaluation-result }}'
141141
- id: 'step-20'
142142
uses: 'actions/github-script@v7'

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,17 @@ public class JobBuilder<OUTPUT : JobOutputs>(
142142
workflowBuilder.gitRootDir?.let {
143143
sourceFile.toPath().relativeToAbsolute(it).invariantSeparatorsPathString
144144
}
145+
val commandParts =
146+
listOf(
147+
this.workflowBuilder.workflow.targetFileName,
148+
this.id,
149+
id,
150+
)
145151
val newStep =
146152
KotlinLogicStep(
147153
id = id,
148154
name = name,
149-
command = "GHWKT_RUN_STEP='${this.id}:$id' '$sourceFilePath'",
155+
command = "GHWKT_RUN_STEP='${commandParts.joinToString(":")}' '$sourceFilePath'",
150156
logic = logic,
151157
env = env + mapOf("GHWKT_GITHUB_CONTEXT_JSON" to "\${{ toJSON(github) }}"),
152158
condition =

github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/ToYaml.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ internal fun Workflow.writeToFile(
3737
val runStepEnvVar = getenv("GHWKT_RUN_STEP")
3838

3939
if (runStepEnvVar != null) {
40-
val (jobId, stepId) = runStepEnvVar.split(":")
40+
val (targetFileName, jobId, stepId) = runStepEnvVar.split(":")
41+
42+
if (this.targetFileName != targetFileName) {
43+
// Handles a case where there are multiple workflows with Kotlin-based
44+
// steps in a single .main.kts file. Thanks to this check, only the step
45+
// from the desired workflow is run.
46+
return
47+
}
48+
4149
val kotlinLogicStep =
4250
this.jobs
4351
.first { it.id == jobId }

0 commit comments

Comments
 (0)