Skip to content

Instantly share code, notes, and snippets.

@pierangeloc
Created June 13, 2022 13:37
Show Gist options
  • Save pierangeloc/69ace54dcc78edf9d27db0b27e467d76 to your computer and use it in GitHub Desktop.
Save pierangeloc/69ace54dcc78edf9d27db0b27e467d76 to your computer and use it in GitHub Desktop.
Ammonite script to `tar` all the target files. Useful for caching compilation results in multi stage pipelines. Built for Azure devops, it can be easily modified to cope with other CI platforms
val currentDir= os.pwd
println(os.list(currentDir).toList.map(p => (os.isDir(p), p.relativeTo(currentDir))).mkString("\n"))
def allSubDirectoriesNamed(name: String, p: os.Path): List[os.Path] = {
if (os.isDir(p) && p.last == name) List(p)
else if (os.isDir(p)) {
for {
child <- os.list(p).toList
subDirs <- allSubDirectoriesNamed(name, child)
} yield subDirs
} else List()
}
val allTargetDirs = allSubDirectoriesNamed("target", currentDir)
case class Artifact(publish: os.RelPath, name: String)
def exportYaml(artifact: Artifact, current: String) =
s"""|- publish: ${current}/${artifact.publish}
| artifact: ${artifact.name}""".stripMargin
def importYaml(artifact: Artifact) =
s"""| - download: current
| artifact: ${artifact.name}""".stripMargin
def zipTargetShell(artifacts: List[Artifact]): String =
s"""#! /bin/bash
|set -e
|
|TARGETS="version.txt ${artifacts.map(_.publish).mkString(" ")}"
|
|tar -cf targets-cache.tar $$TARGETS
|""".stripMargin
def makeArtifact(p: os.RelPath): Artifact = Artifact(p, p.segments.toList.mkString("_"))
val azureBuildDirectory = "$(Build.Repository.LocalPath)"
val allArtifacts = allTargetDirs.map(dir => makeArtifact(dir.relativeTo(currentDir)))
val exportAllYaml = allArtifacts.map(a => exportYaml(a, azureBuildDirectory)).mkString("\n")
val importAllYaml = allArtifacts.map(importYaml).mkString("\n")
println(s"All artifacts: \n$allArtifacts\n")
println(s"ExportAll Yaml: \n$exportAllYaml\n")
println(s"ImportAll Yaml: \n$importAllYaml\n")
println(s"zip-target.sh: \n${zipTargetShell(allArtifacts)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment