| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| import com.google.common.base.Preconditions |
| import com.google.common.base.Strings |
| |
| description = 'Solr Docker image' |
| |
| apply plugin: 'base' |
| |
| // Solr Docker inputs |
| def dockerImageRepo = propertyOrEnvOrDefault("solr.docker.imageRepo", "SOLR_DOCKER_IMAGE_REPO", "apache/solr") |
| def dockerImageTag = propertyOrEnvOrDefault("solr.docker.imageTag", "SOLR_DOCKER_IMAGE_TAG", "${version}") |
| def dockerImageName = propertyOrEnvOrDefault("solr.docker.imageName", "SOLR_DOCKER_IMAGE_NAME", "${dockerImageRepo}:${dockerImageTag}") |
| def baseDockerImage = propertyOrEnvOrDefault("solr.docker.baseImage", "SOLR_DOCKER_BASE_IMAGE", 'openjdk:11-jre-slim') |
| def githubUrlOrMirror = propertyOrEnvOrDefault("solr.docker.githubUrl", "SOLR_DOCKER_GITHUB_URL", 'github.com') |
| |
| // Build directory locations |
| def imageIdFile = "$buildDir/image-id" |
| |
| configurations { |
| packaging { |
| canBeResolved = true |
| } |
| solrArtifacts { |
| canBeResolved = true |
| } |
| dockerContext { |
| canBeResolved = true |
| } |
| dockerImage { |
| canBeResolved = true |
| } |
| } |
| |
| ext { |
| packagingDir = file("${buildDir}/packaging") |
| dockerContextFile = "context.tgz" |
| dockerContextPath = "${buildDir}/${dockerContextFile}" |
| } |
| |
| dependencies { |
| packaging files(packagingDir) { |
| builtBy 'assemblePackaging' |
| } |
| |
| solrArtifacts project(path: ":solr:packaging", configuration: 'archives') |
| |
| dockerContext files(dockerContextPath) { |
| builtBy 'dockerContext' |
| } |
| |
| dockerImage files(imageIdFile) { |
| builtBy 'dockerBuild' |
| } |
| } |
| |
| task assemblePackaging(type: Sync) { |
| description = 'Assemble docker scripts and Dockerfile for Solr Packaging' |
| |
| from(projectDir, { |
| include "Dockerfile" |
| include "scripts/**" |
| }) |
| |
| into packagingDir |
| } |
| |
| task dockerContext(type: Sync) { |
| description = 'Sync Solr tgz to become docker context' |
| |
| from(configurations.solrArtifacts, { |
| include "*.tgz" |
| rename { file -> dockerContextFile } |
| }) |
| |
| into buildDir |
| } |
| |
| task dockerBuild(dependsOn: configurations.dockerContext) { |
| group = 'Docker' |
| description = 'Build Solr docker image' |
| |
| // Ensure that the docker image is rebuilt on build-arg changes or changes in the docker context |
| inputs.properties([ |
| baseDockerImage: baseDockerImage, |
| githubUrlOrMirror: githubUrlOrMirror |
| ]) |
| inputs.files(dockerContextPath) |
| |
| doLast { |
| exec { |
| standardInput = configurations.dockerContext.singleFile.newDataInputStream() |
| commandLine "docker", "build", |
| "-f", "solr-${version}/docker/Dockerfile", |
| "--iidfile", imageIdFile, |
| "--build-arg", "BASE_IMAGE=${inputs.properties.baseDockerImage}", |
| "--build-arg", "GITHUB_URL=${inputs.properties.githubUrlOrMirror}", |
| "-" |
| } |
| } |
| |
| // Print information on the image after it has been created |
| doLast { |
| def dockerImageId = file(imageIdFile).text |
| project.logger.lifecycle("Solr Docker Image Created") |
| project.logger.lifecycle("\tID: \t$dockerImageId") |
| project.logger.lifecycle("\tBase Image: \t$baseDockerImage") |
| project.logger.lifecycle("\tSolr Version: \t$version") |
| } |
| |
| outputs.files(imageIdFile) |
| } |
| |
| task dockerTag(dependsOn: tasks.dockerBuild) { |
| group = 'Docker' |
| description = 'Tag Solr docker image' |
| |
| def dockerImageIdFile = file(imageIdFile) |
| // Ensure that the docker image is re-tagged if the image ID or desired tag changes |
| inputs.properties([ |
| dockerImageName: dockerImageName, |
| ]) |
| inputs.file(dockerImageIdFile) |
| |
| doLast { |
| def dockerImageId = dockerImageIdFile.text |
| |
| exec { |
| commandLine "docker", "tag", dockerImageId, inputs.properties.dockerImageName |
| } |
| |
| // Print information on the image after it has been created |
| project.logger.lifecycle("Solr Docker Image Tagged") |
| project.logger.lifecycle("\tID: \t$dockerImageId") |
| project.logger.lifecycle("\tTag: \t$dockerImageName") |
| } |
| } |
| |
| task testDocker(dependsOn: tasks.dockerBuild) { |
| group = 'Docker' |
| description = 'Test Solr docker image' |
| |
| def inputDir = "tests/cases" |
| def outputDir = "$buildDir/tmp/tests" |
| |
| // Ensure that the docker image is re-tested if the image ID changes or the test files change |
| inputs.properties([ |
| includeTests: new HashSet(Arrays.asList(propertyOrEnvOrDefault("solr.docker.tests.include", "SOLR_DOCKER_TESTS_INCLUDE", ",").split(","))), |
| excludeTests: new HashSet(Arrays.asList(propertyOrEnvOrDefault("solr.docker.tests.exclude", "SOLR_DOCKER_TESTS_EXCLUDE", ",").split(","))) |
| ]) |
| inputs.file(imageIdFile) |
| inputs.dir(inputDir) |
| |
| doLast { |
| def solrImageId = tasks.dockerBuild.outputs.files.singleFile.text |
| def solrImageName = solrImageId.substring(7, 14) |
| |
| // Print information on the image before it is tested |
| logger.lifecycle("Testing Solr Image:") |
| logger.lifecycle("\tID: $solrImageId\n") |
| |
| // Run the tests |
| def sourceDir = file(inputDir) |
| sourceDir.eachFile { file -> |
| def testName = file.getName() |
| def testCaseBuildDir = "${outputDir}/${testName}" |
| |
| // If specific tests are specified, only run those. Otherwise run all that are not ignored. |
| def runTest = !inputs.properties.includeTests.isEmpty() ? inputs.properties.includeTests.contains(testName) : !inputs.properties.excludeTests.contains(testName) |
| if (runTest) { |
| exec { |
| environment "TEST_DIR", file |
| environment "BUILD_DIR", testCaseBuildDir |
| commandLine "bash", "$file/test.sh", solrImageName |
| } |
| } |
| } |
| } |
| |
| outputs.dir(outputDir) |
| } |
| |
| task dockerPush(dependsOn: tasks.dockerTag) { |
| group = 'Docker' |
| description = 'Push Solr docker image' |
| |
| // Ensure that the docker image is re-pushed if the image ID or tag changes |
| inputs.properties([ |
| dockerImageName: dockerImageName, |
| ]) |
| inputs.file(imageIdFile) |
| |
| // We don't want to push a docker image unless the tests have passed |
| mustRunAfter tasks.testDocker |
| |
| doLast { |
| exec { |
| commandLine "docker", "push", dockerImageName |
| } |
| |
| // Print information on the image after it has been created |
| project.logger.lifecycle("Solr Docker Image Pushed: \t$dockerImageName") |
| } |
| } |
| |
| // One task to build and tag a Solr docker image |
| task docker { |
| dependsOn tasks.dockerBuild, tasks.dockerTag |
| } |