Skip to content

Commit

Permalink
Add support for docker:publish in DockerSpotifyClientPlugin (#1338)
Browse files Browse the repository at this point in the history
This adds support for pushing built tags in DockerSpotifyClientPlugin.
Additionally, this introduces a small refactoring to extract the
definition of the ProgressHandler, used by both building and pushing
tasks to log progress.
  • Loading branch information
pdalpra authored May 11, 2020
1 parent d345b4d commit 625b93c
Showing 1 changed file with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ object DockerSpotifyClientPlugin extends AutoPlugin {
def clientSettings =
Seq(
publishLocal := publishLocalDocker.value,
publish := publishDocker.value,
dockerVersion := dockerServerVersion.value,
dockerApiVersion := dockerServerApiVersion.value
)
Expand All @@ -71,6 +72,15 @@ object DockerSpotifyClientPlugin extends AutoPlugin {
docker.packageDocker(primaryAlias, aliases, dockerDirectory, log)
}

def publishDocker: Def.Initialize[Task[Unit]] = Def.task {
val _ = publishLocal.value
val aliases = dockerAliases.value
val log = streams.value.log

val docker = new DockerClientTask()
docker.publishDocker(aliases, log)
}

def dockerServerVersion: Def.Initialize[Task[Option[DockerVersion]]] = Def.task {
val docker = new DockerClientTask()
docker.dockerServerVersion()
Expand Down Expand Up @@ -104,19 +114,23 @@ private class DockerClientTask {

log.info(s"PublishLocal using Docker API ${docker.version().apiVersion()}")

docker.build(Paths.get(dockerDirectory), primaryAlias.toString, new ProgressHandler {
override def progress(message: ProgressMessage): Unit =
Option(message.error()) match {
case Some(error) if error.nonEmpty => log.error(message.error())
case _ => Option(message.stream()) foreach (v => log.info(v))
}
}, BuildParam.forceRm())
docker.build(Paths.get(dockerDirectory), primaryAlias.toString, progressHandler(log), BuildParam.forceRm())

aliases.foreach { tag =>
docker.tag(primaryAlias.toString, tag.toString, true)
}
}

def publishDocker(aliases: Seq[DockerAlias], log: Logger): Unit = {
val docker: DockerClient = DefaultDockerClient.fromEnv().build()

log.info(s"Publish using Docker API ${docker.version().apiVersion()}")

aliases.foreach { tag =>
docker.push(tag.toString, progressHandler(log))
}
}

def dockerServerVersion(): Option[DockerVersion] = {
val docker: DockerClient = DefaultDockerClient.fromEnv().build()
DockerVersion.parse(docker.version().version())
Expand All @@ -126,4 +140,12 @@ private class DockerClientTask {
val docker: DockerClient = DefaultDockerClient.fromEnv().build()
DockerApiVersion.parse(docker.version().apiVersion())
}

private def progressHandler(log: Logger) = new ProgressHandler {
override def progress(message: ProgressMessage): Unit =
Option(message.error()) match {
case Some(error) if error.nonEmpty => log.error(message.error())
case _ => Option(message.stream()) foreach (v => log.info(v))
}
}
}

0 comments on commit 625b93c

Please sign in to comment.