diff --git a/README.md b/README.md index 1d5bef7..b84cee1 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,9 @@ docker { // name and value of a buildarg. Optional, default = no build arguments buildArgs = ['http_proxy="http://some.proxy.url"'] + // platforms to be built, Optional, default = no. E.g. platforms = ['linux/arm64', 'linux/amd64'] + platforms = ['linux/arm64', 'linux/amd64'] + // Always remove intermediate containers, even after unsuccessful builds. Optional, default = false removeIntermediateContainers = true diff --git a/build.gradle b/build.gradle index 652b1b0..2e1cc90 100644 --- a/build.gradle +++ b/build.gradle @@ -26,7 +26,7 @@ java { } group = 'org.sglahn' -version = "0.8" +version = "0.8.1" pluginBundle { website = 'https://github.com/sglahn/gradle-dockerfile-plugin' diff --git a/example-project-groovy-dsl/build.gradle b/example-project-groovy-dsl/build.gradle index a4c0cd9..7900cc4 100644 --- a/example-project-groovy-dsl/build.gradle +++ b/example-project-groovy-dsl/build.gradle @@ -37,6 +37,8 @@ docker { labels = ['branch=master', 'mylabel=test'] // name and value of a buildarg. Optional, default = no build arguments buildArgs = ['http_proxy="http://some.proxy.url"'] + // platforms to be built, Optional, default = no. E.g. platforms = ['linux/arm64', 'linux/amd64'] + platforms = ['linux/amd64'] // Always remove intermediate containers, even after unsuccessful builds. Optional, default = false removeIntermediateContainers = true // Isolation specifies the type of isolation technology used by containers. Optional, default = default diff --git a/example-project-kotlin-dsl/build.gradle.kts b/example-project-kotlin-dsl/build.gradle.kts index ef6e34d..33bd61e 100644 --- a/example-project-kotlin-dsl/build.gradle.kts +++ b/example-project-kotlin-dsl/build.gradle.kts @@ -39,6 +39,8 @@ docker { labels = listOf("branch=master", "mylabel=test") // name and value of a buildarg. Optional, default = no build arguments buildArgs = listOf("http_proxy=\"http://some.proxy.url\"") + // platforms to be built, Optional, default = no. E.g. platforms = ['linux/arm64', 'linux/amd64'] + platforms = listOf('linux/amd64') // Always remove intermediate containers, even after unsuccessful builds. Optional, default = false removeIntermediateContainers = true // Isolation specifies the type of isolation technology used by containers. Optional, default = default diff --git a/src/main/groovy/org/sglahn/gradle/docker/DockerFilePluginExtension.groovy b/src/main/groovy/org/sglahn/gradle/docker/DockerFilePluginExtension.groovy index 2ba1c30..ed8523a 100644 --- a/src/main/groovy/org/sglahn/gradle/docker/DockerFilePluginExtension.groovy +++ b/src/main/groovy/org/sglahn/gradle/docker/DockerFilePluginExtension.groovy @@ -25,6 +25,7 @@ class DockerFilePluginExtension { List tags = [] List labels = [] List buildArgs = [] + List platforms = [] String dockerFile String buildContext diff --git a/src/main/groovy/org/sglahn/gradle/docker/util/DockerHelper.groovy b/src/main/groovy/org/sglahn/gradle/docker/util/DockerHelper.groovy index 1c9d133..b9f4b4c 100644 --- a/src/main/groovy/org/sglahn/gradle/docker/util/DockerHelper.groovy +++ b/src/main/groovy/org/sglahn/gradle/docker/util/DockerHelper.groovy @@ -70,7 +70,12 @@ class DockerHelper { def dockerRepository = project.docker.dockerRepository ?: '' if (!dockerRepository.empty && !dockerRepository.endsWith('/')) dockerRepository = dockerRepository + '/' def imageName = project.docker.imageName ?: project.getName() - def cmd = "docker push ${dockerRepository}${imageName}:${tag}" + def cmd = "" + if (useBuildX(project)) { + cmd = "docker " + dockerBuildParameter(project, true).join(" ") + } else { + cmd = "docker push ${dockerRepository}${imageName}:${tag}" + } executeCmd(project, cmd) } @@ -84,7 +89,7 @@ class DockerHelper { executeCmd(project, cmd) } - static List dockerBuildParameter(project) { + static List dockerBuildParameter(project, push=false) { List arguments = new ArrayList<>() def dockerRepository = project.docker.dockerRepository ?: '' @@ -92,8 +97,16 @@ class DockerHelper { def imageName = project.docker.imageName ?: project.getName() def imageVersion = project.docker.imageVersion ?: "latest" + if (useBuildX(project)) { + arguments.add('buildx') + } arguments.add('build') + println("push: $push") + if (push) { + arguments.add("--push") + } + if (project.docker.tags.isEmpty()) { arguments.add('-t') arguments.add("${dockerRepository}${imageName}:${imageVersion}") @@ -112,6 +125,10 @@ class DockerHelper { arguments.add("--build-arg=${it}") } + if (useBuildX(project)) { + arguments.add("--platform=${project.docker.platforms.join(",")}") + } + if (project.docker.isolation == null) { arguments.add("--isolation=default") } @@ -130,4 +147,8 @@ class DockerHelper { arguments } + + static boolean useBuildX(project) { + return !project.docker.platforms.isEmpty() + } }