From cb22ccc9c0d7204f10acc00d1ee3ec2e3fec11b6 Mon Sep 17 00:00:00 2001 From: Ulysse Rubens Date: Thu, 23 Jun 2022 15:25:38 +0200 Subject: [PATCH 1/9] refactor: rewrite Dockerfile using Docker guidelines & multi-stage build --- .dockerignore | 5 ++++ build.gradle | 9 ++++--- docker/Dockerfile | 40 ----------------------------- docker/check-status.sh | 19 -------------- docker/deploy.sh | 29 --------------------- scripts/docker/Dockerfile | 54 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 64 insertions(+), 92 deletions(-) create mode 100644 .dockerignore delete mode 100644 docker/Dockerfile delete mode 100755 docker/check-status.sh delete mode 100644 docker/deploy.sh create mode 100644 scripts/docker/Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..df0f3d7 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +* +!src +!build.gradle +!settings.gradle +!scripts/docker diff --git a/build.gradle b/build.gradle index 99ee168..3508033 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group 'be.cytomine.bioformats' -version '3.1.0' +version '0.0.0' sourceCompatibility = 1.8 @@ -29,9 +29,9 @@ dependencies { exclude group: 'org.slf4j', module: 'slf4j-log4j12' exclude group: 'ch.qos.logback', module: 'logback-classic' } - compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.17.1' - compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.17.1' - compile('org.apache.logging.log4j:log4j-slf4j-impl:2.17.1') + compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.17.2' + compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.17.2' + compile('org.apache.logging.log4j:log4j-slf4j-impl:2.17.2') testCompile group: 'junit', name: 'junit', version: '4.12' } @@ -58,6 +58,7 @@ run { } jar { + archiveName = 'cytomine-bioformats-wrapper.jar' manifest { attributes('Main-Class': 'be.cytomine.bioformats.BioFormatServerSocket') } diff --git a/docker/Dockerfile b/docker/Dockerfile deleted file mode 100644 index f8655b0..0000000 --- a/docker/Dockerfile +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright (c) 2009-2019. Authors: see NOTICE file. -# -# Licensed 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. - -ARG FROM_NAMESPACE -ARG FROM_VERSION -FROM ${FROM_NAMESPACE}/java8:$FROM_VERSION - -ENV BIOFORMAT_PORT 4321 -ENV BIOFORMAT_POOL_SIZE 4 - -RUN apt-get -y update && \ - apt-get install -y \ - cron \ - rsyslog && \ - rm -rf /var/lib/apt/lists/* - -ARG VERSION -ARG NAMESPACE=Cytomine-ULiege -ARG RELEASE_PATH=https://github.com/${NAMESPACE}/Cytomine-BioFormats/release/download/v${VERSION} -ADD ${RELEASE_PATH}/cytomine-bioformats-wrapper.jar /tmp/cytomine-bioformats-wrapper.jar -RUN chmod +x /tmp/cytomine-bioformats-wrapper.jar - -COPY check-status.sh /tmp/check-status.sh -RUN chmod +x /tmp/check-status.sh - -COPY deploy.sh /tmp/deploy.sh -RUN chmod +x /tmp/deploy.sh - -ENTRYPOINT ["/tmp/deploy.sh"] diff --git a/docker/check-status.sh b/docker/check-status.sh deleted file mode 100755 index 18dae49..0000000 --- a/docker/check-status.sh +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright (c) 2009-2019. Authors: see NOTICE file. -# -# Licensed 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. - -COUNT=$(ps aux | grep "java -jar /tmp/cytomine-bioformats-wrapper.jar" | grep -v grep | wc -l) -if [ $COUNT -lt 1 ]; then - echo "$COUNT / 1 on $(date)" - java -jar /tmp/cytomine-bioformats-wrapper.jar $1 $2 > /tmp/log & -fi; \ No newline at end of file diff --git a/docker/deploy.sh b/docker/deploy.sh deleted file mode 100644 index 94edcd5..0000000 --- a/docker/deploy.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash - -# Copyright (c) 2009-2019. Authors: see NOTICE file. -# -# Licensed 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. - -java -jar /tmp/cytomine-bioformats-wrapper.jar $BIOFORMAT_PORT $BIOFORMAT_POOL_SIZE > /tmp/log & - -touch /tmp/crontab -echo "BIOFORMAT_PORT=$BIOFORMAT_PORT" >> /tmp/crontab -echo "BIOFORMAT_POOL_SIZE=$BIOFORMAT_POOL_SIZE" >> /tmp/crontab -echo "*/1 * * * * /bin/bash /tmp/check-status.sh $BIOFORMAT_PORT $BIOFORMAT_POOL_SIZE >> /tmp/cron.out" >> /tmp/crontab -crontab /tmp/crontab -rm /tmp/crontab - -service rsyslog restart -service cron restart - -tail -F /tmp/log diff --git a/scripts/docker/Dockerfile b/scripts/docker/Dockerfile new file mode 100644 index 0000000..af9268b --- /dev/null +++ b/scripts/docker/Dockerfile @@ -0,0 +1,54 @@ +# Copyright (c) 2009-2019. Authors: see NOTICE file. +# +# Licensed 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. + +ARG VERSION_NUMBER +ARG GRADLE_TAG=4.10.3-jdk8 + +FROM gradle:${GRADLE_TAG} AS dependencies +USER root + +# We first copy the pom.xml file and the binaries stored in the source repository. +# This way, we retrieve all maven dependencies at the beginning. +# All these steps will be cached by Docker unless pom.xml or libs/ has been updated. +# This means that we only retrieve all dependencies if we modify the dependencies definition. +RUN mkdir -p /opt/gradle/.gradle +ENV GRADLE_USER_HOME=/opt/gradle/.gradle + +WORKDIR /app +COPY ./build.gradle . +RUN gradle clean build --no-daemon --console=verbose || return 0 + + +FROM gradle:${GRADLE_TAG} AS builder +USER root + +COPY --from=dependencies /opt/gradle/.gradle /opt/gradle/.gradle + +WORKDIR /app +COPY . . + +RUN sed -i -- 's/version '0.0.0'/version '$VERSION_NUMBER'/g' build.gradle \ + && gradle jar --console=verbose + + +FROM openjdk:8 AS production + +WORKDIR /app +COPY --from=builder /app/build/libs/cytomine-bioformats-wrapper.jar . + +ENV BIOFORMAT_POOL_SIZE 4 +EXPOSE 4321 + +# We use CMD shell form to allow variable substitution +CMD java -jar /app/cytomine-bioformats-wrapper.jar 4321 ${BIOFORMAT_POOL_SIZE} From 535cdb1c91ec058be8517c2cab1e2f08e0786145 Mon Sep 17 00:00:00 2001 From: Ulysse Rubens Date: Thu, 23 Jun 2022 15:27:13 +0200 Subject: [PATCH 2/9] refactor: rewrite ci scripts for Jenkins and remove Travis --- .travis.yml | 59 ------------------ Jenkinsfile | 54 ++++++++++++++++ scripts/build-version.sh | 38 ++++++++++++ scripts/ci.sh | 130 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 222 insertions(+), 59 deletions(-) delete mode 100644 .travis.yml create mode 100644 Jenkinsfile create mode 100644 scripts/build-version.sh create mode 100644 scripts/ci.sh diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index c695760..0000000 --- a/.travis.yml +++ /dev/null @@ -1,59 +0,0 @@ -sudo: required - -dist: trusty - -services: - - docker - -language: java - -jdk: - - oraclejdk8 - -env: - global: - - DOCKER_USERNAME=cytomineulg - - secure: "Jwdb9/zNClvneLnqN4tpw4Gxvl+9pn2I36hek7VC1ggxXYHvYux1tMocSl4F9//JpOkjayA4y343nuXdKhi4W6BSQvIDeK/sU6I1z8DsjRaX5ZZ30vu85qlJ/EeBqK/xn2AyKP7Z0vIIDhgTlik1URur2DMNTA7a6rhvJeSQIjgOZ1GU5O1xAFMd4LXgzQKa+xbXCk1qRB74oo7eTDh8N4w+xvp/mRZDAfgqVUhgQopsk/ecOr+iOKPZUvJZm5LsJVCKqiaGX1jdzpfZNUrXdkwc2LpWaVx9sY/5p/442Hk4JNyf7GHB3agHjl3Pg2GJuT5Nj9EP4pRa18ay9hUNxjLBf851i70NYqAMOuSk49bWSx17VjJp1KZBXTCxDFHv46hh3iQsKLq68PYumlt4G0J8uKty4pV+V8W6aM420E6GO3eae0yhMAkO7FIRZevk0wmOX5UcUQZe1Va/+mDOndwJcgVHobcVI0Ittw+XgOfl/ZqCgGqor6Q+peZL4o8jPqQyMoEEx7TloHPW7AqRR65ywejBeKRJpdvJushE8nz+BmrYfVMW/oZEtuydJLKAk3iHqqgFDWYU2xCC7aKGI/aa4BJhWVYlNSKf59ys74becyyMXYxd+cKS2n857MjYLDeJcMKbaOQ4iDCrCXqhkuTZljgdbY3tfPa1cxRrKHw=" - -script: true - -after_success: - - git fetch --tags - - export VERSION=$(gradle properties -q | grep "version:" | awk '{print $2}' | tr -d '[:space:]') - - export DEPLOY=false - - > - if [[ ! $(git tag -l v$VERSION) ]]; then - git config --local user.name "$(git log -1 --pretty=format:'%an')"; - git config --local user.email "$(git log -1 --pretty=format:'%ae')"; - git tag "v$VERSION"; - export DEPLOY=true; - echo "Deploy with version $VERSION"; - ./gradlew jar; - mv build/libs/cytomine-bioformats-wrapper-$VERSION.jar cytomine-bioformats-wrapper.jar; - cp cytomine-bioformats-wrapper.jar docker/; - docker build --build-arg RELEASE_PATH="." --build-arg FROM_NAMESPACE=cytomineuliege --build-arg FROM_VERSION=v1.2.0 -t cytomineuliege/bioformat:latest -t cytomineuliege/bioformat:v$VERSION docker/ - fi; - -deploy: - - provider: script - script: > - echo $VERSION && - docker login -u "$DOCKER_USERNAME" -p "$DOCKER_PASSWORD" && - docker push cytomineuliege/bioformat:v$VERSION && - echo "Done." - skip_cleanup: true - on: - condition: $DEPLOY = true - - provider: releases - api_key: - secure: TPQa8EpiTwijPQUjMvJuo1QrkpDrBOxwmkGsJ4A7L+8JhAs2c9OVs3iyPwlxwrUHgglC3kOGq0HVtY7vZrBY/FTCeKuKFjCEjsDHeioulYDFK4oV55vQQeLks65Xr1gbXE42TnzluGjsREAMOjywf4JhyhqmKC424YGtY2ZOuaAbHSNOtzl6I9rwnezXSSY3Kzdudo9zS0pq+L4QUHxRYh50hHdv/vyhaEpMCZiIowtXign348PdkRRjmGwQKlPB6L0oY+lbxA35vTb+WALOmrNLbyyG55WnjXSWnL3cn8KDmllLcNmhzXDldsDGPph2fTP65XieVRuUqvTb38837JhlDe9nvz5Msd9JNlMP04vxKcGhZnVoPUVPfRmZCItzeOrvwOUt1V99BJ4uJeQVxR0W2ZcJ9KvC4UbBG1+FNBMpB+jqZg76aQqTLJjJHRXbTGj/wskGR0Gw/yvjZCyydCeMWVNPp8i+NHmh+RQu69DkYfRzoVi5EynutyIzY3OkZZ4N8lezN3nUNxRghOBZXc6wSJdhJpMmIigvjKqxBZONZ3u3qpH/JashQkTNZLfS2vsQ0Uzz4of24QysQ7rm9yTS3ZyCB5GU11P82Hm2SzScVf+OiQFGjLpb19jk+t9SPMyY23cpvT5s38HIY5wWGceb7NcQtwI1ycOVd7HnoZ0= - file: - - cytomine-bioformats-wrapper.jar - skip_cleanup: true - on: - condition: $DEPLOY = true - -notifications: - email: false - slack: - secure: TrdyGx56LOXEzrMxZgMhnYwpLQjIX3fL40YYujq40W66Rcis6nm2SP4i0By3fOSwwKcRwP8HXvs219sZddL3KJSZ0ormMvLHVjo9NNEbUAHqWVUBnaNJjF6ANmRFpuIKkVRTu2LQHaJhjMFyrKg8VVRAxNR1keaOsVeb4/U6gLF9LqfMciz56TtoDzfbMfB0V7l+RtUfRpeKGjDqp+lAEIzueSRItK49hP4gACGxJEMACIG+z1PZNLtVTnV6DokqnVmhUBIo9+ikLmU8g48Ark6hmj0JC5zZHK+LwZUhlMeJmPsJMydJ0ZPiOzcdPhQoLZC5looGFYPru2Igu3GTIM/hoTCDIYUCYRStr0vRF4niEEWMXKp9NJUQHkK0RwPCsuc0iwl1Jaeh9+qSEUeEW3tYyRgNwBPI4YK/XFt1/oDHufi8xU50xVTysSOxVJWbZ29OVdJ4LtWrF5rdKiFfl54sAqEnVREp8QcvRFXLMwL89cW5omi4xqmlojLm1pS/6DthlrED7dMz8qQi43shOPQbVqA+TRRUXl7jPo4DNTbI/8KfJGXg76scytZB4qUx4t6j2C9YxllHEZf7nlNyvHQzTLrcNOB6kxYFOibdrWIm1TycpbIBkb3l/1ZP0MgVFLpEu89eYcCpk29g6QkdyMU6Pb69avq4c9HUk0WhGME= diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..a4dd550 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,54 @@ +node { + stage ('Retrieve sources') { + checkout([ + $class: 'GitSCM', + branches: [[name: 'refs/heads/'+env.BRANCH_NAME]], + extensions: [[$class: 'CloneOption', noTags: false, shallow: false, depth: 0, reference: '']], + userRemoteConfigs: scm.userRemoteConfigs, + ]) + } + + stage ('Clean') { + sh 'rm -rf output' + sh 'mkdir -p output' + } + + stage ('Compute version number') { + env.VERSION_NUMBER = sh(script: 'scripts/compute-version.sh ${BRANCH_NAME}', returnStdout: true) + + stage ('Download and cache dependencies') { + sh 'scripts/ci.sh dependencies ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}' + } + + stage ('Build jar') { + sh 'scripts/ci.sh build-jar ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}' + } + + stage ('Publish jar') { + sh 'scripts/ci.sh build-jar ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}' + + stage ('Build Docker image') { + sh 'scripts/ci.sh build-docker ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}' + } + stage ('Publish Docker image') { + withCredentials( + [ + usernamePassword( + credentialsId: 'DOCKERHUB_CREDENTIAL', + usernameVariable: 'DOCKERHUB_USER', + passwordVariable: 'DOCKERHUB_TOKEN' + ) + ] + ) { + docker.withRegistry('https://index.docker.io/v1/', 'DOCKERHUB_CREDENTIAL') { + sh 'scripts/ci.sh publish-docker ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}' + } + } + } + } + + stage ('Clean Docker images') { + sh 'scripts/ci.h clean ${NAMESPACE} docker.io bioformat ${BRANCH_NAME' + } + } +} diff --git a/scripts/build-version.sh b/scripts/build-version.sh new file mode 100644 index 0000000..9ecb442 --- /dev/null +++ b/scripts/build-version.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +set -e + +function get_git_head_branch { + # Go to root + cd $(git rev-parse --show-toplevel) + + echo $(git rev-parse --abbrev-ref HEAD) +} + +function get_git_tag { + # Go to root + cd $(git rev-parse --show-toplevel) + + lastGitTag=$(git describe --long --dirty) + if [[ $lastGitTag =~ v[0-9]+.[0-9]+.[0-9]+-0-[0-9a-g]{8,9}$ ]]; then + echo $lastGitTag + else + echo ${1:-get_git_head_branch} + fi +} + +function get_version_number { + # Go to root + cd $(git rev-parse --show-toplevel) + + lastGitTag=$(git describe --long --dirty) + if [[ $lastGitTag =~ v[0-9]+.[0-9]+.[0-9]+-0-[0-9a-g]{8,9}$ ]]; then + # official release x.y.z + versionNumber=$(echo $lastGitTag | sed -r "s/v([0-9]+\.[0-9]+\.[0-9]+)-[0-9]+-.+/\1/") + else + # rc: branchname + date + 'SNAPSHOT' + versionNumber=${1:-get_git_head_branch}-$(date "+%Y%m%d%H%M%S")-SNAPSHOT + fi + echo $versionNumber +} + diff --git a/scripts/ci.sh b/scripts/ci.sh new file mode 100644 index 0000000..ef7ff25 --- /dev/null +++ b/scripts/ci.sh @@ -0,0 +1,130 @@ +#!/bin/bash + +set -e + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +ROOT="$( cd $DIR && cd .. && pwd )" +ME=$(basename $0) + +source $DIR/build-version.sh + +NAMESPACE=${2:-cytomine} +DOCKER_REGISTRY=${3:-docker.io} +IMAGE_NAME=${4:-bioformat} +BRANCH=${5:-$(get_git_head_branch)} +TAG=$(get_git_tag $BRANCH) +VERSION_NUMBER=${VERSION_NUMBER:-$(get_version_number $BRANCH)} + +function printInfo() { + echo "************************************** $ME - $1 ******************************************" + echo "$ME - $1: Git branch/tag is $TAG" + echo "$ME - $1: Version number is $VERSION_NUMBER" + echo "$ME - $1: Docker Registry is $DOCKER_REGISTRY" + echo "$ME - $1: Docker Namespace is $NAMESPACE" +} + +function downloadDependencies() { + printInfo "downloadDependencies" + + image=$NAMESPACE/$IMAGE_NAME-download-dependencies:v$VERSION_NUMBER + docker build \ + -f $DIR/docker/Dockerfile \ + --target dependencies \ + -t $image $ROOT + + containerDownloadDependenciesId=$(docker create $image) + docker rm $containerDownloadDependenciesId +} + +function buildJar() { + printInfo "buildJar" + + image=$NAMESPACE/$IMAGE_NAME-jar:v$VERSION_NUMBER + docker build \ + -f $DIR/docker/Dockerfile \ + --target builder \ + --build-arg VERSION_NUMBER=$VERSION_NUMBER \ + -t $image $ROOT + + mkdir -p ./ci + containerId=$(docker create $image) + docker start -ai $containerId + docker cp $containerId:/app/build/libs/cytomine-bioformats-wrapper.jar ./ci + docker rm $containerId +} + +function publishJar() { + printInfo "publishJar" + # TODO +} + +function buildDockerImage() { + printInfo "buildDockerImage" + + image=$NAMESPACE/$IMAGE_NAME:v$VERSION_NUMBER + docker build \ + -f $DIR/docker/Dockerfile \ + --build-arg VERSION_NUMBER=$VERSION_NUMBER \ + -t $image $ROOT +} + +function publishDockerImage() { + printInfo "publishDockerImage" + + image=$NAMESPACE/$IMAGE_NAME:v$VERSION_NUMBER + docker push $DOCKER_REGISTRY/$image +} + +function imageDockerExists() { + docker inspect $1 > /dev/null; echo $? +} + +function cleanDocker() { + printInfo "cleanDocker" + image=$NAMESPACE/$IMAGE_NAME-download-dependencies:v$VERSION_NUMBER + if [[ $(imageDockerExists $image) ]]; then + docker rmi $image + fi + + image=$NAMESPACE/$IMAGE_NAME-jar:v$VERSION_NUMBER + if [[ $(imageDockerExists $image) ]]; then + docker rmi $image + fi +} + +# Commands +case $1 in + "dependencies") + downloadDependencies + ;; + "build-jar") + buildJar + ;; + "publish-jar") + publishJar + ;; + "build-docker") + buildDockerImage + ;; + "publish-docker") + publishDockerImage + ;; + "clean-docker") + cleanDocker + ;; + "build") + downloadDependencies + buildJar + buildDockerImage + ;; + "all") + downloadDependencies + buildJar + publishJar + buildDockerImage + publishDockerImage + cleanDocker + ;; + *) + echo "No command found." +esac \ No newline at end of file From b3108abaf2a1374e3689a7b201aeb3698b0b0745 Mon Sep 17 00:00:00 2001 From: Ulysse Rubens Date: Thu, 23 Jun 2022 16:08:18 +0200 Subject: [PATCH 3/9] fix: version computation for CI --- Jenkinsfile | 6 ++++- scripts/build-version.sh | 38 ---------------------------- scripts/ci.sh | 54 +++++++++++++++++++++++++++++++++------- 3 files changed, 50 insertions(+), 48 deletions(-) delete mode 100644 scripts/build-version.sh diff --git a/Jenkinsfile b/Jenkinsfile index a4dd550..f748b06 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -14,7 +14,11 @@ node { } stage ('Compute version number') { - env.VERSION_NUMBER = sh(script: 'scripts/compute-version.sh ${BRANCH_NAME}', returnStdout: true) + env.VERSION_NUMBER = sh( + script: 'scripts/ci.sh version-number ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}', + returnStdout: true + ) + echo("Version number: ${env.VERSION_NUMBER}") stage ('Download and cache dependencies') { sh 'scripts/ci.sh dependencies ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}' diff --git a/scripts/build-version.sh b/scripts/build-version.sh deleted file mode 100644 index 9ecb442..0000000 --- a/scripts/build-version.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash - -set -e - -function get_git_head_branch { - # Go to root - cd $(git rev-parse --show-toplevel) - - echo $(git rev-parse --abbrev-ref HEAD) -} - -function get_git_tag { - # Go to root - cd $(git rev-parse --show-toplevel) - - lastGitTag=$(git describe --long --dirty) - if [[ $lastGitTag =~ v[0-9]+.[0-9]+.[0-9]+-0-[0-9a-g]{8,9}$ ]]; then - echo $lastGitTag - else - echo ${1:-get_git_head_branch} - fi -} - -function get_version_number { - # Go to root - cd $(git rev-parse --show-toplevel) - - lastGitTag=$(git describe --long --dirty) - if [[ $lastGitTag =~ v[0-9]+.[0-9]+.[0-9]+-0-[0-9a-g]{8,9}$ ]]; then - # official release x.y.z - versionNumber=$(echo $lastGitTag | sed -r "s/v([0-9]+\.[0-9]+\.[0-9]+)-[0-9]+-.+/\1/") - else - # rc: branchname + date + 'SNAPSHOT' - versionNumber=${1:-get_git_head_branch}-$(date "+%Y%m%d%H%M%S")-SNAPSHOT - fi - echo $versionNumber -} - diff --git a/scripts/ci.sh b/scripts/ci.sh index ef7ff25..15db9ca 100644 --- a/scripts/ci.sh +++ b/scripts/ci.sh @@ -6,14 +6,39 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" ROOT="$( cd $DIR && cd .. && pwd )" ME=$(basename $0) -source $DIR/build-version.sh +function getGitHeadBranch { + # Go to root + cd $(git rev-parse --show-toplevel) -NAMESPACE=${2:-cytomine} -DOCKER_REGISTRY=${3:-docker.io} -IMAGE_NAME=${4:-bioformat} -BRANCH=${5:-$(get_git_head_branch)} -TAG=$(get_git_tag $BRANCH) -VERSION_NUMBER=${VERSION_NUMBER:-$(get_version_number $BRANCH)} + echo $(git rev-parse --abbrev-ref HEAD) +} + +function getGitTag { + # Go to root + cd $(git rev-parse --show-toplevel) + + lastGitTag=$(git describe --long --dirty) + if [[ $lastGitTag =~ v[0-9]+.[0-9]+.[0-9]+-0-[0-9a-g]{8,9}$ ]]; then + echo $lastGitTag + else + echo ${BRANCH:-$(getGitHeadBranch)} + fi +} + +function getVersionNumber { + # Go to root + cd $(git rev-parse --show-toplevel) + + lastGitTag=$(git describe --long --dirty --tags) + if [[ $lastGitTag =~ v[0-9]+.[0-9]+.[0-9]+-0-[0-9a-g]{8,9}$ ]]; then + # official release x.y.z + versionNumber=$(echo $lastGitTag | sed -r "s/v([0-9]+\.[0-9]+\.[0-9]+)-[0-9]+-.+/\1/") + else + # rc: branchname + date + 'SNAPSHOT' + versionNumber=${BRANCH:-$(getGitHeadBranch)}-$(date "+%Y%m%d%H%M%S")-SNAPSHOT + fi + echo $versionNumber +} function printInfo() { echo "************************************** $ME - $1 ******************************************" @@ -49,7 +74,7 @@ function buildJar() { mkdir -p ./ci containerId=$(docker create $image) docker start -ai $containerId - docker cp $containerId:/app/build/libs/cytomine-bioformats-wrapper.jar ./ci + docker cp $containerId:/app/build/libs/cytomine-bioformats-wrapper.jar ./output docker rm $containerId } @@ -92,8 +117,19 @@ function cleanDocker() { fi } -# Commands +#### Variables +NAMESPACE=${2:-cytomine} +DOCKER_REGISTRY=${3:-docker.io} +IMAGE_NAME=${4:-bioformat} +BRANCH=${5:-$(getGitHeadBranch)} +TAG=$(getGitTag $BRANCH) +VERSION_NUMBER=${VERSION_NUMBER:-$(getVersionNumber $BRANCH)} + +#### Commands case $1 in + "version-number") + echo $VERSION_NUMBER + ;; "dependencies") downloadDependencies ;; From b70d093ec4a6086f1e2ace264a3f5153b7a389e4 Mon Sep 17 00:00:00 2001 From: Ulysse Rubens Date: Thu, 23 Jun 2022 16:14:44 +0200 Subject: [PATCH 4/9] fix: ci script chmod --- .idea/libraries/bioformats_package.xml | 9 --------- scripts/ci.sh | 0 2 files changed, 9 deletions(-) delete mode 100644 .idea/libraries/bioformats_package.xml mode change 100644 => 100755 scripts/ci.sh diff --git a/.idea/libraries/bioformats_package.xml b/.idea/libraries/bioformats_package.xml deleted file mode 100644 index e7831ee..0000000 --- a/.idea/libraries/bioformats_package.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/scripts/ci.sh b/scripts/ci.sh old mode 100644 new mode 100755 From 604dabea6dec41b711e98c74c49eed19e3d4ae56 Mon Sep 17 00:00:00 2001 From: Ulysse Rubens Date: Tue, 28 Jun 2022 16:01:29 +0200 Subject: [PATCH 5/9] chore: fix typo in Jenkinsfile --- Jenkinsfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index f748b06..de6ba41 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -29,7 +29,7 @@ node { } stage ('Publish jar') { - sh 'scripts/ci.sh build-jar ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}' + sh 'scripts/ci.sh publish-jar ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}' stage ('Build Docker image') { sh 'scripts/ci.sh build-docker ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}' @@ -52,7 +52,7 @@ node { } stage ('Clean Docker images') { - sh 'scripts/ci.h clean ${NAMESPACE} docker.io bioformat ${BRANCH_NAME' + sh 'scripts/ci.h clean ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}' } } } From 1d6489cdd090bd01d84bb0c831bf4e24c6349978 Mon Sep 17 00:00:00 2001 From: Ulysse Rubens Date: Tue, 28 Jun 2022 16:02:40 +0200 Subject: [PATCH 6/9] chore: fix typo in ci.sh --- scripts/ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ci.sh b/scripts/ci.sh index 15db9ca..f91603b 100755 --- a/scripts/ci.sh +++ b/scripts/ci.sh @@ -71,7 +71,7 @@ function buildJar() { --build-arg VERSION_NUMBER=$VERSION_NUMBER \ -t $image $ROOT - mkdir -p ./ci + mkdir -p ./output containerId=$(docker create $image) docker start -ai $containerId docker cp $containerId:/app/build/libs/cytomine-bioformats-wrapper.jar ./output From 25ffbe19fc74027e3570bd3f4376df60680a84a8 Mon Sep 17 00:00:00 2001 From: Ulysse Rubens Date: Tue, 28 Jun 2022 16:08:14 +0200 Subject: [PATCH 7/9] chore: fix typo in Jenkinsfile --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index de6ba41..e7920fa 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -52,7 +52,7 @@ node { } stage ('Clean Docker images') { - sh 'scripts/ci.h clean ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}' + sh 'scripts/ci.sh clean ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}' } } } From 4f3d795dd7a1204b9d970cd9d4f61c45f226a558 Mon Sep 17 00:00:00 2001 From: Ulysse Rubens Date: Wed, 29 Jun 2022 10:12:32 +0200 Subject: [PATCH 8/9] feat: add docker healthcheck --- scripts/docker/Dockerfile | 7 +++++-- scripts/docker/docker-healthcheck.sh | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100755 scripts/docker/docker-healthcheck.sh diff --git a/scripts/docker/Dockerfile b/scripts/docker/Dockerfile index af9268b..46c38c9 100644 --- a/scripts/docker/Dockerfile +++ b/scripts/docker/Dockerfile @@ -44,11 +44,14 @@ RUN sed -i -- 's/version '0.0.0'/version '$VERSION_NUMBER'/g' build.gradle \ FROM openjdk:8 AS production +ENV BIOFORMAT_POOL_SIZE 4 +EXPOSE 4321 + WORKDIR /app COPY --from=builder /app/build/libs/cytomine-bioformats-wrapper.jar . -ENV BIOFORMAT_POOL_SIZE 4 -EXPOSE 4321 +COPY scripts/docker/docker-healthcheck.sh /usr/local/bin/docker-healthcheck +HEALTHCHECK CMD ["docker-healthcheck"] # We use CMD shell form to allow variable substitution CMD java -jar /app/cytomine-bioformats-wrapper.jar 4321 ${BIOFORMAT_POOL_SIZE} diff --git a/scripts/docker/docker-healthcheck.sh b/scripts/docker/docker-healthcheck.sh new file mode 100755 index 0000000..52686c4 --- /dev/null +++ b/scripts/docker/docker-healthcheck.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -e + +COUNT=$(ps aux | grep "java -jar /app/cytomine-bioformats-wrapper.jar" | grep -v grep | wc -l) +if [ $COUNT -ge 1 ]; then + # Healthy + exit 0 +fi + +# Unhealthy +exit 1 \ No newline at end of file From bfa825c25b36a921964ef3e6b951727bcdbba54e Mon Sep 17 00:00:00 2001 From: Ulysse Rubens Date: Thu, 30 Jun 2022 11:11:02 +0200 Subject: [PATCH 9/9] chore: add release on Github when it's an official one --- Jenkinsfile | 28 ++++++++++++++++++++++++- scripts/ci.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index e7920fa..0eb3a4c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -49,10 +49,36 @@ node { } } } + + stage ('Check official release status') { + env.OFFICIAL_RELEASE = sh( + script: 'scripts/ci.sh is-official-release ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}', + returnStdout: true + ) + echo("Official Release to publish on Github ? ${env.OFFICIAL_RELEASE}") + + if (env.OFFICIAL_RELEASE && env.OFFICIAL_RELEASE.toBoolean()) { + echo("official") + stage ('Publish official release on Github') { + env.GITHUB_REPO = scm.getUserRemoteConfigs()[0].getUrl().replaceFirst(/^.*?(?::\/\/.*?\/|:)(.*).git$/, '$1') + withCredentials( + [ + usernamePassword( + credentialsId: 'GITHUB_RELEASE_CREDENTIAL', + usernameVariable: 'GITHUB_RELEASE_USER', + passwordVariable: 'GITHUB_RELEASE_TOKEN' + ) + ] + ) { + sh 'scripts/ci.sh publish-github' + } + } + } + } } stage ('Clean Docker images') { - sh 'scripts/ci.sh clean ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}' + sh 'scripts/ci.sh clean-docker ${NAMESPACE} docker.io bioformat ${BRANCH_NAME}' } } } diff --git a/scripts/ci.sh b/scripts/ci.sh index f91603b..a24e05b 100755 --- a/scripts/ci.sh +++ b/scripts/ci.sh @@ -4,6 +4,7 @@ set -e DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" ROOT="$( cd $DIR && cd .. && pwd )" +OUTPUT=$ROOT/output ME=$(basename $0) function getGitHeadBranch { @@ -25,6 +26,18 @@ function getGitTag { fi } +function isOfficialRelease { + # Go to root + cd $(git rev-parse --show-toplevel) + + lastGitTag=$(git describe --long --dirty --tags) + if [[ $lastGitTag =~ v[0-9]+.[0-9]+.[0-9]+-0-[0-9a-g]{8,9}$ ]]; then + echo "true" + else + echo "false" + fi +} + function getVersionNumber { # Go to root cd $(git rev-parse --show-toplevel) @@ -71,10 +84,10 @@ function buildJar() { --build-arg VERSION_NUMBER=$VERSION_NUMBER \ -t $image $ROOT - mkdir -p ./output + mkdir -p $OUTPUT containerId=$(docker create $image) docker start -ai $containerId - docker cp $containerId:/app/build/libs/cytomine-bioformats-wrapper.jar ./output + docker cp $containerId:/app/build/libs/cytomine-bioformats-wrapper.jar $OUTPUT docker rm $containerId } @@ -117,6 +130,41 @@ function cleanDocker() { fi } +function publishGithub() { + if [[ $(isOfficialRelease) == "true" ]]; then + GITHUB_REPO=${GITHUB_REPO:?"GITHUB_REPO is unset. Abort."} + GITHUB_RELEASE_USER=${GITHUB_RELEASE_USER:?"GITHUB_RELEASE_USER is unset. Abort."} + GITHUB_RELEASE_TOKEN=${GITHUB_RELEASE_TOKEN:?"GITHUB_RELEASE_TOKEN is unset. Abort."} + + # Create a release + release=$( + curl https://api.github.com/repos/${GITHUB_REPO}/releases \ + -X POST \ + --trace-ascii - \ + -H "Accept: application/vnd.github.v3+json" \ + -H "Authorization: token ${GITHUB_RELEASE_TOKEN}" \ + --data @<(cat <