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

Ability to set arbitrary tags for the image #7

Merged
merged 4 commits into from
May 20, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ Add ECR settings to your `build.sbt`. The following snippet assumes a Docker ima
// Authenticate and publish a local Docker image before pushing to ECR
push in ecr <<= (push in ecr) dependsOn (publishLocal in Docker, login in ecr)

## Tagging

By default, the produced image will be tagged as "latest". It is possible to provide arbitrary additional tags,
for example to add the version tag to the image:

repositoryTags in ecr ++= Seq(version.value)

If you don't want latest tag on your image you could override the ```repositoryTags``` value completely:

repositoryTags in ecr := Seq(version.value)

30 changes: 17 additions & 13 deletions src/main/scala/sbtecr/EcrPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ object EcrPlugin extends AutoPlugin {
lazy val region = settingKey[Region]("Amazon EC2 region.")
lazy val repositoryName = settingKey[String]("Amazon ECR repository name.")
lazy val localDockerImage = settingKey[String]("Local Docker image.")
lazy val repositoryTags = settingKey[Seq[String]]("Tags managed in the Amazon ECR repository")

lazy val createRepository = taskKey[Unit]("Create a repository in Amazon ECR.")
lazy val login = taskKey[Unit]("Login to Amazon ECR.")
Expand All @@ -25,7 +26,7 @@ object EcrPlugin extends AutoPlugin {
override lazy val projectSettings = inConfig(ecr)(defaultSettings ++ tasks)

lazy val defaultSettings: Seq[Def.Setting[_]] = Seq(
version := "latest",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can safely remove import sbt.Keys.version after removing this setting. Also, please update the README.md file as it still references version in the example.

repositoryTags := List("latest"),
localDockerImage := s"${repositoryName.value}:${version.value}"
)

Expand All @@ -51,19 +52,22 @@ object EcrPlugin extends AutoPlugin {
val accountId = Sts.accountId(region.value)

val src = localDockerImage.value
val dst = s"${Ecr.domain(region.value, accountId)}/${repositoryName.value}:${version.value}"
def destination(tag: String) = s"${Ecr.domain(region.value, accountId)}/${repositoryName.value}:$tag"

val tag = List("docker", "tag", src, dst)
Process(tag)! match {
case 0 =>
val push = List("docker", "push", dst)
Process(push)! match {
case 0 =>
case _ =>
sys.error(s"Pushing failed. Command: ${push.mkString(" ")}")
}
case _ =>
sys.error(s"Tagging failed. Command: ${tag.mkString(" ")}")
repositoryTags.value.foreach { tag =>
val dst = destination(tag)
val command = List("docker", "tag", src, dst)
Process(command) ! match {
case 0 =>
val push = List("docker", "push", dst)
Process(push) ! match {
case 0 =>
case _ =>
sys.error(s"Pushing failed. Command: ${push.mkString(" ")}")
}
case _ =>
sys.error(s"Tagging failed. Command: ${command.mkString(" ")}")
}
}
}
)
Expand Down