From 491012159a9b351b7b9b89ed66594c3033dcd80d Mon Sep 17 00:00:00 2001 From: Joshua Fernandes Date: Fri, 21 Jun 2019 19:14:10 +1000 Subject: [PATCH] cleaning up the build process for docker (#1590) Signed-off-by: Adrian Sutton --- Jenkinsfile | 7 ++++--- build.gradle | 40 +++++++++++++++++++++++++++++++++++++++ docker/.gitignore | 1 + docker/Dockerfile | 6 +----- kubernetes/Dockerfile | 18 +++++------------- kubernetes/build_image.sh | 39 -------------------------------------- 6 files changed, 51 insertions(+), 60 deletions(-) delete mode 100755 kubernetes/build_image.sh diff --git a/Jenkinsfile b/Jenkinsfile index ba1ae27936..d325d2e4b7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -185,20 +185,20 @@ try { def stage_name = 'Kubernetes Docker image node: ' def image = imageRepos + '/pantheon-kubernetes:' + imageTag def kubernetes_folder = 'kubernetes' - def kubernetes_image_build_script = kubernetes_folder + '/build_image.sh' def version_property_file = 'gradle.properties' def reports_folder = kubernetes_folder + '/reports' def dockerfile = kubernetes_folder + '/Dockerfile' node { checkout scm - unstash 'distTarBall' docker.image(build_image).inside() { stage(stage_name + 'Dockerfile lint') { sh "docker run --rm -i hadolint/hadolint < ${dockerfile}" } + stage(stage_name + 'Build image') { - sh "${kubernetes_image_build_script} '${image}'" + sh './gradlew docker' } + stage(stage_name + "Test image labels") { shortCommit = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim() version = sh(returnStdout: true, script: "grep -oE \"version=(.*)\" ${version_property_file} | cut -d= -f2").trim() @@ -211,6 +211,7 @@ try { ${image} \ | grep ${version}" } + try { stage(stage_name + 'Test image') { sh "mkdir -p ${reports_folder}" diff --git a/build.gradle b/build.gradle index b25468e953..413ff7c2ce 100644 --- a/build.gradle +++ b/build.gradle @@ -13,6 +13,8 @@ import net.ltgt.gradle.errorprone.CheckSeverity +import java.text.SimpleDateFormat + plugins { id 'com.diffplug.gradle.spotless' version '3.23.1' id 'com.jfrog.bintray' version '1.8.4' @@ -22,6 +24,7 @@ plugins { id 'me.champeau.gradle.jmh' version '0.4.8' apply false id 'net.ltgt.errorprone' version '0.8.1' id 'net.researchgate.release' version '2.7.0' + id "com.palantir.docker" version "0.22.1" } group = 'tech.pegasys.pantheon' @@ -397,6 +400,35 @@ distZip { } } +// rename the top level dir from pantheon- to pantheon and this makes it really +// simple for use in docker +tasks.register("dockerDistUntar") { + dependsOn distTar + def dockerBuildDir = "build/distributions/docker-pantheon/" + def distTarFile = distTar.outputs.files.singleFile + def distTarFileName = distTar.outputs.files.singleFile.name.replace(".tar.gz", "") + + doFirst { + new File(dockerBuildDir).mkdir() + copy { + from tarTree(distTarFile) + into (dockerBuildDir) + } + file("${dockerBuildDir}/${distTarFileName}").renameTo("${dockerBuildDir}/pantheon") + } +} + +docker { + def image_tag = "develop" + dependsOn dockerDistUntar + name "pegasyseng/pantheon-kubernetes:${image_tag}" + dockerfile file('kubernetes/Dockerfile') + files "build/distributions/docker-pantheon/" + buildArgs(['BUILD_DATE': buildTime(), 'VERSION': rootProject.version, 'VCS_REF': getCheckedOutGitCommitHash()]) + pull true + noCache true +} + task jacocoRootReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) { additionalSourceDirs.from files(subprojects.sourceSets.main.allSource.srcDirs) sourceDirectories.from files(subprojects.sourceSets.main.allSource.srcDirs) @@ -422,6 +454,14 @@ configure(subprojects.findAll {it.name != 'errorprone-checks'}) { } } +// http://label-schema.org/rc1/ +// using the RFC3339 format "2016-04-12T23:20:50.52Z" +def buildTime() { + def df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'") + df.setTimeZone(TimeZone.getTimeZone("UTC")) + return df.format(new Date()) +} + // Takes the version, and if -SNAPSHOT is part of it replaces SNAPSHOT // with the git commit version. def calculateVersion() { diff --git a/docker/.gitignore b/docker/.gitignore index 91b4176f64..3ea01bdaea 100644 --- a/docker/.gitignore +++ b/docker/.gitignore @@ -1 +1,2 @@ pantheon-*.tar.gz +pantheon/* \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile index cee14d28a9..d09ce96122 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,10 +1,6 @@ FROM openjdk:11.0.2-jre-slim-stretch -COPY pantheon-*.tar.gz /tmp/. -RUN tar xzf /tmp/pantheon-*.tar.gz -C /tmp && \ - rm /tmp/pantheon-*.tar.gz && \ - mv /tmp/pantheon-* /opt/pantheon - +COPY pantheon /opt/pantheon/ RUN mkdir /var/lib/pantheon RUN mkdir /etc/pantheon/ diff --git a/kubernetes/Dockerfile b/kubernetes/Dockerfile index 18a40ea434..d6d7372342 100644 --- a/kubernetes/Dockerfile +++ b/kubernetes/Dockerfile @@ -1,27 +1,19 @@ -# extract image stage -# extractin here reduces the number of layers in the final image -FROM alpine:3.9 AS extract-stage -# Copy Pantheon binaries from previous jenkins artefact step -# or from the result of ./gradlew distTar -# and lett ADD unpack them -ADD pantheon-*.tar.gz /tmp/ -# Run image stage -# Use openJDK JRE only for running pantheon FROM openjdk:11.0.2-jre-slim-stretch -# Copy extracted binaries from the previous step image -COPY --from=extract-stage /tmp/pantheon* /opt/pantheon + +COPY pantheon /opt/pantheon/ WORKDIR /opt/pantheon + # Expose services ports # 8545 HTTP JSON-RPC # 8546 WS JSON-RPC # 8547 HTTP GraphQL # 30303 P2P EXPOSE 8545 8546 8547 30303 + ENTRYPOINT ["/opt/pantheon/bin/pantheon"] + # Build-time metadata as defined at http://label-schema.org -# Use the build_image.sh script in the kubernetes directory of this project to -# easily build this image or as an example of how to inject build parameters. ARG BUILD_DATE ARG VCS_REF ARG VERSION diff --git a/kubernetes/build_image.sh b/kubernetes/build_image.sh deleted file mode 100755 index 825a87e68f..0000000000 --- a/kubernetes/build_image.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/sh -e -# This script presents a sample way to build Pantheon Docker image -# with automatic build arguments from the current build workspace. -# It must be started from the same path as where the Dockerfile is located. -# you have to pass the imnage tag as an argument like for instance : -# build_image.sh "pegasyseng/pantheon-kubernetes:develop" - -CONTEXT_FOLDER=kubernetes/ -PANTHEON_BUILD_SOURCE='build/distributions/pantheon-*.tar.gz' - -# Checking that you passed the tag for the image to be build -if [ -z "$1" ] - then - me=`basename "$0"` - echo "No image tag argument supplied to ${me}" - echo "ex.: ${me} \"pegasyseng/pantheon-kubernetes:develop\"" - exit 1 -fi - -# looking for the distribution archive, either form CI step that builds form this -# workspace sources but with multiple test steps first -# or it builds it if you don't have one as you are probably -# not in a CI step. -if ls ${PANTHEON_BUILD_SOURCE} 1> /dev/null 2>&1; then - cp ${PANTHEON_BUILD_SOURCE} ${CONTEXT_FOLDER} -else - echo "No pantheon-*.tar.gz archive found." - echo "You are probably not running this from CI so running './gradlew distTar' first to have a local build" - ./gradlew distTar - cp ${PANTHEON_BUILD_SOURCE} ${CONTEXT_FOLDER} -fi - -# Builds docker image with tags matching the info form this current workspace -docker build \ --t "$1" \ ---build-arg BUILD_DATE="`date`" \ ---build-arg VCS_REF="`git show -s --format=%h`" \ ---build-arg VERSION="`grep -oE "version=(.*)" gradle.properties | cut -d= -f2`" \ -${CONTEXT_FOLDER}