Skip to content

Commit fd53d15

Browse files
authored
test: move tests ran on GitHub to end-to-end workflow (#1416)
It will let us test mechanisms present in Kotlin scripting but not present in regular Kotlin.
1 parent ed8f4d1 commit fd53d15

File tree

3 files changed

+285
-260
lines changed

3 files changed

+285
-260
lines changed

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

Lines changed: 188 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
@file:DependsOn("io.github.typesafegithub:github-workflows-kt:1.14.1-SNAPSHOT")
44
@file:Repository("https://github-workflows-kt-bindings.colman.com.br/binding/")
55
@file:DependsOn("actions:checkout:v4")
6+
@file:DependsOn("actions:github-script:v7")
67
@file:DependsOn("actions:setup-java:v4")
8+
@file:DependsOn("actions:setup-python:v5")
79
@file:DependsOn("gradle:actions__setup-gradle:v3")
810

9-
import io.github.typesafegithub.workflows.actions.actions.Checkout
10-
import io.github.typesafegithub.workflows.actions.actions.SetupJava
11+
import io.github.typesafegithub.workflows.actions.actions.*
1112
import io.github.typesafegithub.workflows.actions.gradle.ActionsSetupGradle
13+
import io.github.typesafegithub.workflows.domain.JobOutputs
14+
import io.github.typesafegithub.workflows.domain.Mode
15+
import io.github.typesafegithub.workflows.domain.Permission
1216
import io.github.typesafegithub.workflows.domain.RunnerType
17+
import io.github.typesafegithub.workflows.domain.actions.*
1318
import io.github.typesafegithub.workflows.domain.triggers.PullRequest
1419
import io.github.typesafegithub.workflows.domain.triggers.Push
20+
import io.github.typesafegithub.workflows.dsl.expressions.Contexts
21+
import io.github.typesafegithub.workflows.dsl.expressions.expr
1522
import io.github.typesafegithub.workflows.dsl.workflow
1623
import io.github.typesafegithub.workflows.yaml.writeToFile
1724

@@ -37,12 +44,187 @@ workflow(
3744
},
3845
sourceFile = __FILE__.toPath(),
3946
) {
47+
val GREETING by Contexts.env
48+
val FIRST_NAME by Contexts.env
49+
val SECRET by Contexts.env
50+
val TOKEN by Contexts.env
51+
val SUPER_SECRET by Contexts.secrets
52+
53+
val testJob1 =
54+
job(
55+
id = "test_job_1",
56+
runsOn = RunnerType.UbuntuLatest,
57+
env = linkedMapOf(
58+
GREETING to "World",
59+
),
60+
permissions = mapOf(
61+
Permission.Actions to Mode.Read,
62+
Permission.Checks to Mode.Write,
63+
Permission.Contents to Mode.None,
64+
),
65+
outputs = object : JobOutputs() {
66+
var scriptKey by output()
67+
var scriptKey2 by output()
68+
var scriptResult by output()
69+
},
70+
) {
71+
run(
72+
name = "Hello world!",
73+
command = "echo 'hello!'",
74+
)
75+
76+
run(
77+
name = "Hello world! Multiline command with pipes",
78+
command = """
79+
less test.txt \
80+
| grep -P "foobar" \
81+
| sort \
82+
> result.txt
83+
""".trimIndent(),
84+
)
85+
86+
uses(
87+
name = "Check out",
88+
action = CustomAction(
89+
actionOwner = "actions",
90+
actionName = "checkout",
91+
actionVersion = "v4",
92+
),
93+
)
94+
95+
uses(
96+
name = "Run local action",
97+
action = CustomLocalAction(
98+
actionPath = "./.github/workflows/test-local-action",
99+
inputs = mapOf(
100+
"name" to "Rocky",
101+
),
102+
),
103+
)
104+
105+
uses(
106+
name = "Run alpine",
107+
action = CustomDockerAction(
108+
actionImage = "alpine",
109+
actionTag = "latest",
110+
),
111+
)
112+
113+
uses(
114+
name = "Check out again",
115+
action = object : RegularAction<Action.Outputs>(
116+
actionOwner = "actions",
117+
actionName = "checkout",
118+
actionVersion = "v4",
119+
) {
120+
override fun toYamlArguments() =
121+
linkedMapOf(
122+
"repository" to "actions/checkout",
123+
"ref" to "v3",
124+
"path" to "./.github/actions/checkout",
125+
"clean" to "false",
126+
)
127+
128+
override fun buildOutputObject(stepId: String) = Action.Outputs(stepId)
129+
},
130+
)
131+
132+
uses(
133+
name = "Run local action",
134+
action = object : LocalAction<Action.Outputs>(
135+
actionPath = "./.github/workflows/test-local-action",
136+
) {
137+
override fun toYamlArguments() =
138+
linkedMapOf(
139+
"name" to "Balboa",
140+
)
141+
142+
override fun buildOutputObject(stepId: String) = Action.Outputs(stepId)
143+
},
144+
)
145+
146+
uses(
147+
name = "Run alpine",
148+
action = object : DockerAction<Action.Outputs>(
149+
actionImage = "alpine",
150+
actionTag = "latest",
151+
) {
152+
override fun toYamlArguments() = linkedMapOf<String, String>()
153+
154+
override fun buildOutputObject(stepId: String) = Action.Outputs(stepId)
155+
},
156+
)
157+
158+
val addAndCommit = uses(action = SetupPython())
159+
160+
uses(
161+
name = "Some step consuming other step's output",
162+
action = Checkout(
163+
sshKey = expr(addAndCommit.outputs.pythonVersion),
164+
path = expr(addAndCommit.outputs["my-unsafe-output"]),
165+
),
166+
)
167+
168+
run(
169+
name = "Custom environment variable",
170+
env = linkedMapOf(
171+
FIRST_NAME to "Patrick",
172+
),
173+
command = "echo $GREETING $FIRST_NAME",
174+
)
175+
run(
176+
name = "Encrypted secret",
177+
env = linkedMapOf(
178+
SECRET to expr { SUPER_SECRET },
179+
TOKEN to expr { secrets.GITHUB_TOKEN },
180+
),
181+
command = "echo secret=$SECRET token=$TOKEN",
182+
)
183+
run(
184+
name = "RunnerContext create temp directory",
185+
command = "mkdir " + expr { runner.temp } + "/build_logs",
186+
)
187+
run(
188+
name = "GitHubContext echo sha",
189+
command = "echo " + expr { github.sha } + " ev " + expr { github.eventRelease.release.url },
190+
)
191+
run(
192+
name = "Default environment variable",
193+
command = "action=${Contexts.env.GITHUB_ACTION} repo=${Contexts.env.GITHUB_REPOSITORY}",
194+
condition = expr { always() },
195+
)
196+
val scriptStep =
197+
uses(
198+
action = GithubScript(
199+
script = """
200+
core.setOutput("key", "value")
201+
core.setOutput("key2", "value2")
202+
return "return"
203+
""".trimIndent(),
204+
),
205+
)
206+
jobOutputs.scriptKey = scriptStep.outputs["key"]
207+
jobOutputs.scriptKey2 = scriptStep.outputs["key2"]
208+
jobOutputs.scriptResult = scriptStep.outputs.result
209+
}
210+
40211
job(
41-
id = "main-job",
42-
name = "Main job",
212+
id = "test_job_2",
43213
runsOn = RunnerType.UbuntuLatest,
214+
condition = "\${{ always() }}",
215+
needs = listOf(testJob1),
44216
) {
45-
uses(action = Checkout())
46-
run(command = "echo 'Hello world!'")
217+
run(
218+
name = "Hello world, again!",
219+
command = "echo 'hello again!'",
220+
)
221+
run(
222+
name = "use output of script",
223+
command = """
224+
echo ${expr { testJob1.outputs.scriptKey }}
225+
echo ${expr { testJob1.outputs.scriptKey2 }}
226+
echo ${expr { testJob1.outputs.scriptResult }}
227+
""".trimIndent(),
228+
)
47229
}
48230
}.writeToFile()

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

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,107 @@ jobs:
3535
- id: 'step-5'
3636
name: 'Consistency check'
3737
run: 'git diff --exit-code ''.github/workflows/end-to-end-tests.yaml'''
38-
main-job:
39-
name: 'Main job'
38+
test_job_1:
4039
runs-on: 'ubuntu-latest'
40+
permissions:
41+
actions: 'read'
42+
checks: 'write'
43+
contents: 'none'
4144
needs:
4245
- 'check_yaml_consistency'
46+
env:
47+
$GREETING: 'World'
48+
outputs:
49+
scriptKey: '${{ steps.step-15.outputs.key }}'
50+
scriptKey2: '${{ steps.step-15.outputs.key2 }}'
51+
scriptResult: '${{ steps.step-15.outputs.result }}'
4352
steps:
4453
- id: 'step-0'
54+
name: 'Hello world!'
55+
run: 'echo ''hello!'''
56+
- id: 'step-1'
57+
name: 'Hello world! Multiline command with pipes'
58+
run: |-
59+
less test.txt \
60+
| grep -P "foobar" \
61+
| sort \
62+
> result.txt
63+
- id: 'step-2'
64+
name: 'Check out'
65+
uses: 'actions/checkout@v4'
66+
- id: 'step-3'
67+
name: 'Run local action'
68+
uses: './.github/workflows/test-local-action'
69+
with:
70+
name: 'Rocky'
71+
- id: 'step-4'
72+
name: 'Run alpine'
73+
uses: 'docker://alpine:latest'
74+
- id: 'step-5'
75+
name: 'Check out again'
76+
uses: 'actions/checkout@v4'
77+
with:
78+
repository: 'actions/checkout'
79+
ref: 'v3'
80+
path: './.github/actions/checkout'
81+
clean: 'false'
82+
- id: 'step-6'
83+
name: 'Run local action'
84+
uses: './.github/workflows/test-local-action'
85+
with:
86+
name: 'Balboa'
87+
- id: 'step-7'
88+
name: 'Run alpine'
89+
uses: 'docker://alpine:latest'
90+
- id: 'step-8'
91+
uses: 'actions/setup-python@v5'
92+
- id: 'step-9'
93+
name: 'Some step consuming other step''s output'
4594
uses: 'actions/checkout@v4'
95+
with:
96+
ssh-key: '${{ steps.step-8.outputs.python-version }}'
97+
path: '${{ steps.step-8.outputs.my-unsafe-output }}'
98+
- id: 'step-10'
99+
name: 'Custom environment variable'
100+
env:
101+
$FIRST_NAME: 'Patrick'
102+
run: 'echo $GREETING $FIRST_NAME'
103+
- id: 'step-11'
104+
name: 'Encrypted secret'
105+
env:
106+
$SECRET: '${{ secrets.SUPER_SECRET }}'
107+
$TOKEN: '${{ secrets.GITHUB_TOKEN }}'
108+
run: 'echo secret=$SECRET token=$TOKEN'
109+
- id: 'step-12'
110+
name: 'RunnerContext create temp directory'
111+
run: 'mkdir ${{ runner.temp }}/build_logs'
112+
- id: 'step-13'
113+
name: 'GitHubContext echo sha'
114+
run: 'echo ${{ github.sha }} ev ${{ github.event.release.url }}'
115+
- id: 'step-14'
116+
name: 'Default environment variable'
117+
run: 'action=$GITHUB_ACTION repo=$GITHUB_REPOSITORY'
118+
if: '${{ always() }}'
119+
- id: 'step-15'
120+
uses: 'actions/github-script@v7'
121+
with:
122+
script: |-
123+
core.setOutput("key", "value")
124+
core.setOutput("key2", "value2")
125+
return "return"
126+
test_job_2:
127+
runs-on: 'ubuntu-latest'
128+
needs:
129+
- 'test_job_1'
130+
- 'check_yaml_consistency'
131+
if: '${{ always() }}'
132+
steps:
133+
- id: 'step-0'
134+
name: 'Hello world, again!'
135+
run: 'echo ''hello again!'''
46136
- id: 'step-1'
47-
run: 'echo ''Hello world!'''
137+
name: 'use output of script'
138+
run: |-
139+
echo ${{ needs.test_job_1.outputs.scriptKey }}
140+
echo ${{ needs.test_job_1.outputs.scriptKey2 }}
141+
echo ${{ needs.test_job_1.outputs.scriptResult }}

0 commit comments

Comments
 (0)