Benjamin Copeland | c78807d | 2019-04-02 08:54:42 +0100 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | |
| 3 | set -ex |
| 4 | |
| 5 | trap cleanup_exit INT TERM EXIT |
| 6 | |
| 7 | cleanup_exit() |
| 8 | { |
| 9 | rm -rf ${HOME}/.docker |
| 10 | rm -f ${WORKSPACE}/{log,config.json,version.txt} |
| 11 | } |
| 12 | |
| 13 | mkdir -p ${HOME}/.docker |
| 14 | sed -e "s|\${DOCKER_AUTH}|${DOCKER_AUTH}|" < ${WORKSPACE}/config.json > ${HOME}/.docker/config.json |
| 15 | chmod 0600 ${HOME}/.docker/config.json |
| 16 | |
| 17 | echo "" |
| 18 | echo "########################################################################" |
| 19 | echo " Gerrit Environment" |
| 20 | env |grep '^GERRIT' |
| 21 | echo "########################################################################" |
| 22 | |
| 23 | rm -f ${WORKSPACE}/{log,config.json,version.txt} |
| 24 | cd dockerfiles/ |
| 25 | |
| 26 | git_previous_commit=$(git rev-parse HEAD~1) |
| 27 | git_commit=$(git rev-parse HEAD) |
| 28 | files=$(git diff --name-only ${git_previous_commit} ${git_commit}) |
| 29 | echo Changes in: ${files} |
| 30 | changed_dirs=$(dirname ${files}) |
| 31 | |
| 32 | update_images="" |
| 33 | for dir in ${changed_dirs}; do |
| 34 | # Find the closest directory with build.sh. This is, primarily, |
| 35 | # to handle changes to tcwg-base/tcwg-build/tcwg-builslave/* directories. |
| 36 | while [ ! -e ${dir}/build.sh -a ! -e ${dir}/.git ]; do |
| 37 | dir=$(dirname ${dir}) |
| 38 | done |
| 39 | # Add this and all dependant images in the update. |
| 40 | dir_basename=$(basename ${dir}) |
| 41 | case "${dir_basename}" in |
| 42 | "tcwg-"*) |
| 43 | # ${dir} is one of generic tcwg-base/* directories. Add dependent |
| 44 | # images to the list. |
| 45 | update_images="${update_images} $(dirname $(find . -path "*-${dir_basename}*/build.sh" | sed -e "s#^\./##g"))" |
| 46 | ;; |
| 47 | *) |
| 48 | update_images="${update_images} $(dirname $(find ${dir} -name build.sh))" |
| 49 | ;; |
| 50 | esac |
| 51 | done |
| 52 | update_images="$(echo "${update_images}" | tr " " "\n" | sort -u)" |
| 53 | |
| 54 | host_arch=$(dpkg-architecture -qDEB_HOST_ARCH) |
| 55 | |
| 56 | for image in ${update_images}; do |
| 57 | ( |
| 58 | cd ${image} |
| 59 | image_arch=$(basename ${PWD} | cut -f2 -d '-') |
| 60 | skip="skip" |
| 61 | if [ -f gerrit-branches ]; then |
| 62 | # Build only from branches mentioned in gerrit-branches |
| 63 | if grep -q "^${GERRIT_BRANCH}\$" gerrit-branches; then |
| 64 | skip="no" |
| 65 | fi |
| 66 | elif [ x"${GERRIT_BRANCH}" = x"master" ]; then |
| 67 | # No gerrit-branch file, so build only from "master" branch. |
| 68 | skip="no" |
| 69 | fi |
| 70 | case "${skip}:${host_arch}:${image_arch}" in |
| 71 | "skip:"*) |
| 72 | echo "Skipping: don't need to build ${image} on branch ${GERRIT_BRANCH}" |
| 73 | ;; |
| 74 | "no:amd64:amd64"|"no:amd64:i386"|"no:arm64:arm64"|"no:armhf:armhf") |
| 75 | echo "=== Start build: ${image} ===" |
| 76 | ./build.sh || echo "=== FAIL: ${image} ===" >> ${WORKSPACE}/log |
| 77 | ;; |
| 78 | *) |
| 79 | echo "Skipping: can't build for ${image_arch} on ${host_arch}" |
| 80 | ;; |
| 81 | esac |
| 82 | if [ -r .docker-tag ]; then |
| 83 | docker_tag=$(cat .docker-tag) |
| 84 | if [ x"${GERRIT_BRANCH}" != x"master" ]; then |
| 85 | new_tag=${docker_tag}-${GERRIT_BRANCH} |
| 86 | docker tag ${docker_tag} ${new_tag} |
| 87 | docker_tag=${new_tag} |
| 88 | fi |
| 89 | docker push ${docker_tag} |
| 90 | fi |
| 91 | )||echo $image failed >> ${WORKSPACE}/log |
| 92 | done |
| 93 | |
| 94 | if [ -e ${WORKSPACE}/log ] |
| 95 | then |
| 96 | echo "some images failed:" |
| 97 | cat ${WORKSPACE}/log |
| 98 | exit 1 |
| 99 | fi |