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

flag to update latest tag #317

Merged
merged 2 commits into from
Aug 31, 2014
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
26 changes: 20 additions & 6 deletions src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ trait DockerPlugin extends Plugin with UniversalPlugin {
}
}

def publishLocalDocker(context: File, tag: String, log: Logger): Unit = {
def publishLocalDocker(context: File, tag: String, latest: Boolean, log: Logger): Unit = {
val cmd = Seq("docker", "build", "--force-rm", "-t", tag, ".")
val cwd = context.getParentFile

Expand All @@ -105,6 +105,15 @@ trait DockerPlugin extends Plugin with UniversalPlugin {
throw new RuntimeException("Nonzero exit value: " + ret)
else
log.info("Built image " + tag)

if (latest) {
val name = tag.substring(0, tag.lastIndexOf(":")) + ":latest"
val latestCmd = Seq("docker", "tag", tag, name)
Process(latestCmd).! match {
case 0 => log.info("Update Latest from image" + tag)
case n => sys.error("Failed to run docker tag")
}
}
}

def publishDocker(tag: String, log: Logger): Unit = {
Expand Down Expand Up @@ -152,6 +161,7 @@ trait DockerPlugin extends Plugin with UniversalPlugin {
packageName in Docker <<= packageName,
executableScriptName in Docker <<= executableScriptName,
dockerRepository := None,
dockerUpdateLatest := false,
sourceDirectory in Docker <<= sourceDirectory apply (_ / "docker"),
target in Docker <<= target apply (_ / "docker"),

Expand Down Expand Up @@ -180,13 +190,17 @@ trait DockerPlugin extends Plugin with UniversalPlugin {
(repo, name, version) =>
repo.map(_ + "/").getOrElse("") + name + ":" + version
},
publishLocal <<= (dockerGenerateConfig, dockerGenerateContext, dockerTarget, streams) map {
(config, _, target, s) =>
publishLocalDocker(config, target, s.log)
publishLocal <<= (dockerGenerateConfig, dockerGenerateContext, dockerTarget, dockerUpdateLatest, streams) map {
(config, _, target, updateLatest, s) =>
publishLocalDocker(config, target, updateLatest, s.log)
},
publish <<= (publishLocal, dockerTarget, streams) map {
(_, target, s) =>
publish <<= (publishLocal, dockerTarget, dockerUpdateLatest, streams) map {
(_, target, updateLatest, s) =>
publishDocker(target, s.log)
if (updateLatest) {
val name = target.substring(0, target.lastIndexOf(":")) + ":latest"
publishDocker(name, s.log)
}
}
))
}
1 change: 1 addition & 0 deletions src/main/scala/com/typesafe/sbt/packager/docker/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ trait DockerKeys {
val dockerExposedPorts = SettingKey[Seq[Int]]("dockerExposedPorts", "Ports exposed by Docker image")
val dockerExposedVolumes = SettingKey[Seq[String]]("dockerExposedVolumes", "Volumes exposed by Docker image")
val dockerRepository = SettingKey[Option[String]]("dockerRepository", "Repository for published Docker image")
val dockerUpdateLatest = SettingKey[Boolean]("dockerUpdateLatest", "Set to update latest tag")
}

object Keys extends DockerKeys {
Expand Down
12 changes: 12 additions & 0 deletions src/sbt-test/docker/update-latest/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import com.typesafe.sbt.SbtNativePackager._
import NativePackagerKeys._

packageArchetype.java_application

name := "docker-test"

version := "0.1.0"

maintainer := "Gary Coady <[email protected]>"

dockerUpdateLatest := true
1 change: 1 addition & 0 deletions src/sbt-test/docker/update-latest/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))
3 changes: 3 additions & 0 deletions src/sbt-test/docker/update-latest/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Main extends App {
println("Hello world")
}
3 changes: 3 additions & 0 deletions src/sbt-test/docker/update-latest/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generate the Docker image locally
> docker:publishLocal
$ exec bash -c 'docker run docker-test:latest | grep -q "Hello world"'
2 changes: 2 additions & 0 deletions src/sphinx/DetailedTopics/docker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Publishing Settings
``dockerRepository``
The repository to which the image is pushed when the ``docker:publish`` task is run. This should be of the form ``[username]`` (assumes use of the ``index.docker.io`` repository) or ``[repository.host]/[username]``.

``dockerUpdateLatest``
The flag to automatic update the latest tag when the ``docker:publish`` task is run. Default value is ``FALSE``.

Tasks
-----
Expand Down