Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit 7587444

Browse files
authored
Merge pull request #94 from Tapad/feature/versionTagOverride
Adding 'composeServiceVersionTask' to allow for overrriding of the de…
2 parents a846b53 + 8db7f43 commit 7587444

File tree

7 files changed

+19
-1
lines changed

7 files changed

+19
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ plugin will attempt to locate it in one of three places with the precedence orde
5858
```
5959
composeFile := // Specify the full path to the Compose File to use to create your test instance. It defaults to docker-compose.yml in your resources folder.
6060
composeServiceName := // Specify the name of the service in the Docker Compose file being tested. This setting prevents the service image from being pull down from the Docker Registry. It defaults to the sbt Project name.
61+
composeServiceVersionTask := The version to tag locally built images with in the docker-compose file. This defaults to the 'version' SettingKey.
6162
composeNoBuild := // True if a Docker Compose file is to be started without building any images and only using ones that already exist in the Docker Registry. This defaults to False.
6263
composeRemoveContainersOnShutdown := // True if a Docker Compose should remove containers when shutting down the compose instance. This defaults to True.
6364
composeRemoveNetworkOnShutdown := // True if a Docker Compose should remove the network it created when shutting down the compose instance. This defaults to True.

src/main/scala/com/tapad/docker/ComposeFile.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ trait ComposeFile extends SettingsHelper with ComposeCustomTagHelpers with Print
7171
//compose file
7272
val (updatedImageName, imageSource) = if (!useExistingImages && serviceName == localService) {
7373
//If the image does not contain a tag or has the tag "latest" it will not be replaced
74-
(replaceDefinedVersionTag(imageName, getSetting(version)), buildImageSource)
74+
(replaceDefinedVersionTag(imageName, getComposeServiceVersion(state)), buildImageSource)
7575
} else if (imageName.toLowerCase.contains(useLocalBuildTag)) {
7676
(processImageTag(state, args, imageName), buildImageSource)
7777
} else if (imageName.toLowerCase.contains(skipPullTag) || containsArg(DockerComposePlugin.skipPullArg, args)) {
@@ -138,6 +138,18 @@ trait ComposeFile extends SettingsHelper with ComposeCustomTagHelpers with Print
138138
}
139139
}
140140

141+
/**
142+
* Gets the version to use for local image tagging in docker compose
143+
* @param state The sbt state
144+
* @return The version to use for local image tagging in docker compose
145+
*/
146+
def getComposeServiceVersion(implicit state: State): String = {
147+
val extracted = Project.extract(state)
148+
val (_, version) = extracted.runTask(composeServiceVersionTask, state)
149+
150+
version
151+
}
152+
141153
def getUnsupportedFieldErrorMsg(fieldName: String): String = {
142154
s"Docker Compose field '$fieldName:' is currently not supported by sbt-docker-compose. Please see the README for " +
143155
s"more information on the set of unsupported fields."

src/main/scala/com/tapad/docker/DockerComposeKeys.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ object DockerComposeKeys extends DockerComposeKeysLocal
88
trait DockerComposeKeysLocal {
99
val composeFile = settingKey[String]("Specify the full path to the Compose File to use to create your test instance. It defaults to docker-compose.yml in your resources folder.")
1010
val composeServiceName = settingKey[String]("The name of the service in the Docker Compose file being tested. This setting prevents the service image from being pull down from the Docker Registry. This defaults to the Project name.")
11+
val composeServiceVersionTask = taskKey[String]("The version to tag locally built images with in the docker-compose file. This defaults to the 'version' SettingKey.")
1112
val composeNoBuild = settingKey[Boolean]("True if a Docker Compose file is to be started without building any images and only using ones that already exist in the Docker Registry. This defaults to False.")
1213
val composeRemoveContainersOnShutdown = settingKey[Boolean]("True if a Docker Compose should remove containers when shutting down the compose instance. This defaults to True.")
1314
val composeRemoveNetworkOnShutdown = settingKey[Boolean]("True if a Docker Compose should remove the network it created when shutting down the compose instance. This defaults to True.")

src/main/scala/com/tapad/docker/DockerComposePlugin.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ object DockerComposePlugin extends DockerComposePluginLocal {
6262
object autoImport {
6363
val composeFile = DockerComposeKeys.composeFile
6464
val composeServiceName = DockerComposeKeys.composeServiceName
65+
val composeServiceVersionTask = DockerComposeKeys.composeServiceVersionTask
6566
val composeNoBuild = DockerComposeKeys.composeNoBuild
6667
val composeRemoveContainersOnShutdown = DockerComposeKeys.composeRemoveContainersOnShutdown
6768
val composeRemoveNetworkOnShutdown = DockerComposeKeys.composeRemoveNetworkOnShutdown

src/main/scala/com/tapad/docker/DockerComposeSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ trait DockerComposeSettingsLocal extends PrintFormatting {
2626
},
2727
// By default set the Compose service name to be that of the sbt Project Name
2828
composeServiceName := name.value.toLowerCase,
29+
composeServiceVersionTask := version.value,
2930
composeNoBuild := false,
3031
composeRemoveContainersOnShutdown := true,
3132
composeRemoveNetworkOnShutdown := true,

src/test/scala/ComposeFileProcessingSpec.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ class ComposeFileProcessingSpec extends FunSuite with BeforeAndAfter with OneIns
381381
doReturn(versionNumber).when(composeMock).getSetting(version)(null)
382382
doReturn(noBuild).when(composeMock).getSetting(composeNoBuild)(null)
383383
doReturn(false).when(composeMock).getSetting(suppressColorFormatting)(null)
384+
doReturn(versionNumber).when(composeMock).getComposeServiceVersion(null)
384385

385386
(composeMock, composeFilePath)
386387
}

src/test/scala/PrintFormattingSpec.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class PrintFormattingSpec extends FunSuite with BeforeAndAfter with OneInstanceP
118118
doReturn(versionNumber).when(composeMock).getSetting(version)(null)
119119
doReturn(noBuild).when(composeMock).getSetting(composeNoBuild)(null)
120120
doReturn(containerHost).when(composeMock).getContainerHost(any[String], any[String], any[JValue])
121+
doReturn("1.0.0").when(composeMock).getComposeServiceVersion(null)
121122

122123
serviceNames.zip(containerIds).foreach {
123124
case (serviceName, containerId) =>

0 commit comments

Comments
 (0)