Skip to content

Commit 4dd08af

Browse files
committed
Add support calling reusable workflow
1 parent 793c0c0 commit 4dd08af

File tree

5 files changed

+328
-12
lines changed

5 files changed

+328
-12
lines changed

src/main/scala/sbtghactions/GenerativePlugin.scala

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ object GenerativePlugin extends AutoPlugin {
3737
type WorkflowStep = sbtghactions.WorkflowStep
3838
val WorkflowStep = sbtghactions.WorkflowStep
3939

40+
type WorkflowAction = sbtghactions.WorkflowAction
41+
42+
type WorkflowSteps = sbtghactions.WorkflowSteps
43+
val WorkflowSteps = sbtghactions.WorkflowSteps
44+
45+
type WorkflowApply = sbtghactions.WorkflowApply
46+
val WorkflowApply = sbtghactions.WorkflowApply
47+
4048
type RefPredicate = sbtghactions.RefPredicate
4149
val RefPredicate = sbtghactions.RefPredicate
4250

@@ -73,6 +81,9 @@ object GenerativePlugin extends AutoPlugin {
7381
type Concurrency = sbtghactions.Concurrency
7482
val Concurrency = sbtghactions.Concurrency
7583

84+
type Secrets = sbtghactions.Secrets
85+
val Secrets = sbtghactions.Secrets
86+
7687
type Graalvm = sbtghactions.Graalvm
7788
val Graalvm = sbtghactions.Graalvm
7889
}
@@ -187,6 +198,12 @@ object GenerativePlugin extends AutoPlugin {
187198
s"concurrency: ${wrap(concurrency.group)}"
188199
}
189200

201+
def compileSecrets(secrets: Secrets): String =
202+
secrets match {
203+
case Secrets.Inherit => "secrets: inherit"
204+
case Secrets.Explicit(secretMap) => compileEnv(secretMap, "secrets")
205+
}
206+
190207
def compileEnvironment(environment: JobEnvironment): String =
191208
environment.url match {
192209
case Some(url) =>
@@ -355,6 +372,8 @@ ${indent(rendered.mkString("\n"), 1)}"""
355372

356373

357374
def compileJob(job: WorkflowJob, sbt: String): String = {
375+
val renderedName = s"""name: ${wrap(job.name)}"""
376+
358377
val renderedNeeds = if (job.needs.isEmpty)
359378
""
360379
else
@@ -485,24 +504,59 @@ ${indent(rendered.mkString("\n"), 1)}"""
485504

486505
val declareShell = job.oses.exists(_.contains("windows"))
487506

488-
val runsOn = if (job.runsOnExtraLabels.isEmpty)
489-
s"$${{ matrix.os }}"
490-
else
491-
job.runsOnExtraLabels.mkString(s"""[ "$${{ matrix.os }}", """, ", ", " ]" )
507+
val runsOn = job.action match {
508+
case steps: WorkflowSteps if steps.runsOnExtraLabels.isEmpty =>
509+
"\nruns-on: ${{ matrix.os }}"
510+
case steps: WorkflowSteps =>
511+
steps.runsOnExtraLabels.mkString(s"""\nruns-on: [ "$${{ matrix.os }}", """, ", ", " ]")
512+
case _ =>
513+
""
514+
}
492515

493516
val renderedFailFast = job.matrixFailFast.fold("")("\n fail-fast: " + _)
494517

495-
val body = s"""name: ${wrap(job.name)}${renderedNeeds}${renderedCond}
496-
strategy:${renderedFailFast}
518+
val renderedStrategy = s"""\nstrategy:${renderedFailFast}
497519
matrix:
498520
os:${compileList(job.oses, 3)}
499521
scala:${compileList(job.scalas, 3)}
500-
java:${compileList(job.javas.map(_.render), 3)}${renderedMatrices}
501-
runs-on: ${runsOn}${renderedEnvironment}${renderedContainer}${renderedTimeout}${renderedPerm}${renderedEnv}${renderedConcurrency}
502-
steps:
503-
${indent(job.steps.map(compileStep(_, sbt, job.sbtStepPreamble, declareShell = declareShell)).mkString("\n\n"), 1)}"""
522+
java:${compileList(job.javas.map(_.render), 3)}"""
523+
524+
val renderedSteps = job.action match {
525+
case steps: WorkflowSteps =>
526+
"\nsteps:\n" +
527+
indent(steps.steps.map(compileStep(_, sbt, steps.sbtStepPreamble, declareShell = declareShell)).mkString("\n\n"), 1)
528+
case _ =>
529+
""
530+
}
531+
532+
val renderedUses = job.action match {
533+
case apply: WorkflowApply =>
534+
val renderedSecrets =
535+
apply.secrets.map(compileSecrets).map("\n" + _).getOrElse("")
536+
537+
s"\nuses: ${apply.ref}${renderParams(apply.params)}${renderedSecrets}"
538+
case _ =>
539+
""
540+
}
504541

505-
s"${job.id}:\n${indent(body, 1)}"
542+
val content = List(
543+
renderedName,
544+
renderedNeeds,
545+
renderedCond,
546+
renderedStrategy,
547+
renderedMatrices,
548+
runsOn,
549+
renderedEnvironment,
550+
renderedContainer,
551+
renderedTimeout,
552+
renderedPerm,
553+
renderedEnv,
554+
renderedConcurrency,
555+
renderedSteps,
556+
renderedUses,
557+
).reduce(_ ++ _)
558+
559+
s"${job.id}:\n${indent(content, 1)}"
506560
}
507561

508562
def compileWorkflow(
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2020-2021 Daniel Spiewak
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sbtghactions
18+
19+
sealed trait Secrets extends Product with Serializable
20+
21+
object Secrets {
22+
case object Inherit extends Secrets
23+
case class Explicit(secrets: Map[String, String]) extends Secrets
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2020-2021 Daniel Spiewak
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sbtghactions
18+
19+
sealed trait WorkflowAction
20+
21+
final case class WorkflowSteps(
22+
steps: List[WorkflowStep],
23+
sbtStepPreamble: List[String] = List(),
24+
runsOnExtraLabels: List[String] = List(),
25+
) extends WorkflowAction
26+
27+
28+
final case class WorkflowApply(
29+
ref: String,
30+
params: Map[String, String] = Map.empty,
31+
secrets: Option[Secrets] = None,
32+
) extends WorkflowAction

src/main/scala/sbtghactions/WorkflowJob.scala

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,28 @@ package sbtghactions
1919
import scala.concurrent.duration.FiniteDuration
2020

2121
final case class WorkflowJob(
22+
id: String,
23+
name: String,
24+
action: WorkflowAction,
25+
cond: Option[String],
26+
permissions: Option[Permissions],
27+
env: Map[String, String],
28+
oses: List[String],
29+
scalas: List[String],
30+
javas: List[JavaSpec],
31+
needs: List[String],
32+
matrixFailFast: Option[Boolean],
33+
matrixAdds: Map[String, List[String]],
34+
matrixIncs: List[MatrixInclude],
35+
matrixExcs: List[MatrixExclude],
36+
container: Option[JobContainer],
37+
environment: Option[JobEnvironment],
38+
concurrency: Option[Concurrency],
39+
timeout: Option[FiniteDuration],
40+
)
41+
42+
object WorkflowJob {
43+
def apply(
2244
id: String,
2345
name: String,
2446
steps: List[WorkflowStep],
@@ -38,4 +60,70 @@ final case class WorkflowJob(
3860
container: Option[JobContainer] = None,
3961
environment: Option[JobEnvironment] = None,
4062
concurrency: Option[Concurrency] = None,
41-
timeout: Option[FiniteDuration] = None)
63+
timeout: Option[FiniteDuration] = None
64+
): WorkflowJob =
65+
WorkflowJob(
66+
id,
67+
name,
68+
action = WorkflowSteps(steps, sbtStepPreamble, runsOnExtraLabels),
69+
cond,
70+
permissions,
71+
env,
72+
oses,
73+
scalas,
74+
javas,
75+
needs,
76+
matrixFailFast,
77+
matrixAdds,
78+
matrixIncs,
79+
matrixExcs,
80+
container,
81+
environment,
82+
concurrency,
83+
timeout
84+
)
85+
86+
def use(
87+
id: String,
88+
name: String,
89+
ref: String,
90+
params: Map[String, String] = Map.empty,
91+
secrets: Option[Secrets] = None,
92+
cond: Option[String] = None,
93+
permissions: Option[Permissions] = None,
94+
env: Map[String, String] = Map(),
95+
oses: List[String] = List("ubuntu-latest"),
96+
scalas: List[String] = List("2.13.10"),
97+
javas: List[JavaSpec] = List(JavaSpec.zulu("8")),
98+
needs: List[String] = List(),
99+
matrixFailFast: Option[Boolean] = None,
100+
matrixAdds: Map[String, List[String]] = Map(),
101+
matrixIncs: List[MatrixInclude] = List(),
102+
matrixExcs: List[MatrixExclude] = List(),
103+
container: Option[JobContainer] = None,
104+
environment: Option[JobEnvironment] = None,
105+
concurrency: Option[Concurrency] = None,
106+
timeout: Option[FiniteDuration] = None,
107+
): WorkflowJob =
108+
WorkflowJob(
109+
id,
110+
name,
111+
action = WorkflowApply(ref, params, secrets),
112+
cond,
113+
permissions,
114+
env,
115+
oses,
116+
scalas,
117+
javas,
118+
needs,
119+
matrixFailFast,
120+
matrixAdds,
121+
matrixIncs,
122+
matrixExcs,
123+
container,
124+
environment,
125+
concurrency,
126+
timeout
127+
)
128+
129+
}

0 commit comments

Comments
 (0)