diff --git a/Jenkinsfile b/Jenkinsfile index e3788abd6f..ad61a10c74 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -182,20 +182,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 './gradlew docker' + sh "${kubernetes_image_build_script} '${image}'" } - 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() @@ -208,7 +208,6 @@ try { ${image} \ | grep ${version}" } - try { stage(stage_name + 'Test image') { sh "mkdir -p ${reports_folder}" diff --git a/build.gradle b/build.gradle index 664c38514d..161259f5f6 100644 --- a/build.gradle +++ b/build.gradle @@ -13,8 +13,6 @@ 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' @@ -24,7 +22,6 @@ 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' @@ -477,35 +474,6 @@ 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) @@ -531,14 +499,6 @@ 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 3ea01bdaea..91b4176f64 100644 --- a/docker/.gitignore +++ b/docker/.gitignore @@ -1,2 +1 @@ pantheon-*.tar.gz -pantheon/* \ No newline at end of file diff --git a/kubernetes/Dockerfile b/kubernetes/Dockerfile index d6d7372342..18a40ea434 100644 --- a/kubernetes/Dockerfile +++ b/kubernetes/Dockerfile @@ -1,19 +1,27 @@ +# 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 pantheon /opt/pantheon/ +# Copy extracted binaries from the previous step image +COPY --from=extract-stage /tmp/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 new file mode 100755 index 0000000000..825a87e68f --- /dev/null +++ b/kubernetes/build_image.sh @@ -0,0 +1,39 @@ +#!/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}