Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom matrices for artifact upload/download #98

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jobs:
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/series/0.4')
uses: actions/upload-artifact@v2
with:
name: target-${{ matrix.os }}-${{ matrix.scala }}-${{ matrix.java }}
name: target-${{ matrix.os }}-${{ matrix.java }}-${{ matrix.scala }}-${{ matrix.project }}
path: targets.tar

publish:
Expand Down Expand Up @@ -124,12 +124,12 @@ jobs:
~/Library/Caches/Coursier/v1
key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }}

- name: Download target directories (2.12.15)
- name: Download target directories (2.12.15, rootJVM)
uses: actions/download-artifact@v2
with:
name: target-${{ matrix.os }}-2.12.15-${{ matrix.java }}
name: target-${{ matrix.os }}-${{ matrix.java }}-2.12.15-rootJVM

- name: Inflate target directories (2.12.15)
- name: Inflate target directories (2.12.15, rootJVM)
run: |
tar xf targets.tar
rm targets.tar
Expand Down
3 changes: 2 additions & 1 deletion ci/src/main/scala/org/typelevel/sbt/CrossRootProject.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ object TypelevelCiCrossPlugin extends AutoPlugin {

override def buildSettings = Seq(
githubWorkflowBuildSbtStepPreamble ~= { s"project $${{ matrix.project }}" +: _ },
githubWorkflowBuildMatrixAdditions += "project" -> Nil
githubWorkflowBuildMatrixAdditions += "project" -> Nil,
githubWorkflowArtifactDownloadExtraKeys += "project"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ trait GenerativeKeys {

lazy val githubWorkflowArtifactUpload = settingKey[Boolean](
"Controls whether or not to upload target directories in the event that multiple jobs are running sequentially. Can be set on a per-project basis (default: true)")
lazy val githubWorkflowArtifactDownloadExtraKeys = settingKey[Set[String]](
"Additional matrix keys for which *all* artifacts should be downloaded and not just for the primary value (default: {})")
lazy val githubWorkflowJobSetup = settingKey[Seq[WorkflowStep]](
"The automatically-generated checkout, setup, and cache steps which are common to all jobs which touch the build (default: autogenerated)")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}

override def buildSettings = settingDefaults ++ Seq(
githubWorkflowPREventTypes := PREventType.Defaults,
githubWorkflowArtifactDownloadExtraKeys := Set.empty,
githubWorkflowGeneratedUploadSteps := {
if (githubWorkflowArtifactUpload.value) {
val sanitized = pathStrs.value map { str =>
Expand All @@ -582,11 +583,19 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
name = Some("Compress target directories"),
cond = Some(publicationCond.value))

val keys = githubWorkflowBuildMatrixAdditions
.value
.keys
.toList
.sorted
.map(k => s"$${{ matrix.$k }}")
.mkString("-", "-", "")

val upload = WorkflowStep.Use(
UseRef.Public("actions", "upload-artifact", "v2"),
name = Some(s"Upload target directories"),
params = Map(
"name" -> s"target-$${{ matrix.os }}-$${{ matrix.scala }}-$${{ matrix.java }}",
"name" -> s"target-$${{ matrix.os }}-$${{ matrix.java }}-$${{ matrix.scala }}$keys",
"path" -> "targets.tar"),
cond = Some(publicationCond.value)
)
Expand All @@ -597,19 +606,52 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
}
},
githubWorkflowGeneratedDownloadSteps := {
val scalas = githubWorkflowScalaVersions.value
val extraKeys = githubWorkflowArtifactDownloadExtraKeys.value
val additions = githubWorkflowBuildMatrixAdditions.value
val matrix = additions.map {
case (key, values) =>
if (extraKeys(key))
key -> values // we want to iterate over all values
else
key -> values.take(1) // we only want the primary value
}

val keys = "scala" :: additions.keys.toList.sorted
val scalas = githubWorkflowScalaVersions.value.toList
val exclusions = githubWorkflowBuildMatrixExclusions.value

// we build the list of artifacts, by iterating over all combinations of keys
val artifacts = matrix
.toList
.sortBy(_._1)
.map(_._2)
.foldLeft(List(scalas)) { (artifacts, values) =>
for {
artifact <- artifacts
value <- values
} yield artifact :+ value
} // then, we filter artifacts for keys that are excluded from the matrix
.filterNot { artifact =>
val job = keys.zip(artifact).toMap
exclusions.exists { // there is an exclude that matches the current job
case MatrixExclude(matching) => matching.toSet.subsetOf(job.toSet)
}
}

if (githubWorkflowArtifactUpload.value) {
scalas flatMap { v =>
artifacts flatMap { v =>
val pretty = v.mkString(", ")

val download = WorkflowStep.Use(
UseRef.Public("actions", "download-artifact", "v2"),
name = Some(s"Download target directories ($v)"),
params = Map("name" -> s"target-$${{ matrix.os }}-$v-$${{ matrix.java }}")
name = Some(s"Download target directories ($pretty)"),
params = Map(
"name" -> s"target-$${{ matrix.os }}-$${{ matrix.java }}${v.mkString("-", "-", "")}")
)

val untar = WorkflowStep.Run(
List("tar xf targets.tar", "rm targets.tar"),
name = Some(s"Inflate target directories ($v)"))
name = Some(s"Inflate target directories ($pretty)"))

Seq(download, untar)
}
Expand Down