incr.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/bin/bash
  2. ##############################################################################
  3. #
  4. # HPCC SYSTEMS software Copyright (C) 2020 HPCC Systems®.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. ##############################################################################
  18. # Use this script to build local images for testing, while leveraging the work already done in the most recent tag
  19. # on the branch that your local branch was based on
  20. #
  21. # Pass in a get commit (that you previously built using this script) if you want to override the default calculation
  22. # of the base build
  23. # NB: INPUT_* may be pre-set as environment variables.
  24. while getopts “d:flpt:u:” opt; do
  25. case $opt in
  26. l) TAGLATEST=1 ;;
  27. p) PUSH=1 ;;
  28. t) INPUT_BUILD_THREADS=$OPTARG ;;
  29. d) INPUT_DOCKER_REPO=$OPTARG ;;
  30. u) INPUT_BUILD_USER=$OPTARG ;;
  31. b) INPUT_BUILD_TYPE=$OPTARG ;;
  32. f) FORCE=1 ;;
  33. esac
  34. done
  35. shift $(( $OPTIND-1 ))
  36. # NB: Not used if FORCE build
  37. PREV=$1
  38. DOCKER_REPO=hpccsystems
  39. [[ -n ${INPUT_DOCKER_REPO} ]] && DOCKER_REPO=${INPUT_DOCKER_REPO}
  40. BUILD_TYPE=Debug
  41. [[ -n ${INPUT_BUILD_TYPE} ]] && BUILD_TYPE=${INPUT_BUILD_TYPE}
  42. BUILD_THREADS=$INPUT_BUILD_THREADS # If not set, picks up default based on nproc
  43. HEAD=$(git rev-parse --short HEAD)
  44. BUILD_LABEL="${HEAD}-Debug"
  45. if [[ -z "$FORCE" ]] ; then
  46. # Look for an image that matches a commit, exclude images based on dirty working tree.
  47. if [[ -z ${PREV} ]] ; then
  48. docker images ${DOCKER_REPO}/platform-build --format {{.Tag}} | egrep -ve '-dirty.*$' > .candidate-tags
  49. PREV=$(git log --format=format:%h-Debug $(git describe --abbrev=0 --tags)..HEAD | fgrep -f .candidate-tags | head -n 1)
  50. rm -f .candidate-tags
  51. fi
  52. # If not found above, look for latest tagged
  53. if [[ -z ${PREV} ]] ; then
  54. PREV=$(git describe --abbrev=0 --tags)-Debug
  55. IMAGE_FOUND=$(docker images ${DOCKER_REPO}/platform-build --format {{.Tag}} | fgrep "$PREV" | head -n 1)
  56. if [[ -z "$IMAGE_FOUND" ]] ; then
  57. echo "Could not locate docker image based on PREV tag: ${PREV} for docker user: ${DOCKER_REPO}"
  58. exit
  59. fi
  60. fi
  61. PREV_COMMIT=$(echo "${PREV}" | sed -e "s/-Debug.*$//")
  62. # create empty patch file
  63. echo -n > platform-build-incremental/hpcc.gitpatch
  64. if [[ -n "$(git status -uno --porcelain)" ]] ; then
  65. git diff --binary ${PREV_COMMIT} ':!./' > platform-build-incremental/hpcc.gitpatch
  66. # PATCH_MD5 is an ARG of the docker file, which ensures that if different from cached version, image will rebuild from that stage
  67. PATCH_MD5=$(md5sum platform-build-incremental/hpcc.gitpatch | awk '{print $1}')
  68. DIRTY="-dirty-${PATCH_MD5}"
  69. # If working tree is dirty, annotate build tag, so doesn't conflict with base commit
  70. BUILD_LABEL="${BUILD_LABEL}${DIRTY}"
  71. elif [[ "$BUILD_LABEL" == "$PREV" ]] ; then
  72. echo Checking if docker image ${DOCKER_REPO}/platform-core:${BUILD_LABEL} already exists
  73. EXISTING_IMAGE_LABEL=$(git log --format=format:%h-Debug $(git describe --abbrev=0 --tags)..HEAD | grep `docker images ${DOCKER_REPO}/platform-build --format {{.Tag}} | head -n 2 | tail -n 1`)
  74. if [[ -n ${EXISTING_IMAGE_LABEL} ]] ; then
  75. PREV=${EXISTING_IMAGE_LABEL}
  76. PREV_COMMIT=$(echo "${PREV}" | sed -e "s/-Debug//")
  77. fi
  78. fi
  79. fi
  80. set -e
  81. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  82. pushd $DIR 2>&1 > /dev/null
  83. build_image() {
  84. local name=$1
  85. local dockerfolder=$2
  86. # if 2nd arg. present, it names the docker folder, otherwise same as $name
  87. [[ -z ${dockerfolder} ]] && dockerfolder=$name
  88. local label=$BUILD_LABEL
  89. if ! docker pull ${DOCKER_REPO}/${name}:${label} ; then
  90. docker image build -t ${DOCKER_REPO}/${name}:${label} \
  91. --build-arg DOCKER_REPO=${DOCKER_REPO} \
  92. --build-arg BUILD_LABEL=${BUILD_LABEL} \
  93. --build-arg BUILD_TYPE=${BUILD_TYPE} ${@:3} \
  94. ${dockerfolder}/
  95. if [ "$TAGLATEST" = "1" ] ; then
  96. docker tag ${DOCKER_REPO}/${name}:${label} ${DOCKER_REPO}/${name}:latest
  97. if [ "$PUSH" = "1" ] ; then
  98. docker push ${DOCKER_REPO}/${name}:${label}
  99. docker push ${DOCKER_REPO}/${name}:latest
  100. fi
  101. else
  102. if [ "$PUSH" = "1" ] ; then
  103. docker push ${DOCKER_REPO}/${name}:${label}
  104. fi
  105. fi
  106. fi
  107. }
  108. if [[ -n "$FORCE" ]] ; then
  109. echo Building local forced build images [ BUILD_LABEL=${BUILD_LABEL} ]
  110. build_image platform-build platform-build --build-arg BUILD_USER=${INPUT_BUILD_USER} --build-arg BUILD_TAG=${HEAD} --build-arg BASE_VER=7.8 --build-arg BUILD_THREADS=${BUILD_THREADS}
  111. else
  112. echo Building local incremental images [BUILD_LABEL=${BUILD_LABEL}] based on ${PREV}
  113. build_image platform-build platform-build-incremental --build-arg DOCKER_REPO=${DOCKER_REPO} --build-arg PREV_LABEL=${PREV} --build-arg PATCH_MD5=${PATCH_MD5} --build-arg BUILD_THREADS=${BUILD_THREADS}
  114. # rm platform-build-incremental/hpcc.gitpatch
  115. fi
  116. build_image platform-core platform-core-debug
  117. build_image roxie
  118. build_image dali
  119. build_image esp
  120. build_image eclccserver
  121. build_image eclagent
  122. build_image hthor
  123. build_image toposerver
  124. build_image thormaster
  125. build_image thorslave
  126. echo Built ${DOCKER_REPO}/*:${BUILD_LABEL}