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.*
1112import 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
1216import io.github.typesafegithub.workflows.domain.RunnerType
17+ import io.github.typesafegithub.workflows.domain.actions.*
1318import io.github.typesafegithub.workflows.domain.triggers.PullRequest
1419import 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
1522import io.github.typesafegithub.workflows.dsl.workflow
1623import 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()
0 commit comments