From 4947aa151e267b42b037be99f65d9a6c8c13c29f Mon Sep 17 00:00:00 2001 From: Pavol Mederly Date: Fri, 5 Oct 2018 23:47:10 +0200 Subject: [PATCH] Continue build procedure cleanup $tag is now defined in common.bash and used throughout building scripts. The goal is to allow building and testing both from Jenkins and standalone. build.sh is now more generic; allows rebuilding if -r is specified. --- Dockerfile | 2 +- Jenkinsfile | 13 +++++-------- build.sh | 31 +++++++++++++++++++++++++------ download-midpoint | 11 ++++++++--- jenkins-rebuild.sh | 22 ---------------------- tests/main.bats | 4 ++-- 6 files changed, 41 insertions(+), 42 deletions(-) delete mode 100755 jenkins-rebuild.sh diff --git a/Dockerfile b/Dockerfile index d4e2e81..7a76f7a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # -# Building assumes midpoint-3.9-SNAPSHOT-dist.tar.gz is present in the current directory. +# Building assumes midpoint-dist.tar.gz is present in the current directory. # FROM tier/shibboleth_sp diff --git a/Jenkinsfile b/Jenkinsfile index cd48ffc..c9311d7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -27,7 +27,9 @@ pipeline { sh 'ls' sh 'mv bin/* ../bin/.' } - sh "echo \"tag=\\\"${tag}\\\"\" > tag.bash ; chmod a+x tag.bash ; echo tag.bash ; cat tag.bash' + // Build and test scripts expect that 'tag' is present in common.bash. This is necessary for both Jenkins and standalone testing. + // We don't care if there are more 'tag' assignments there. The latest one wins. + sh "echo >> common.bash ; echo \"tag=\\\"${tag}\\\"\" >> common.bash ; echo common.bash ; cat common.bash' } } } @@ -35,13 +37,8 @@ pipeline { steps { script { try { - if (env.BRANCH_NAME == "master") { - toDownload = "3.9-SNAPSHOT" - } else { - toDownload = env.BRANCH_NAME - } - sh "./download-midpoint ${toDownload} 2>&1 | tee -a debug ; test \${PIPESTATUS[0]} -eq 0" - sh './jenkins-rebuild.sh 2>&1 | tee -a debug ; test ${PIPESTATUS[0]} -eq 0' // temporary + // using custom ./build.sh instead of bin/rebuild.sh because the bin/ version does not support building specific tag yet + sh './build.sh -r 2>&1 | tee -a debug ; test ${PIPESTATUS[0]} -eq 0' } catch (error) { def error_details = readFile('./debug') def message = "BUILD ERROR: There was a problem building ${imagename}:${tag}. \n\n ${error_details}" diff --git a/build.sh b/build.sh index 783e3bf..cb5d55c 100755 --- a/build.sh +++ b/build.sh @@ -1,16 +1,35 @@ #!/bin/bash cd "$(dirname "$0")" +source common.bash + SKIP_DOWNLOAD=0 -while getopts "nh?" opt; do +while getopts "nhr?" opt; do case $opt in - n) SKIP_DOWNLOAD=1 ;; - h | ?) echo "Options: -n skip download" ; exit 0 ;; - *) echo "Unknown option: $opt" ; exit 1 ;; + n) + SKIP_DOWNLOAD=1 + ;; + r) + result=$(docker ps -a | grep $maintainer/$imagename:$tag) + if [ ! -z "$result" ]; then + echo "Cleaning up $maintainer/$imagename:$tag..." + docker rm -f $(docker ps -a | grep $maintainer/$imagename:$tag | awk '{print $1}') + docker rmi -f $maintainer/$imagename:$tag + echo "Done" + fi + ;; + h | ?) + echo "Options: -n skip download" + exit 0 + ;; + *) + echo "Unknown option: $opt" + exit 1 + ;; esac done -if [ "$SKIP_DOWNLOAD" = "0" ]; then ./download-midpoint; fi -docker build --tag tier/midpoint:latest . +if [ "$SKIP_DOWNLOAD" = "0" ]; then ./download-midpoint || exit 1; fi +docker build --tag $maintainer/$imagename:$tag --build-arg maintainer=$maintainer --build-arg imagename=$imagename . || exit 1 echo "---------------------------------------------------------------------------------------" echo "The midPoint containers were successfully built. To start them, execute the following:" echo "" diff --git a/download-midpoint b/download-midpoint index fe04d58..a803f95 100755 --- a/download-midpoint +++ b/download-midpoint @@ -1,17 +1,22 @@ #!/bin/bash DIR=`dirname "$0"` +source $DIR/common.bash if [[ -n "$1" ]]; then MP_VERSION=$1 else - MP_VERSION=3.9-SNAPSHOT + if [[ $tag == "latest" ]]; then + MP_VERSION=3.9-SNAPSHOT + else + MP_VERSION=$tag + fi fi echo "Downloading midPoint $MP_VERSION" echo "-----------------------------------------" -curl --output $dir/midpoint-dist.tar.gz "https://evolveum.com/downloads/midpoint-tier/midpoint-$MP_VERSION-dist.tar.gz" +curl --output $DIR/midpoint-dist.tar.gz "https://evolveum.com/downloads/midpoint-tier/midpoint-$MP_VERSION-dist.tar.gz" echo "-----------------------------------------" echo "Checking the download..." -if tar -tf $dir/midpoint-dist.tar.gz >/dev/null; then +if tar -tf $DIR/midpoint-dist.tar.gz >/dev/null; then echo "OK" exit 0 else diff --git a/jenkins-rebuild.sh b/jenkins-rebuild.sh deleted file mode 100755 index b368c74..0000000 --- a/jenkins-rebuild.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash - -# Temporary workaround for docker/util not rebuilding the specific version of the image - -# from destroy.sh (adapted) - -source common.bash -source tag.bash - -echo "Rebuilding $maintainer/$imagename:$tag..." - -result=$(docker ps -a | grep $maintainer/$imagename:$tag) - -if [ ! -z "$result" ]; then - docker rm -f $(docker ps -a | grep $maintainer/$imagename:$tag | awk '{print $1}') - docker rmi -f $maintainer/$imagename:$tag -fi - -# from build.sh (adapted) - -echo "Building new Docker image($maintainer/$imagename:$tag)" -docker build --rm -t $maintainer/$imagename:$tag --build-arg maintainer=$maintainer --build-arg imagename=$imagename . diff --git a/tests/main.bats b/tests/main.bats index 36dc6c1..7d7f2c7 100755 --- a/tests/main.bats +++ b/tests/main.bats @@ -3,11 +3,11 @@ load ../common @test "010 Image is present" { - docker image inspect tier/midpoint:latest + docker image inspect tier/midpoint:$tag } @test "020 Check basic components" { - docker run -i $maintainer/$imagename \ + docker run -i $maintainer/$imagename:$tag \ find \ /usr/local/bin/startup.sh \ /opt/midpoint/var/ \