|
| 1 | +import groovy.time.* |
| 2 | + |
| 3 | +/** |
| 4 | + * Utility to build docker images based in gradle projects |
| 5 | + * |
| 6 | + * This extends gradle's 'application' plugin logic with a 'distDocker' task which builds |
| 7 | + * a docker image from the Dockerfile of the project that applies this file. The image |
| 8 | + * is automatically tagged and pushed if a tag and/or a registry is given. |
| 9 | + * |
| 10 | + * Parameters that can be set on project level: |
| 11 | + * - dockerImageName (required): The name of the image to build (e.g. controller) |
| 12 | + * - dockerRegistry (optional): The registry to push to |
| 13 | + * - dockerImageTag (optional, default 'latest'): The tag for the image |
| 14 | + * - dockerImagePrefix (optional, default 'whisk'): The prefix for the image, |
| 15 | + * 'controller' becomes 'whisk/controller' per default |
| 16 | + * - dockerTimeout (optional, default 840): Timeout for docker operations in seconds |
| 17 | + * - dockerRetries (optional, default 3): How many times to retry docker operations |
| 18 | + * - dockerBinary (optional, default 'docker'): The binary to execute docker commands |
| 19 | + * - dockerBuildArgs (options, default ''): Project specific custom docker build arguments |
| 20 | + * - dockerHost (optional): The docker host to run commands on, default behaviour is |
| 21 | + * docker's own DOCKER_HOST environment variable |
| 22 | + */ |
| 23 | + |
| 24 | +ext { |
| 25 | + dockerRegistry = project.hasProperty('dockerRegistry') ? dockerRegistry + '/' : '' |
| 26 | + dockerImageTag = project.hasProperty('dockerImageTag') ? dockerImageTag : 'latest' |
| 27 | + dockerImagePrefix = project.hasProperty('dockerImagePrefix') ? dockerImagePrefix : 'whisk' |
| 28 | + dockerTimeout = project.hasProperty('dockerTimeout') ? dockerTimeout.toInteger() : 840 |
| 29 | + dockerRetries = project.hasProperty('dockerRetries') ? dockerRetries.toInteger() : 3 |
| 30 | + dockerBinary = project.hasProperty('dockerBinary') ? [dockerBinary] : ['docker'] |
| 31 | + dockerBuildArg = ['build'] |
| 32 | +} |
| 33 | +ext.dockerTaggedImageName = dockerRegistry + dockerImagePrefix + '/' + dockerImageName + ':' + dockerImageTag |
| 34 | + |
| 35 | +if(project.hasProperty('dockerHost')) { |
| 36 | + dockerBinary += ['--host', project.dockerHost] |
| 37 | +} |
| 38 | + |
| 39 | +if(project.hasProperty('dockerBuildArgs')) { |
| 40 | + dockerBuildArgs.each { arg -> |
| 41 | + dockerBuildArg += ['--build-arg', arg] |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +task distDocker { |
| 46 | + doLast { |
| 47 | + def start = new Date() |
| 48 | + def cmd = dockerBinary + dockerBuildArg + ['-t', dockerImageName, project.buildscript.sourceFile.getParentFile().getAbsolutePath()] |
| 49 | + retry(cmd, dockerRetries, dockerTimeout) |
| 50 | + println("Building '${dockerImageName}' took ${TimeCategory.minus(new Date(), start)}") |
| 51 | + } |
| 52 | +} |
| 53 | +task tagImage { |
| 54 | + doLast { |
| 55 | + def versionString = (dockerBinary + ['-v']).execute().text |
| 56 | + def matched = (versionString =~ /(\d+)\.(\d+)\.(\d+)/) |
| 57 | + |
| 58 | + def major = matched[0][1] as int |
| 59 | + def minor = matched[0][2] as int |
| 60 | + |
| 61 | + def dockerCmd = ['tag'] |
| 62 | + if(major == 1 && minor < 12) { |
| 63 | + dockerCmd += ['-f'] |
| 64 | + } |
| 65 | + retry(dockerBinary + dockerCmd + [dockerImageName, dockerTaggedImageName], dockerRetries, dockerTimeout) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +task pushImage { |
| 70 | + doLast { |
| 71 | + def cmd = dockerBinary + ['push', dockerTaggedImageName] |
| 72 | + retry(cmd, dockerRetries, dockerTimeout) |
| 73 | + } |
| 74 | +} |
| 75 | +pushImage.dependsOn tagImage |
| 76 | +pushImage.onlyIf { dockerRegistry != '' } |
| 77 | +distDocker.finalizedBy pushImage |
| 78 | + |
| 79 | +def retry(cmd, retries, timeout) { |
| 80 | + println("${new Date()}: Executing '${cmd.join(" ")}'") |
| 81 | + def proc = cmd.execute() |
| 82 | + proc.consumeProcessOutput(System.out, System.err) |
| 83 | + proc.waitForOrKill(timeout * 1000) |
| 84 | + if(proc.exitValue() != 0) { |
| 85 | + def message = "${new Date()}: Command '${cmd.join(" ")}' failed with exitCode ${proc.exitValue()}" |
| 86 | + if(proc.exitValue() == 143) { // 143 means the process was killed (SIGTERM signal) |
| 87 | + message = "${new Date()}: Command '${cmd.join(" ")}' was killed after ${timeout} seconds" |
| 88 | + } |
| 89 | + |
| 90 | + if(retries > 1) { |
| 91 | + println("${message}, ${retries-1} retries left, retrying...") |
| 92 | + retry(cmd, retries-1, timeout) |
| 93 | + } |
| 94 | + else { |
| 95 | + println("${message}, no more retries left, aborting...") |
| 96 | + throw new GradleException(message) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments